CD (Delivery) #19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CD (Delivery) | |
on: | |
workflow_dispatch: | |
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#workflow_run | |
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#limiting-your-workflow-to-run-based-on-branches | |
# This workflow with the following trigger will only run when | |
# - the workflow named `Node.js CI Workflow` completed on the `main` branch. | |
workflow_run: | |
workflows: | |
- CI (lint, test, build) | |
types: | |
- completed | |
branches: | |
- main | |
jobs: | |
on-success: | |
name: Continuous Delivery (CD) | |
# https://stackoverflow.com/questions/67531606/how-to-deal-with-long-conditional-expression-in-github-actions-workflow | |
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#running-a-workflow-based-on-the-conclusion-of-another-workflow | |
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }} | |
runs-on: ubuntu-22.04 | |
# https://docs.docker.com/build/ci/github-actions/multi-platform/ | |
# https://github.com/marketplace/actions/build-and-push-docker-images#path-context | |
steps: | |
- name: Check out code | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # https://github.com/actions/checkout/releases/tag/v4.2.2 | |
# https://github.com/marketplace/actions/docker-setup-buildx | |
# Add support for more platforms with QEMU (optional) | |
# https://github.com/docker/setup-qemu-action | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@4574d27a4764455b42196d70a065bc6853246a25 # https://github.com/docker/setup-qemu-action/releases/tag/v3.4.0 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@f7ce87c1d6bead3e36075b2ce75da1f6cc28aaca # https://github.com/docker/setup-buildx-action/releases/tag/v3.9.0 | |
# https://github.com/marketplace/actions/docker-login | |
- name: Log in to Docker Hub | |
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # https://github.com/docker/login-action/releases/tag/v3.3.0 | |
with: | |
username: ${{ vars.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Set IMAGE_NAME and IMAGE_VERSION | |
run: | | |
echo "IMAGE_NAME=$(node -p 'require("./package.json").name')" >> ${GITHUB_ENV} | |
echo "IMAGE_VERSION=$(node -p 'require("./package.json").version')" >> ${GITHUB_ENV} | |
# https://docs.docker.com/reference/cli/docker/buildx/build/ | |
# This is equivalent to the docker action below. | |
# - name: Build and Push Multi-Arch Docker Image By Docker CLI | |
# env: | |
# DOCKERHUB_IMAGE: ${{ vars.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }} | |
# run: | | |
# docker buildx build \ | |
# --platform linux/amd64,linux/arm64 \ | |
# --tag ${{ env.DOCKERHUB_IMAGE }}:latest \ | |
# --tag ${{ env.DOCKERHUB_IMAGE }}:${{ env.IMAGE_VERSION }} \ | |
# --cache-from=type=registry,ref=${{ env.DOCKERHUB_IMAGE }}:build-cache \ | |
# --cache-from=type=registry,ref=${{ env.DOCKERHUB_IMAGE }}:latest \ | |
# --cache-to=type=registry,ref=${{ env.DOCKERHUB_IMAGE }}:build-cache,mode=max \ | |
# --cache-to=type=inline \ | |
# --push \ | |
# . | |
# https://github.com/marketplace/actions/build-and-push-docker-images | |
- name: Build and Push Multi-Arch Docker Image By docker action | |
uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 # https://github.com/docker/build-push-action/releases/tag/v6.13.0 | |
env: | |
DOCKERHUB_IMAGE: ${{ vars.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }} | |
with: | |
context: . | |
push: true | |
platforms: linux/amd64,linux/arm64 | |
tags: | | |
${{ env.DOCKERHUB_IMAGE }}:latest | |
${{ env.DOCKERHUB_IMAGE }}:${{ env.IMAGE_VERSION }} | |
# https://docs.docker.com/build/ci/github-actions/cache/ | |
# https://docs.docker.com/reference/cli/docker/buildx/build/#cache-from | |
# https://docs.docker.com/reference/cli/docker/buildx/build/#cache-to | |
cache-from: | | |
type=registry,ref=${{ env.DOCKERHUB_IMAGE }}:build-cache | |
type=registry,ref=${{ env.DOCKERHUB_IMAGE }}:latest | |
cache-to: | | |
type=registry,ref=${{ env.DOCKERHUB_IMAGE }}:build-cache,mode=max | |
type=inline | |
on-failure: | |
name: Notify on Dependent Workflow Failure | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'failure' }} | |
steps: | |
- run: echo "Due to the failure of the dependent workflow, this workflow will not run." |