From f570a180c7a071de249c87630ea8bf9dc8621bed Mon Sep 17 00:00:00 2001 From: Frederico Sabino <3332770+fmrsabino@users.noreply.github.com> Date: Tue, 16 Nov 2021 15:25:12 +0100 Subject: [PATCH] Deploy only tags (#145) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Trigger docker-deploy only for release events - Use docker/build-push-action to publish the docker image - Remove scripts/deploy_docker.sh – this script relied on builds from the branches develop and master. For tags, it would pull the latest staging image and tag it (which we don't want since we want to build the tag and deploy it) --- .github/workflows/python.yml | 55 +++++++++++++++++++++++------------- scripts/deploy_docker.sh | 18 ------------ 2 files changed, 36 insertions(+), 37 deletions(-) delete mode 100644 scripts/deploy_docker.sh diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 9c95d11..99c5de5 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -75,26 +75,43 @@ jobs: docker-deploy: runs-on: ubuntu-latest needs: test-app - if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/develop' || startsWith(github.ref, 'refs/tags/') + if: github.event_name == 'release' && github.event.action == 'released' steps: - - uses: actions/checkout@v2 - - name: Dockerhub login - uses: docker/login-action@v1 + - name: Checkout + uses: actions/checkout@v2.4.0 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1.6.0 + - name: Cache Docker layers + uses: actions/cache@v2.1.6 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + - name: Set tag (release) + # Set the tag versions as Docker tag if it is a release event + if: github.event_name == 'release' && github.event.action == 'released' + run: echo "DOCKER_TAG=${{ github.event.release.tag_name }}" >> $GITHUB_ENV + - name: Login to DockerHub + uses: docker/login-action@v1.10.0 with: username: ${{ secrets.DOCKER_USER }} password: ${{ secrets.DOCKER_PASSWORD }} - - name: Deploy Master - if: github.ref == 'refs/heads/master' - run: bash scripts/deploy_docker.sh staging - env: - DOCKERHUB_PROJECT: safe-notification-service - - name: Deploy Develop - if: github.ref == 'refs/heads/develop' - run: bash scripts/deploy_docker.sh develop - env: - DOCKERHUB_PROJECT: safe-notification-service - - name: Deploy Tag - if: startsWith(github.ref, 'refs/tags/') - run: bash scripts/deploy_docker.sh ${GITHUB_REF##*/} - env: - DOCKERHUB_PROJECT: safe-notification-service + - name: Build and push + id: docker_build + uses: docker/build-push-action@v2.7.0 + with: + context: . + push: true + tags: gnosispm/safe-notification-service:${{ env.DOCKER_TAG }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache-new + - # Temp fix + # https://github.com/docker/build-push-action/issues/252 + # https://github.com/moby/buildkit/issues/1896 + name: Move cache + run: | + rm -rf /tmp/.buildx-cache + mv /tmp/.buildx-cache-new /tmp/.buildx-cache + - name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} diff --git a/scripts/deploy_docker.sh b/scripts/deploy_docker.sh deleted file mode 100644 index eb4d57f..0000000 --- a/scripts/deploy_docker.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -set -euo pipefail - -export DOCKER_BUILDKIT=1 - -if [ "$1" = "develop" -o "$1" = "master" ]; then - # If image does not exist, don't use cache - docker pull gnosispm/$DOCKERHUB_PROJECT:$1 && \ - docker build -t $DOCKERHUB_PROJECT -f docker/web/Dockerfile . --cache-from gnosispm/$DOCKERHUB_PROJECT:$1 || \ - docker build -t $DOCKERHUB_PROJECT -f docker/web/Dockerfile . -else - docker pull gnosispm/$DOCKERHUB_PROJECT:staging && \ - docker build -t $DOCKERHUB_PROJECT -f docker/web/Dockerfile . --cache-from gnosispm/$DOCKERHUB_PROJECT:staging || \ - docker build -t $DOCKERHUB_PROJECT -f docker/web/Dockerfile . -fi -docker tag $DOCKERHUB_PROJECT gnosispm/$DOCKERHUB_PROJECT:$1 -docker push gnosispm/$DOCKERHUB_PROJECT:$1