Build Docker Images #355
This file contains hidden or 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 Images | |
on: | |
workflow_dispatch: | |
inputs: | |
tag: | |
description: 'tag for the Docker image' | |
required: true | |
default: 'latest' | |
push: | |
branches: | |
- 'main' | |
jobs: | |
define-matrix: | |
runs-on: ubuntu-latest | |
outputs: | |
tag: ${{ steps.set_tag.outputs.tag }} | |
tag_cn: ${{ steps.set_tag.outputs.tag_cn }} | |
build_targets: ${{ steps.get_build_matrix.outputs.build_targets }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Set up tag | |
id: set_tag | |
run: | | |
if [ -n "${{ inputs.tag }}" ]; then | |
tag=${{ inputs.tag }} | |
else | |
tag=${{ github.sha }}::6 | |
fi | |
tag_cn=$tag-cn | |
echo "tag=$tag" >> $GITHUB_OUTPUT | |
echo "tag_cn=$tag_cn" >> $GITHUB_OUTPUT | |
- name: Get build matrix | |
id: get_build_matrix | |
run: | | |
if [ -n "${{ inputs.tag }}" ]; then | |
build_targets=$(bash script/get_all_dockerfile.sh) | |
else | |
build_targets=$(bash script/get_changed_dockerfile.sh ${{ github.event.before }} ${{ github.sha }}) | |
fi | |
echo "build_targets=$build_targets" >> $GITHUB_OUTPUT | |
build: | |
runs-on: ubuntu-latest | |
needs: define-matrix | |
strategy: | |
fail-fast: false | |
matrix: | |
build_target: ${{ fromJson(needs.define-matrix.outputs.build_targets) }} | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to ghcr.io | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Login to Aliyun ACR | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ secrets.ALIYUN_REGISTRY }} | |
username: ${{ secrets.ALIYUN_USERNAME }} | |
password: ${{ secrets.ALIYUN_PASSWORD }} | |
- name: Build and push images | |
run: | | |
echo "build_target=${{ matrix.build_target }}" | |
echo "tag=${{ needs.define-matrix.outputs.tag }}" | |
ghcr_image_name=$(bash script/get_image_name.sh "ghcr.io" "${{ github.repository_owner }}" "${{ matrix.build_target }}" "${{ needs.define-matrix.outputs.tag }}" | tr '[:upper:]' '[:lower:]') | |
acr_image_name=$(bash script/get_image_name.sh "${{ secrets.ALIYUN_REGISTRY }}" "${{ secrets.ALIYUN_NAMESPACE }}" "${{ matrix.build_target }}" "${{ needs.define-matrix.outputs.tag }}" | tr '[:upper:]' '[:lower:]') | |
echo "ghcr_image_name=$ghcr_image_name" >> $GITHUB_OUTPUT | |
echo "acr_image_name=$acr_image_name" >> $GITHUB_OUTPUT | |
echo "Building and pushing both images..." | |
is_cn="0" bash script/build_and_push_images.sh "${{ matrix.build_target }}" "$ghcr_image_name" "$acr_image_name" $is_cn | |
- name: Build and push images_cn | |
run: | | |
echo "build_target=${{ matrix.build_target }}" | |
echo "tag_cn=${{ needs.define-matrix.outputs.tag_cn }}" | |
ghcr_image_name_cn=$(bash script/get_image_name.sh "ghcr.io" "${{ github.repository_owner }}" "${{ matrix.build_target }}" "${{ needs.define-matrix.outputs.tag_cn }}" | tr '[:upper:]' '[:lower:]') | |
acr_image_name_cn=$(bash script/get_image_name.sh "${{ secrets.ALIYUN_REGISTRY }}" "${{ secrets.ALIYUN_NAMESPACE }}" "${{ matrix.build_target }}" "${{ needs.define-matrix.outputs.tag_cn }}" | tr '[:upper:]' '[:lower:]') | |
echo "ghcr_image_name_cn=$ghcr_image_name_cn" >> $GITHUB_OUTPUT | |
echo "acr_image_name_cn=$acr_image_name_cn" >> $GITHUB_OUTPUT | |
echo "Building and pushing both images..." | |
is_cn="1" bash script/build_and_push_images.sh "${{ matrix.build_target }}" "$ghcr_image_name_cn" "$acr_image_name_cn" $is_cn | |
# TODO: generate runtime yaml and json |