Github Workflow Optimizations #2
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 and Push Docker Image | |
on: | |
push: | |
branches: | |
- main | |
tags: | |
- 'v*' | |
pull_request: | |
branches: | |
- main | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository_owner }}/halberd | |
jobs: | |
prepare: | |
runs-on: ubuntu-latest | |
outputs: | |
version: ${{ steps.version.outputs.version }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
json: ${{ steps.meta.outputs.json }} | |
requirements-hash: ${{ steps.requirements.outputs.hash }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.11' | |
- name: Get version | |
id: version | |
run: | | |
VERSION=$(python3 -c "from version import __version__; print(__version__)") | |
echo "version=${VERSION}" >> "$GITHUB_OUTPUT" | |
echo "Extracted version: ${VERSION}" | |
- name: Generate requirements hash | |
id: requirements | |
run: | | |
HASH=$(sha256sum requirements.txt | cut -d' ' -f1) | |
echo "hash=${HASH}" >> "$GITHUB_OUTPUT" | |
echo "Requirements hash: ${HASH}" | |
- name: Extract metadata | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
tags: | | |
type=ref,event=branch | |
type=ref,event=pr | |
type=semver,pattern={{version}} | |
type=semver,pattern={{major}}.{{minor}} | |
type=semver,pattern={{major}} | |
type=raw,value=latest,enable={{is_default_branch}} | |
type=raw,value=v${{ steps.version.outputs.version }} | |
build-amd64: | |
runs-on: ubuntu-latest | |
needs: prepare | |
permissions: | |
contents: read | |
packages: write | |
id-token: write | |
outputs: | |
digest: ${{ steps.build.outputs.digest }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to Container Registry | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push AMD64 | |
id: build | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
file: ./Dockerfile | |
platforms: linux/amd64 | |
push: ${{ github.event_name != 'pull_request' }} | |
labels: | | |
${{ needs.prepare.outputs.labels }} | |
org.opencontainers.image.version=${{ needs.prepare.outputs.version }} | |
build-args: | | |
HALBERD_VERSION=${{ needs.prepare.outputs.version }} | |
cache-from: | | |
type=gha,scope=linux/amd64 | |
type=gha,scope=wheels-${{ needs.prepare.outputs.requirements-hash }} | |
cache-to: | | |
type=gha,mode=max,scope=linux/amd64 | |
type=gha,mode=max,scope=wheels-${{ needs.prepare.outputs.requirements-hash }} | |
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }} | |
build-arm64: | |
runs-on: ubuntu-24.04-arm | |
needs: prepare | |
permissions: | |
contents: read | |
packages: write | |
id-token: write | |
outputs: | |
digest: ${{ steps.build.outputs.digest }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to Container Registry | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push ARM64 | |
id: build | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
file: ./Dockerfile | |
platforms: linux/arm64 | |
push: ${{ github.event_name != 'pull_request' }} | |
labels: | | |
${{ needs.prepare.outputs.labels }} | |
org.opencontainers.image.version=${{ needs.prepare.outputs.version }} | |
build-args: | | |
HALBERD_VERSION=${{ needs.prepare.outputs.version }} | |
cache-from: | | |
type=gha,scope=linux/arm64 | |
type=gha,scope=wheels-${{ needs.prepare.outputs.requirements-hash }} | |
cache-to: | | |
type=gha,mode=max,scope=linux/arm64 | |
type=gha,mode=max,scope=wheels-${{ needs.prepare.outputs.requirements-hash }} | |
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }} | |
merge: | |
runs-on: ubuntu-latest | |
if: github.event_name != 'pull_request' | |
needs: [prepare, build-amd64, build-arm64] | |
permissions: | |
contents: read | |
packages: write | |
id-token: write | |
attestations: write | |
steps: | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Create manifest list and push | |
id: manifest | |
run: | | |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ | |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.build-amd64.outputs.digest }} \ | |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.build-arm64.outputs.digest }} | |
MANIFEST_DIGEST=$(docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:v${{ needs.prepare.outputs.version }} --format '{{json .Manifest}}' | jq -r '.digest') | |
echo "digest=${MANIFEST_DIGEST}" >> $GITHUB_OUTPUT | |
echo "Manifest list digest: ${MANIFEST_DIGEST}" | |
env: | |
DOCKER_METADATA_OUTPUT_JSON: ${{ needs.prepare.outputs.json }} | |
- name: Generate build provenance attestation | |
uses: actions/attest-build-provenance@v1 | |
with: | |
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
subject-digest: ${{ steps.manifest.outputs.digest }} | |
push-to-registry: true |