From 56c5352df67a0809bc02d2d3deb8fd5c3e608fd0 Mon Sep 17 00:00:00 2001 From: Keith James Date: Wed, 19 Apr 2023 13:57:10 +0100 Subject: [PATCH] Make image deployment conditional on release Move image build and deployment into separate jobs. Make this job conditional on the build being a release. --- .github/workflows/create-release.yml | 88 ++++++++++++++++++++++ .github/workflows/push_container_image.yml | 41 ---------- 2 files changed, 88 insertions(+), 41 deletions(-) create mode 100644 .github/workflows/create-release.yml delete mode 100644 .github/workflows/push_container_image.yml diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml new file mode 100644 index 0000000..28d4328 --- /dev/null +++ b/.github/workflows/create-release.yml @@ -0,0 +1,88 @@ +name: "Create release" + +on: + push: + tags: + - "*" + +jobs: + release: + runs-on: ubuntu-latest + + defaults: + run: + shell: bash -l -e -o pipefail {0} + + steps: + - name: "Checkout code" + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: "Fetch Tags" + # Workaround for https://github.com/actions/checkout/issues/290 + run: | + # Avoid git exiting when Actions runs + git config --global --add safe.directory "$PWD" + + git fetch --tags --force + + - name: "Get release variables" + run: | + echo 'RELEASE_VERSION='$(git describe --always --tags) >> $GITHUB_ENV + echo 'MASTER_SHA='$(git rev-parse origin/master) >> $GITHUB_ENV + + - name: "Create Release" + uses: ncipollo/release-action@v1.12.0 + with: + name: ${{ env.RELEASE_VERSION }} + prerelease: ${{ !(github.sha == env.MASTER_SHA) }} + generateReleaseNotes: true + + outputs: + isRelease: ${{ github.sha == env.MASTER_SHA }} + + deploy: + runs-on: ubuntu-latest + + needs: release + + # Workaround for https://github.com/actions/runner/issues/1483 + # Actions coerces boolean to string + if: needs.release.outputs.isRelease == 'true' + + steps: + - name: "Checkout code" + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: "Fetch Tags" + # Workaround for https://github.com/actions/checkout/issues/290 + run: | + # Avoid git exiting when Actions runs + git config --global --add safe.directory "$PWD" + + git fetch --tags --force + + - name: "Set up Docker Buildx" + uses: docker/setup-buildx-action@v2 + with: + buildkitd-flags: --debug + + - name: "Login to ghcr.io" + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: "Build and push Docker images" + run: | + cd docker + make GITHUB_ACTIONS=${GITHUB_ACTIONS} GITHUB_REPOSITORY_OWNER=${{ github.repository_owner }} + + docker image prune --force + docker images + + make push GITHUB_ACTIONS=${GITHUB_ACTIONS} GITHUB_REPOSITORY_OWNER=${{ github.repository_owner }} diff --git a/.github/workflows/push_container_image.yml b/.github/workflows/push_container_image.yml deleted file mode 100644 index 0995cd2..0000000 --- a/.github/workflows/push_container_image.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: "Publish image" -on: - release: - types: [published] - -jobs: - push_to_registries: - name: "Push image to multiple registries" - runs-on: ubuntu-latest - steps: - - name: "Checkout code" - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - - name: "Force fetch annotated tags (workaround)" - # Workaround for https://github.com/actions/checkout/issues/290 - run: | - git fetch --force --tags - - - name: "Set up Docker Buildx" - uses: docker/setup-buildx-action@v2 - with: - buildkitd-flags: --debug - - - name: "Login to ghcr.io" - uses: docker/login-action@v2 - with: - registry: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: "Build and push Docker images" - run: | - cd docker - make GITHUB_ACTIONS=${GITHUB_ACTIONS} GITHUB_REPOSITORY_OWNER=${{ github.repository_owner }} - - docker image prune --force - docker images - - make push GITHUB_ACTIONS=${GITHUB_ACTIONS} GITHUB_REPOSITORY_OWNER=${{ github.repository_owner }}