Skip to content

Auto Build Image Base #113

Auto Build Image Base

Auto Build Image Base #113

name: Auto Build Image Base
# final packed image : ${ONLINE_REGISTER}/${IMAGE_REPO}/${IMAGE_NAME}:${commit id of yaml path}
# image dockerfile path on the repo: ${IMAGE_ROOT_PATH}/${IMAGE_NAME}/Dockerfile
env:
IMAGE_NAME: spiderpool-base
IMAGE_REPO: ${{ github.repository }}
ONLINE_REGISTER: ghcr.io
IMAGE_ROOT_PATH: images
BUILD_PLATFORM: linux/amd64,linux/arm64
ONLINE_REGISTER_USER: ${{ github.actor }}
ONLINE_REGISTER_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
# when PR commit the change for baseimage dockerfile, trigger the workflow
on:
pull_request_target:
types:
- opened
- synchronize
- reopened
paths:
# can not use env here
- images/spiderpool-base/**
workflow_dispatch:
inputs:
ref:
description: 'sha, Tag'
required: true
default: v1.0.0
permissions: write-all
# concurrency:
# group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
# cancel-in-progress: true
jobs:
build-and-push:
timeout-minutes: 30
environment: release-base-images
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.0.0
- name: Inspect builder
run: |
echo "Name: ${{ steps.buildx.outputs.name }}"
echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}"
echo "Status: ${{ steps.buildx.outputs.status }}"
echo "Flags: ${{ steps.buildx.outputs.flags }}"
echo "Platforms: ${{ steps.buildx.outputs.platforms }}"
- name: Set up QEMU
id: qemu
uses: docker/setup-qemu-action@v3.0.0
- name: Get Code Version Before Checkout
id: get_event_version
continue-on-error: false
run: |
if ${{ github.event_name == 'workflow_dispatch' }}; then
ref=${{ github.event.inputs.ref }}
echo "use re $ref , by workflow_dispatch"
echo "event_ref=${ref}" >> $GITHUB_ENV
else
#trigger by pr
echo "use sha ${{ github.event.pull_request.head.sha }} , by pr"
echo "event_ref=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
fi
- name: Checkout Source Code
uses: actions/checkout@v4
with:
persist-credentials: false
# fetch-depth: 0
ref: ${{ env.event_ref }}
# after checkout code , could get the commit id of path ./images/baseimage , used for base image tag
- name: Generating Base Image Tag
id: base_tag
run: |
echo "tag=$(git ls-tree --full-tree HEAD -- ./${{ env.IMAGE_ROOT_PATH }}/${{ env.IMAGE_NAME }} | awk '{ print $3 }')" >> $GITHUB_ENV
# check whether we have upload the same base image to online register , if so, we could not build it
- name: Checking if tag already exists
id: tag-in-repositories
shell: bash
run: |
if docker buildx imagetools inspect ${{ env.ONLINE_REGISTER }}/${{ env.IMAGE_REPO }}/${{ env.IMAGE_NAME }}:${{ env.tag }} &>/dev/null; then
echo "exists=true" >> $GITHUB_ENV
echo "the target base image exist , no need to build it "
else
echo "exists=false" >> $GITHUB_ENV
echo "the target base image does not exist , build it "
fi
- name: Login to online register
if: ${{ env.exists == 'false' }}
uses: docker/login-action@v3.0.0
with:
username: ${{ env.ONLINE_REGISTER_USER }}
password: ${{ env.ONLINE_REGISTER_PASSWORD }}
registry: ${{ env.ONLINE_REGISTER }}
- name: Release build ${{ env.IMAGE_NAME }}
if: ${{ env.exists == 'false' }}
uses: docker/build-push-action@v5.1.0
continue-on-error: false
id: docker_build_release
with:
context: ./${{ env.IMAGE_ROOT_PATH }}/${{ env.IMAGE_NAME }}
file: ./${{ env.IMAGE_ROOT_PATH }}/${{ env.IMAGE_NAME }}/Dockerfile
push: true
provenance: false
github-token: ${{ secrets.WELAN_PAT }}
platforms: ${{ env.BUILD_PLATFORM }}
tags: |
${{ env.ONLINE_REGISTER }}/${{ env.IMAGE_REPO }}/${{ env.IMAGE_NAME }}:${{ env.tag }}
- name: Image Release Digest
if: ${{ env.exists == 'false' }}
shell: bash
run: |
mkdir -p image-digest/
echo "## ${{ env.IMAGE_NAME }}" > image-digest/${{ env.IMAGE_NAME }}.txt
echo "" >> image-digest/${{ env.IMAGE_NAME }}.txt
echo "\`${{ env.ONLINE_REGISTER }}/${{ env.IMAGE_REPO }}/${{ env.IMAGE_NAME }}:${{ env.tag }}@${{ steps.docker_build_release.outputs.digest }}\`" >> image-digest/${{ env.IMAGE_NAME }}.txt
echo "" >> image-digest/${{ env.IMAGE_NAME }}.txt
- name: Upload artifact digests
if: ${{ env.exists == 'false' }}
uses: actions/upload-artifact@v3.1.3
with:
name: image-digest ${{ env.IMAGE_NAME }}
path: image-digest
retention-days: 1
image-digests:
name: Display Digests
runs-on: ubuntu-latest
needs: build-and-push
steps:
- name: Downloading Image Digests
shell: bash
run: |
mkdir -p image-digest/
- name: Download digests of all images built
uses: actions/download-artifact@v3
with:
path: image-digest/
- name: Image Digests Output
shell: bash
run: |
cd image-digest/
find -type f | sort | xargs -d '\n' cat