From 39fa2fe02db8368212aedb872572f79bcfb387d8 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 18 Sep 2021 01:17:19 -0500 Subject: [PATCH] ci: split docker build workflow from test workflow. Split up the Github workflow. Instead of one workflow with two jobs, one to build the Docker image and one to test it, split it into two separate workflows, one to build and one to test. This way if the Docker build fails it doesn't try to run the tests, and if the tests fail it only marks the test workflow as failed, not the entire workflow. This is especially so the workflows page doesn't show everything as failing just because the tests failed. https://github.com/danbooru/danbooru/actions --- .github/workflows/docker-build.yaml | 78 ++++++++++++++++++++++++++ .github/workflows/test.yaml | 86 ++++------------------------- 2 files changed, 90 insertions(+), 74 deletions(-) create mode 100644 .github/workflows/docker-build.yaml diff --git a/.github/workflows/docker-build.yaml b/.github/workflows/docker-build.yaml new file mode 100644 index 000000000..4644a2bdb --- /dev/null +++ b/.github/workflows/docker-build.yaml @@ -0,0 +1,78 @@ +# On every commit, build and push a Docker image to the GitHub Container +# registry and to DockerHub. +# +# The resulting Docker images are tagged with the full commit hash, the git +# branch name, the git tag, and the 'latest' tag for the latest commit to +# master. +# +# https://github.com/danbooru/danbooru/pkgs/container/danbooru +# https://hub.docker.com/r/evazion/danbooru +# +# https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry +# https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-actions +# https://docs.github.com/en/actions/guides/publishing-docker-images + +name: Docker Build + +# https://docs.github.com/en/actions/reference/events-that-trigger-workflows +on: [push, create] + +jobs: + docker-build: + runs-on: ubuntu-latest + + steps: + # https://github.com/docker/setup-buildx-action + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + # https://github.com/docker/login-action + # https://github.com/docker/build-push-action/blob/master/docs/advanced/push-multi-registries.md + - name: Login to Github Container Registry + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ github.token }} + + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + # Generate Docker tags from Git tags. + # https://github.com/docker/metadata-action + - name: Generate Docker tags + uses: docker/metadata-action@v3 + id: metadata + with: + images: | + docker.io/evazion/danbooru + ghcr.io/danbooru/danbooru + tags: | + type=sha,format=short,prefix= + type=sha,format=long,prefix= + type=ref,event=branch + type=ref,event=tag + type=ref,event=pr + # Tag `latest` on every commit pushed to master + # https://github.com/docker/metadata-action/issues/112 + flavor: | + latest=${{ github.ref == 'refs/heads/master' }} + + # https://github.com/docker/build-push-action + - name: Build image + uses: docker/build-push-action@v2 + with: + push: true + tags: ${{ steps.metadata.outputs.tags }} + labels: ${{ steps.metadata.outputs.labels }} + + # https://github.com/docker/build-push-action/blob/master/docs/advanced/cache.md#registry-cache + # https://dev.to/pst418/speed-up-multi-stage-docker-builds-in-ci-cd-with-buildkit-s-registry-cache-11gi + cache-from: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache + cache-to: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache,mode=max + + build-args: | + SOURCE_COMMIT=${{ github.sha }} diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 4bfff1fba..72aa78d71 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,88 +1,26 @@ -name: Github +# Run the test suite after the Docker image is successfully built. + +name: Test -# Trigger on pushes to master or pull requests to master, but not both. on: - push: - branches: - - master - pull_request: - branches: - - master + # https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_run + workflow_run: + workflows: ["Docker Build"] + types: [completed] + workflow_dispatch: inputs: debug_enabled: - description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)' + description: "Run the workflow with remote debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)" required: false default: false jobs: - # Build Docker image and push to Github Container Registry. - # - # https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry - # https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-actions - # https://docs.github.com/en/actions/guides/publishing-docker-images - docker: - runs-on: ubuntu-latest - - steps: - # https://github.com/docker/setup-buildx-action - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - # https://github.com/docker/login-action - # https://github.com/docker/build-push-action/blob/master/docs/advanced/push-multi-registries.md - - name: Login to Github Container Registry - uses: docker/login-action@v1 - with: - registry: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ github.token }} - - - name: Login to DockerHub - uses: docker/login-action@v1 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - # Generate Docker tags from Git tags. - # https://github.com/docker/metadata-action - - name: Generate Docker tags - uses: docker/metadata-action@v3 - id: metadata - with: - images: | - docker.io/evazion/danbooru - ghcr.io/danbooru/danbooru - tags: | - type=sha,format=short,prefix= - type=sha,format=long,prefix= - type=ref,event=branch - type=ref,event=tag - type=ref,event=pr - # Tag `latest` on every commit pushed to master - # https://github.com/docker/metadata-action/issues/112 - flavor: | - latest=${{ github.ref == 'refs/heads/master' }} - - # https://github.com/docker/build-push-action - - name: Build image - uses: docker/build-push-action@v2 - with: - push: true - tags: ${{ steps.metadata.outputs.tags }} - labels: ${{ steps.metadata.outputs.labels }} - - # https://github.com/docker/build-push-action/blob/master/docs/advanced/cache.md#registry-cache - # https://dev.to/pst418/speed-up-multi-stage-docker-builds-in-ci-cd-with-buildkit-s-registry-cache-11gi - cache-from: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache - cache-to: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache,mode=max - - build-args: | - SOURCE_COMMIT=${{ github.sha }} - test: + if: ${{ github.event.workflow_run.conclusion == 'success' }} + runs-on: ubuntu-latest - needs: docker + container: image: ghcr.io/danbooru/danbooru:${{ github.sha }} options: --user root