Build Docker Image and Publish (v2.7.2) (🧪 dry-run) #21
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: Build Docker | |
run-name: Build Docker Image and Publish (${{ github.event.client_payload.version }}) ${{ github.event.client_payload.dry_run == 'false' && '' || '(🧪 dry-run)' }} | |
on: | |
repository_dispatch: | |
types: [artalk-release] | |
env: | |
DOCKER_IMG: artalk/artalk-go | |
VERSION: ${{ github.event.client_payload.version }} | |
DRY_RUN: ${{ github.event.client_payload.dry_run }} | |
jobs: | |
build_publish: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ env.VERSION }} | |
fetch-depth: 0 | |
- name: Get commit hash | |
run: | | |
echo "COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV | |
echo "COMMIT_HASH_FULL=$(git rev-parse HEAD)" >> $GITHUB_ENV | |
# https://github.com/docker/login-action | |
- name: Login to DockerHub | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
# https://github.com/docker/setup-qemu-action | |
- name: Setup QEMU | |
id: qemu | |
uses: docker/setup-qemu-action@v3 | |
with: | |
platforms: 'amd64,arm64' | |
# https://github.com/docker/setup-buildx-action | |
- name: Setup Docker Buildx | |
id: buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Cache Docker layers | |
uses: actions/cache@v3 | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx-${{ github.sha }} | |
restore-keys: | | |
${{ runner.os }}-buildx- | |
# https://github.com/docker/metadata-action | |
- name: Gen docker meta | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
context: git | |
images: | | |
${{ env.DOCKER_IMG }} | |
tags: | | |
type=ref,event=branch | |
type=ref,event=pr | |
type=semver,pattern={{version}} | |
type=semver,pattern={{major}}.{{minor}} | |
type=sha | |
# https://github.com/docker/build-push-action | |
- name: Build and Push | |
id: docker_build | |
uses: docker/build-push-action@v5 | |
with: | |
builder: ${{ steps.buildx.outputs.name }} | |
context: ./ | |
file: ./Dockerfile | |
platforms: 'linux/amd64,linux/arm64' | |
push: ${{ env.DRY_RUN == 'false' }} | |
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: Comment build info in commit | |
uses: actions/github-script@v7 | |
env: | |
STDOUT: "🐳 Published new docker image: [${{ env.DOCKER_IMG }}](https://hub.docker.com/r/${{ env.DOCKER_IMG }}) (${{ env.VERSION }} / sha-${{ env.COMMIT_HASH }})" | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const comment = { | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
commit_sha: process.env.COMMIT_HASH_FULL, // context.sha | |
body: process.env.STDOUT | |
} | |
if (process.env.DRY_RUN == 'false') | |
github.rest.repos.createCommitComment(comment) | |
else console.log(comment) | |
- name: Print image digest | |
run: echo ${{ steps.docker_build.outputs.digest }} |