From 8eff8847eaea6b7c6208d85a5bb1d876e6939bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Tue, 12 Oct 2021 19:46:32 +0300 Subject: [PATCH] chore(docker): improve docker build workflow --- .github/workflows/cd.yml | 26 ----------- .github/workflows/ci.yml | 2 + .github/workflows/docker.yml | 85 ++++++++++++++++++++++++++++-------- 3 files changed, 69 insertions(+), 44 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index cbb8bec4..0eeb6ad7 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -64,29 +64,3 @@ jobs: with: command: publish args: --locked --token ${{ secrets.CARGO_TOKEN }} - - publish-docker: - name: Publish the Docker image - needs: publish-github - runs-on: ubuntu-20.04 - steps: - - name: Checkout - uses: actions/checkout@master - - name: Set the release version - run: echo "RELEASE_VERSION=${GITHUB_REF:11}" >> $GITHUB_ENV - - name: Build - run: docker build -t rustypaste . - - name: Tag - run: | - docker tag rustypaste orhunp/rustypaste:${{ env.RELEASE_VERSION }} - docker tag rustypaste docker.pkg.github.com/orhun/rustypaste/rustypaste:${{ env.RELEASE_VERSION }} - - name: Login (Docker Hub) - run: echo ${{ secrets.DOCKER_TOKEN }} | - docker login -u orhunp --password-stdin - - name: Push (Docker Hub) - run: docker push orhunp/rustypaste:${{ env.RELEASE_VERSION }} - - name: Login (Package Registry) - run: echo ${{ secrets.GITHUB_TOKEN }} | - docker login -u orhun docker.pkg.github.com --password-stdin - - name: Push (Package Registry) - run: docker push docker.pkg.github.com/orhun/rustypaste/rustypaste:${{ env.RELEASE_VERSION }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 90589011..5a4c45e7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,6 +7,8 @@ on: pull_request: branches: - master + schedule: + - cron: "0 0 * * 0" jobs: check: diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 972595ea..a732212b 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -4,27 +4,76 @@ on: push: branches: - master + tags: + - "v*.*.*" + pull_request: + branches: + - master + schedule: + - cron: "0 0 * * 0" jobs: docker: - name: Docker + name: Docker Build and Push runs-on: ubuntu-20.04 steps: - name: Checkout - uses: actions/checkout@master - - name: Build - run: docker build -t rustypaste . - - name: Tag - run: | - docker tag rustypaste orhunp/rustypaste:latest - docker tag rustypaste docker.pkg.github.com/orhun/rustypaste/rustypaste:latest - - name: Login (Docker Hub) - run: echo ${{ secrets.DOCKER_TOKEN }} | - docker login -u orhunp --password-stdin - - name: Push (Docker Hub) - run: docker push orhunp/rustypaste:latest - - name: Login (Package Registry) - run: echo ${{ secrets.GITHUB_TOKEN }} | - docker login -u orhun docker.pkg.github.com --password-stdin - - name: Push (Package Registry) - run: docker push docker.pkg.github.com/orhun/rustypaste/rustypaste:latest + uses: actions/checkout@v2 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v3 + with: + images: | + orhunp/rustypaste + ghcr.io/${{ github.repository_owner }}/rustypaste/rustypaste + tags: | + type=schedule + type=ref,event=branch + type=ref,event=pr + type=sha + type=raw,value=latest + type=semver,pattern={{version}} + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v1 + + - name: Cache Docker layers + uses: actions/cache@v2 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + + - name: Login to Docker Hub + if: github.event_name != 'pull_request' + uses: docker/login-action@v1 + with: + username: orhunp + password: ${{ secrets.DOCKER_TOKEN }} + + - name: Login to GHCR + if: github.event_name != 'pull_request' + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push + id: docker_build + uses: docker/build-push-action@v2 + with: + context: ./ + file: ./Dockerfile + builder: ${{ steps.buildx.outputs.name }} + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache + + - name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }}