From 9872994fba121e6d8aec4b5535386ec2fd47d8fa Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 1 Mar 2024 13:08:14 +0100 Subject: [PATCH] [CI] Add Docker-Publish pipeline Signed-off-by: Knut Ahlers --- .github/workflows/docker-publish.yml | 36 +++++++++++++++++++++++++ Dockerfile | 6 +++-- ci/docker-publish.sh | 40 ++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/docker-publish.yml create mode 100644 ci/docker-publish.sh diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..ae15e85 --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -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 + +... diff --git a/Dockerfile b/Dockerfile index df24c2f..a14de70 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM luzifer/archlinux as builder -COPY . /go/src/github.com/Luzifer/twitch-bot -WORKDIR /go/src/github.com/Luzifer/twitch-bot +COPY . /go/src/twitch-bot +WORKDIR /go/src/twitch-bot ENV CGO_ENABLED=0 \ GOPATH=/go @@ -10,10 +10,12 @@ RUN set -ex \ && pacman -Syy --noconfirm \ curl \ git \ + git-lfs \ go \ make \ nodejs-lts-hydrogen \ npm \ + && git config --global --add safe.directory /go/src/twitch-bot \ && make node_modules frontend_prod \ && go install \ -trimpath \ diff --git a/ci/docker-publish.sh b/ci/docker-publish.sh new file mode 100644 index 0000000..34f02cb --- /dev/null +++ b/ci/docker-publish.sh @@ -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."