Build and push images #82
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 and push images | |
on: | |
workflow_dispatch: | |
inputs: | |
REGISTRY: | |
description: Target registry to push images | |
required: true | |
type: string | |
default: ghcr.io | |
NAMESPACE: | |
description: Target namespace to the given registry | |
required: true | |
type: string | |
default: this-is-tobi/tools | |
PLATFORMS: | |
description: Target platforms to build images | |
required: true | |
type: string | |
default: linux/amd64,linux/arm64 | |
env: | |
REGISTRY: ${{ inputs.REGISTRY }} | |
NAMESPACE: ${{ inputs.NAMESPACE }} | |
PLATFORMS: ${{ inputs.PLATFORMS }} | |
jobs: | |
matrix: | |
name: Generate matrix for build | |
runs-on: ubuntu-latest | |
outputs: | |
build-matrix: ${{ steps.build-matrix.outputs.BUILD_MATRIX }} | |
steps: | |
- name: Checks-out repository | |
uses: actions/checkout@v4 | |
- name: Generate matrix for build | |
id: build-matrix | |
run: | | |
echo "BUILD_MATRIX=$(jq -c . < ./ci/matrix.json)" >> $GITHUB_OUTPUT | |
build: | |
name: Build images | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
needs: | |
- matrix | |
strategy: | |
matrix: | |
images: ${{ fromJSON(needs.matrix.outputs.build-matrix) }} | |
steps: | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ env.REGISTRY == 'ghcr.io' && github.actor || secrets.REGISTRY_USERNAME }} | |
password: ${{ env.REGISTRY == 'ghcr.io' && secrets.GITHUB_TOKEN || secrets.REGISTRY_PASSWORD }} | |
logout: true | |
- name: Get image status | |
id: image-status | |
run: | | |
IMAGE_STATUS=$(curl \ | |
--head \ | |
--silent \ | |
--write-out '%{http_code}' \ | |
--output /dev/null \ | |
-H "Authorization: Bearer $(echo -n '${{ secrets.GITHUB_TOKEN }}' | base64)" \ | |
https://${{ env.REGISTRY }}/v2/${{ env.NAMESPACE }}/${{ matrix.images.name }}/manifests/${{ matrix.images.build.tag }}) | |
echo "IMAGE_STATUS=$IMAGE_STATUS" >> $GITHUB_OUTPUT | |
- name: Get image tags | |
id: image-tags | |
run: | | |
MAJOR_VERSION="$(echo '${{ matrix.images.build.tag }}' | cut -d '.' -f 1)" | |
MINOR_VERSION="$(echo '${{ matrix.images.build.tag }}' | cut -d '.' -f 2)" | |
IMAGE_TAGS="${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ matrix.images.name}}:${{ matrix.images.build.tag }},${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ matrix.images.name}}:$MAJOR_VERSION.$MINOR_VERSION,${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ matrix.images.name}}:$MAJOR_VERSION" | |
if [[ "${{ matrix.images.build.latest}}" == "true" ]]; then | |
IMAGE_TAGS="$IMAGE_TAGS,${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ matrix.images.name}}:latest" | |
fi | |
echo "IMAGE_TAGS=$IMAGE_TAGS" >> $GITHUB_OUTPUT | |
- name: Checks-out repository | |
uses: actions/checkout@v4 | |
if: ${{ steps.image-status.outputs.IMAGE_STATUS == '404' }} | |
- name: Set up Docker buildx | |
uses: docker/setup-buildx-action@v3 | |
if: ${{ steps.image-status.outputs.IMAGE_STATUS == '404' }} | |
- name: Set up QEMU (for multi platform build) | |
uses: docker/setup-qemu-action@v3 | |
if: ${{ steps.image-status.outputs.IMAGE_STATUS == '404' }} | |
- name: Build docker image | |
uses: docker/build-push-action@v6 | |
if: ${{ steps.image-status.outputs.IMAGE_STATUS == '404' }} | |
with: | |
context: ${{ matrix.images.build.context }} | |
file: ${{ matrix.images.build.dockerfile }} | |
tags: ${{ steps.image-tags.outputs.IMAGE_TAGS }} | |
target: ${{ matrix.images.build.target }} | |
platforms: ${{ env.PLATFORMS }} | |
push: true | |
provenance: false | |
build-args: | | |
BASE_IMAGE=${{ matrix.images.build.base }} |