From 15b0ab2d23251b02ddf592e54be43cb7730084d2 Mon Sep 17 00:00:00 2001 From: Ryan Hodin Date: Wed, 31 Jul 2024 20:56:42 -0500 Subject: [PATCH 1/8] Add a workflow for automated image build and push --- .github/workflows/build-and-push.yml | 79 ++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 .github/workflows/build-and-push.yml diff --git a/.github/workflows/build-and-push.yml b/.github/workflows/build-and-push.yml new file mode 100644 index 00000000..e8dfcacd --- /dev/null +++ b/.github/workflows/build-and-push.yml @@ -0,0 +1,79 @@ +name: "Docker Build and Push" + +on: + release: + types: ["published"] + push: + branches: ["master"] + pull_request: + branches: ["master"] + workflow_dispatch: + inputs: + push: + type: boolean + description: "Push image after build" + default: false + tags: + type: string + description: "Tags to apply to built image in addition to commit ID (separated by spaces)" + default: "" + +jobs: + build: + name: Build and push docker images + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Prepare tag list (manual trigger) + if: github.event_name == 'workflow_dispatch' + run: | + declare -a tags + for tag in ${{ github.inputs.tags }} + do + tags+=("-t \$IMAGE:$tag") + done + tags+=("-t \$IMAGE:$(git rev-parse --short HEAD)") + echo "tags=\"${tags[@]}\"" >> $GITHUB_ENV + - name: Prepare tag list (push trigger) + if: github.event_name != 'workflow_dispatch' + run: | + release=$(git tag --points-at HEAD) + if $? + then + release="-t \$IMAGE:$release" + else + release="" + fi + echo "tags=\"-t \$IMAGE:latest -t \$IMAGE:$(git rev-parse --short HEAD)\" $release" >> $GITHUB_ENV + - name: Check push configuration + run: | + auto=[ "${{ github.event_name }}" = "push" ] && [ "${{ github.ref }}" = "refs/heads/main" ] + manual="${{ github.inputs.push }}" + release=[ "${{ github.event_name }}" = "release" ] + push=($auto || $manual || $release) && 'true' || 'false' + echo "do_push=$push" >> $GITHUB_ENV + - name: Build Cadence server image + run: | + IMAGE=kenellorando/cadence + docker build ${{ env.tags }} -f src/cadence.Dockerfile src + - name: Build icecast2 image + run: | + IMAGE=kenellorando/cadence_icecast2 + docker build ${{ env.tags }} -f src/icecast2.Dockerfile src + - name: Build liquidsoap image + run: | + IMAGE=kenellorando/cadence_liquidsoap + docker build ${{ env.tags }} -f src/liquidsoap.Dockerfile src + - name: Login to Docker Hub + if: env.push == 'true' + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_ACCESS_TOKEN }} + - name: Push Cadence images + if: env.push == 'true' + run: | + sha=$(git rev-parse --short HEAD) + docker push --all-tags kenellorando/cadence:$sha + docker push --all-tags kenellorando/cadence_icecast2:$sha + docker push --all-tags kenellorando/cadence_liquidsoap:$sha From 73e8634234a7fb4f1c8a19784829eb1f6898ce8a Mon Sep 17 00:00:00 2001 From: Ryan Hodin Date: Wed, 31 Jul 2024 21:20:57 -0500 Subject: [PATCH 2/8] Use exit codes explicitly --- .github/workflows/build-and-push.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build-and-push.yml b/.github/workflows/build-and-push.yml index e8dfcacd..52c16008 100644 --- a/.github/workflows/build-and-push.yml +++ b/.github/workflows/build-and-push.yml @@ -47,10 +47,12 @@ jobs: echo "tags=\"-t \$IMAGE:latest -t \$IMAGE:$(git rev-parse --short HEAD)\" $release" >> $GITHUB_ENV - name: Check push configuration run: | - auto=[ "${{ github.event_name }}" = "push" ] && [ "${{ github.ref }}" = "refs/heads/main" ] + [ "${{ github.event_name }}" = "push" ] && [ "${{ github.ref }}" = "refs/heads/main" ] + auto=$? manual="${{ github.inputs.push }}" - release=[ "${{ github.event_name }}" = "release" ] - push=($auto || $manual || $release) && 'true' || 'false' + [ "${{ github.event_name }}" = "release" ] + release=$? + push=`($auto || $manual || $release) && echo 'true' || echo 'false'` echo "do_push=$push" >> $GITHUB_ENV - name: Build Cadence server image run: | From 14a40701bf5a038e2572d36ba99b140276d443c0 Mon Sep 17 00:00:00 2001 From: Ryan Hodin Date: Wed, 31 Jul 2024 21:24:13 -0500 Subject: [PATCH 3/8] More logic updates --- .github/workflows/build-and-push.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-and-push.yml b/.github/workflows/build-and-push.yml index 52c16008..d53c3e64 100644 --- a/.github/workflows/build-and-push.yml +++ b/.github/workflows/build-and-push.yml @@ -49,10 +49,11 @@ jobs: run: | [ "${{ github.event_name }}" = "push" ] && [ "${{ github.ref }}" = "refs/heads/main" ] auto=$? - manual="${{ github.inputs.push }}" + [ "${{ github.inputs.push }}" = "true" ] + manual=$? [ "${{ github.event_name }}" = "release" ] release=$? - push=`($auto || $manual || $release) && echo 'true' || echo 'false'` + push=`[[ $auto || $manual || $release ]] && echo 'true' || echo 'false'` echo "do_push=$push" >> $GITHUB_ENV - name: Build Cadence server image run: | From 865f6c341c7eae71d2fbbb1918042f42596c9e7c Mon Sep 17 00:00:00 2001 From: Ryan Hodin Date: Wed, 31 Jul 2024 21:27:19 -0500 Subject: [PATCH 4/8] Use -eqs --- .github/workflows/build-and-push.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-and-push.yml b/.github/workflows/build-and-push.yml index d53c3e64..9bf02a3f 100644 --- a/.github/workflows/build-and-push.yml +++ b/.github/workflows/build-and-push.yml @@ -53,7 +53,7 @@ jobs: manual=$? [ "${{ github.event_name }}" = "release" ] release=$? - push=`[[ $auto || $manual || $release ]] && echo 'true' || echo 'false'` + push=`[[ $auto -eq 0 || $manual -eq 0 || $release -eq 0 ]] && echo 'true' || echo 'false'` echo "do_push=$push" >> $GITHUB_ENV - name: Build Cadence server image run: | From ecdcac65eb61dfbb74f53351280f30f07658703a Mon Sep 17 00:00:00 2001 From: Ryan Hodin Date: Wed, 31 Jul 2024 21:31:06 -0500 Subject: [PATCH 5/8] Give in and use ifs This feels unnecessarily verbose, but github apparently sets -e for these blocks, and working around that is harder and more verbose than this. --- .github/workflows/build-and-push.yml | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-and-push.yml b/.github/workflows/build-and-push.yml index 9bf02a3f..e84f319b 100644 --- a/.github/workflows/build-and-push.yml +++ b/.github/workflows/build-and-push.yml @@ -47,12 +47,25 @@ jobs: echo "tags=\"-t \$IMAGE:latest -t \$IMAGE:$(git rev-parse --short HEAD)\" $release" >> $GITHUB_ENV - name: Check push configuration run: | - [ "${{ github.event_name }}" = "push" ] && [ "${{ github.ref }}" = "refs/heads/main" ] - auto=$? - [ "${{ github.inputs.push }}" = "true" ] - manual=$? - [ "${{ github.event_name }}" = "release" ] - release=$? + auto=1 + manual=1 + release=1 + + if [ "${{ github.event_name }}" = "push" ] && [ "${{ github.ref }}" = "refs/heads/main" ] + then + auto=0 + fi + + if [ "${{ github.inputs.push }}" = "true" ] + then + manual=0 + fi + + if [ "${{ github.event_name }}" = "release" ] + then + release=0 + fi + push=`[[ $auto -eq 0 || $manual -eq 0 || $release -eq 0 ]] && echo 'true' || echo 'false'` echo "do_push=$push" >> $GITHUB_ENV - name: Build Cadence server image From 22b8931abe9c962d3a2a4cee3a764aa43b0a9368 Mon Sep 17 00:00:00 2001 From: Ryan Hodin Date: Wed, 31 Jul 2024 21:36:08 -0500 Subject: [PATCH 6/8] Move -t setting into each build step This is very annoying, but once again Github seems to make an ass of me by helpfully causing everything to parse in the exact way I want it not to. --- .github/workflows/build-and-push.yml | 29 +++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build-and-push.yml b/.github/workflows/build-and-push.yml index e84f319b..1ae23b60 100644 --- a/.github/workflows/build-and-push.yml +++ b/.github/workflows/build-and-push.yml @@ -30,9 +30,9 @@ jobs: declare -a tags for tag in ${{ github.inputs.tags }} do - tags+=("-t \$IMAGE:$tag") + tags+=("$tag") done - tags+=("-t \$IMAGE:$(git rev-parse --short HEAD)") + tags+=("$(git rev-parse --short HEAD)") echo "tags=\"${tags[@]}\"" >> $GITHUB_ENV - name: Prepare tag list (push trigger) if: github.event_name != 'workflow_dispatch' @@ -40,11 +40,11 @@ jobs: release=$(git tag --points-at HEAD) if $? then - release="-t \$IMAGE:$release" + release="$release" else release="" fi - echo "tags=\"-t \$IMAGE:latest -t \$IMAGE:$(git rev-parse --short HEAD)\" $release" >> $GITHUB_ENV + echo "tags=\"latest $(git rev-parse --short HEAD)\" $release" >> $GITHUB_ENV - name: Check push configuration run: | auto=1 @@ -71,15 +71,30 @@ jobs: - name: Build Cadence server image run: | IMAGE=kenellorando/cadence - docker build ${{ env.tags }} -f src/cadence.Dockerfile src + declare -a tags + for tag in "${{ env.tags }}" + do + tags+=("-t $IMAGE:$tag") + done + docker build ${tags[@]} -f src/cadence.Dockerfile src - name: Build icecast2 image run: | IMAGE=kenellorando/cadence_icecast2 - docker build ${{ env.tags }} -f src/icecast2.Dockerfile src + declare -a tags + for tag in "${{ env.tags }}" + do + tags+=("-t $IMAGE:$tag") + done + docker build ${tags[@]} -f src/icecast2.Dockerfile src - name: Build liquidsoap image run: | IMAGE=kenellorando/cadence_liquidsoap - docker build ${{ env.tags }} -f src/liquidsoap.Dockerfile src + declare -a tags + for tag in "${{ env.tags }}" + do + tags+=("-t $IMAGE:$tag") + done + docker build ${tags[@]} -f src/liquidsoap.Dockerfile src - name: Login to Docker Hub if: env.push == 'true' uses: docker/login-action@v3 From c7a6be7676b1576a3601c95e86c124eb2f024f8e Mon Sep 17 00:00:00 2001 From: Ryan Hodin Date: Wed, 31 Jul 2024 21:39:12 -0500 Subject: [PATCH 7/8] Rename variable Not sure why this is trying to add in extra stuff here... This commit fixes #252. --- .github/workflows/build-and-push.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build-and-push.yml b/.github/workflows/build-and-push.yml index 1ae23b60..f3c0c13e 100644 --- a/.github/workflows/build-and-push.yml +++ b/.github/workflows/build-and-push.yml @@ -71,30 +71,30 @@ jobs: - name: Build Cadence server image run: | IMAGE=kenellorando/cadence - declare -a tags + declare -a flags for tag in "${{ env.tags }}" do - tags+=("-t $IMAGE:$tag") + flags+=("-t $IMAGE:$tag") done - docker build ${tags[@]} -f src/cadence.Dockerfile src + docker build ${flags[@]} -f src/cadence.Dockerfile src - name: Build icecast2 image run: | IMAGE=kenellorando/cadence_icecast2 - declare -a tags + declare -a flags for tag in "${{ env.tags }}" do - tags+=("-t $IMAGE:$tag") + flags+=("-t $IMAGE:$tag") done - docker build ${tags[@]} -f src/icecast2.Dockerfile src + docker build ${flags[@]} -f src/icecast2.Dockerfile src - name: Build liquidsoap image run: | IMAGE=kenellorando/cadence_liquidsoap - declare -a tags + declare -a flags for tag in "${{ env.tags }}" do - tags+=("-t $IMAGE:$tag") + flags+=("-t $IMAGE:$tag") done - docker build ${tags[@]} -f src/liquidsoap.Dockerfile src + docker build ${flags[@]} -f src/liquidsoap.Dockerfile src - name: Login to Docker Hub if: env.push == 'true' uses: docker/login-action@v3 From 7e530a8a7a2ab7e95f16e2da6a384eef512e8321 Mon Sep 17 00:00:00 2001 From: Ryan Hodin Date: Wed, 31 Jul 2024 21:48:30 -0500 Subject: [PATCH 8/8] Remove dead code This is no longer needed because there is no longer an extra -t being added if there are no tags on release. It didn't work originally anyway if a commit had multiple tags... --- .github/workflows/build-and-push.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/build-and-push.yml b/.github/workflows/build-and-push.yml index f3c0c13e..6e9bdded 100644 --- a/.github/workflows/build-and-push.yml +++ b/.github/workflows/build-and-push.yml @@ -38,12 +38,6 @@ jobs: if: github.event_name != 'workflow_dispatch' run: | release=$(git tag --points-at HEAD) - if $? - then - release="$release" - else - release="" - fi echo "tags=\"latest $(git rev-parse --short HEAD)\" $release" >> $GITHUB_ENV - name: Check push configuration run: |