[CI] Add Docker-Publish pipeline

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-03-01 13:08:14 +01:00
parent f2ac1acb17
commit 9872994fba
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
3 changed files with 80 additions and 2 deletions

36
.github/workflows/docker-publish.yml vendored Normal file
View file

@ -0,0 +1,36 @@
---
name: docker-publish
on:
push:
branches: ['master']
tags: ['v*']
permissions:
packages: write
jobs:
docker-publish:
defaults:
run:
shell: bash
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
lfs: true
show-progress: false
- name: Log into registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Docker Build & Publish
run: bash ci/docker-publish.sh
...

View file

@ -1,7 +1,7 @@
FROM luzifer/archlinux as builder FROM luzifer/archlinux as builder
COPY . /go/src/github.com/Luzifer/twitch-bot COPY . /go/src/twitch-bot
WORKDIR /go/src/github.com/Luzifer/twitch-bot WORKDIR /go/src/twitch-bot
ENV CGO_ENABLED=0 \ ENV CGO_ENABLED=0 \
GOPATH=/go GOPATH=/go
@ -10,10 +10,12 @@ RUN set -ex \
&& pacman -Syy --noconfirm \ && pacman -Syy --noconfirm \
curl \ curl \
git \ git \
git-lfs \
go \ go \
make \ make \
nodejs-lts-hydrogen \ nodejs-lts-hydrogen \
npm \ npm \
&& git config --global --add safe.directory /go/src/twitch-bot \
&& make node_modules frontend_prod \ && make node_modules frontend_prod \
&& go install \ && go install \
-trimpath \ -trimpath \

40
ci/docker-publish.sh Normal file
View file

@ -0,0 +1,40 @@
#!/usr/bin/env bash
set -euo pipefail
function log() {
echo "[$(date +%H:%M:%S)] $@" >&2
}
[[ -n $GITHUB_REF_NAME ]] || {
log "ERR: This script is intended to run on a Github Action only."
exit 1
}
repo="ghcr.io/${GITHUB_REPOSITORY,,}"
tags=()
case "${GITHUB_REF_TYPE}" in
branch)
# Generic build to develop: Workflow has to limit branches to master
tags+=(develop)
;;
tag)
# Build to latest & stable: Older tags are not intended to rebuild
tags+=(latest stable ${GITHUB_REF_NAME})
;;
*)
log "ERR: The ref type ${GITHUB_REF_TYPE} is not handled."
exit 1
;;
esac
log "Building Docker image..."
docker build -t "${repo}:local" .
for ref in "${tags[@]}"; do
log "Pushing Docker image to '${repo}:${ref}'..."
docker tag "${repo}:local" "${repo}:${ref}"
docker push "${repo}:${ref}"
done
log "Publish finished."