Build and Push Multi-Arch Docker Image #113
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 Backend and Push Docker Image | |
on: | |
workflow_dispatch: | |
release: | |
types: [ released ] | |
pull_request: | |
branches: | |
- master | |
types: [ closed ] | |
paths: | |
- '**.py' | |
- 'requirements.txt' | |
- '!./web/**' | |
jobs: | |
build_amd64: | |
runs-on: ubuntu-latest | |
env: | |
DOCKERHUB_USERNAME: rikasai | |
DOCKER_REPOSITORY_NAME: fast-runner-backend | |
ARCH: amd64 | |
steps: | |
- name: Check out the repository | |
uses: actions/checkout@v2 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v1 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Get Git tag if exists | |
id: get_tag | |
run: | | |
GIT_TAG=$(git describe --tags --exact-match ${{ github.sha }} 2> /dev/null || echo "") | |
echo "GIT_TAG=$GIT_TAG" >> $GITHUB_ENV | |
- name: Build AMD64 Image | |
uses: docker/build-push-action@v2 | |
with: | |
context: . | |
file: ./Dockerfile | |
platforms: linux/amd64 | |
push: true | |
tags: | | |
${{ env.DOCKERHUB_USERNAME }}/${{ env.DOCKER_REPOSITORY_NAME }}:latest-amd64 | |
${{ env.GIT_TAG != '' && format('{0}/{1}:{2}-amd64', env.DOCKERHUB_USERNAME, env.DOCKER_REPOSITORY_NAME, env.GIT_TAG) || '' }} | |
build_arm64: | |
runs-on: ubuntu-24.04-arm | |
env: | |
DOCKERHUB_USERNAME: rikasai | |
DOCKER_REPOSITORY_NAME: fast-runner-backend | |
ARCH: arm64 | |
steps: | |
- name: Check out the repository | |
uses: actions/checkout@v2 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v1 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Get Git tag if exists | |
id: get_tag | |
run: | | |
GIT_TAG=$(git describe --tags --exact-match ${{ github.sha }} 2> /dev/null || echo "") | |
echo "GIT_TAG=$GIT_TAG" >> $GITHUB_ENV | |
- name: Build ARM64 Image | |
uses: docker/build-push-action@v2 | |
with: | |
context: . | |
file: ./Dockerfile | |
platforms: linux/arm64 | |
push: true | |
tags: | | |
${{ env.DOCKERHUB_USERNAME }}/${{ env.DOCKER_REPOSITORY_NAME }}:latest-arm64 | |
${{ env.GIT_TAG != '' && format('{0}/{1}:{2}-arm64', env.DOCKERHUB_USERNAME, env.DOCKER_REPOSITORY_NAME, env.GIT_TAG) || '' }} | |
create_manifest: | |
needs: [build_amd64, build_arm64] | |
runs-on: ubuntu-latest | |
env: | |
DOCKERHUB_USERNAME: rikasai | |
DOCKER_REPOSITORY_NAME: fast-runner-backend | |
steps: | |
- name: Check out the repository | |
uses: actions/checkout@v2 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Get Git tag if exists | |
id: get_tag | |
run: | | |
GIT_TAG=$(git describe --tags --exact-match ${{ github.sha }} 2> /dev/null || echo "") | |
echo "GIT_TAG=$GIT_TAG" >> $GITHUB_ENV | |
- name: Create and push multi-arch manifests | |
run: | | |
# 启用 Docker CLI 实验性功能 | |
export DOCKER_CLI_EXPERIMENTAL=enabled | |
# 创建并推送 latest manifest | |
docker manifest rm ${{ env.DOCKERHUB_USERNAME }}/${{ env.DOCKER_REPOSITORY_NAME }}:latest || true | |
docker manifest create ${{ env.DOCKERHUB_USERNAME }}/${{ env.DOCKER_REPOSITORY_NAME }}:latest \ | |
--amend ${{ env.DOCKERHUB_USERNAME }}/${{ env.DOCKER_REPOSITORY_NAME }}:latest-amd64 \ | |
--amend ${{ env.DOCKERHUB_USERNAME }}/${{ env.DOCKER_REPOSITORY_NAME }}:latest-arm64 | |
docker manifest push ${{ env.DOCKERHUB_USERNAME }}/${{ env.DOCKER_REPOSITORY_NAME }}:latest | |
# 如果存在 tag,创建并推送 tag manifest | |
if [ ! -z "${{ env.GIT_TAG }}" ]; then | |
docker manifest rm ${{ env.DOCKERHUB_USERNAME }}/${{ env.DOCKER_REPOSITORY_NAME }}:${{ env.GIT_TAG }} || true | |
docker manifest create ${{ env.DOCKERHUB_USERNAME }}/${{ env.DOCKER_REPOSITORY_NAME }}:${{ env.GIT_TAG }} \ | |
--amend ${{ env.DOCKERHUB_USERNAME }}/${{ env.DOCKER_REPOSITORY_NAME }}:${{ env.GIT_TAG }}-amd64 \ | |
--amend ${{ env.DOCKERHUB_USERNAME }}/${{ env.DOCKER_REPOSITORY_NAME }}:${{ env.GIT_TAG }}-arm64 | |
docker manifest push ${{ env.DOCKERHUB_USERNAME }}/${{ env.DOCKER_REPOSITORY_NAME }}:${{ env.GIT_TAG }} | |
fi |