♻️,🏋 Build an image and upload to ghcr #13
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 an image and upload to ghcr" | |
on: | |
workflow_call: | |
inputs: | |
version: | |
required: true | |
type: string | |
description: 'clams-python SDK version' | |
buildfilename: | |
required: true | |
type: string | |
description: 'name of the container build file to use' | |
workflow_dispatch: | |
inputs: | |
version: | |
required: true | |
type: string | |
description: 'clams-python SDK version' | |
buildfilename: | |
required: true | |
type: string | |
description: 'name of the container build file to use' | |
env: | |
REGISTRY: ghcr.io | |
jobs: | |
build-and-push-image: | |
name: "🐳 Build and push image" | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: "🛍️ Checkout repository" | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ inputs.version }} | |
fetch-depth: 0 | |
- name: "🎛 Set up QEMU" | |
uses: docker/setup-qemu-action@v3 | |
- name: "👷 Set up Docker Buildx" | |
id: buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: "🏷 Prepare OCI annotations" | |
id: getlabels | |
run: | | |
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) | |
echo "EXISTING_LABELS<<$EOF" >> $GITHUB_ENV | |
cat ${{ inputs.buildfilename }} | grep LABEL | sed -E 's/^LABEL\s+([^=]+)="?(.+)("|$)/\1=\2/g' >> $GITHUB_ENV | |
echo "$EOF" >> $GITHUB_ENV | |
echo $EXISTING_LABELS | |
- name: "🏷 Get image build context" | |
id: getcontext | |
run: | | |
echo "CONTEXT=$(dirname ${{ inputs.buildfilename }})" >> $GITHUB_ENV | |
- name: "🏷 Get image name suffix" | |
id: getsuffix | |
run: | | |
export filename=$(basename ${{ inputs.buildfilename }}) | |
export nameonly="${filename%.*}" | |
if [ ${nameonly} == ${filename} ]; then echo "SUFFIX=" >> $GITHUB_ENV ; else echo "SUFFIX=-${nameonly}" >> $GITHUB_ENV; fi | |
- name: "get SDK version from latest tag" | |
id: getversion | |
run: | | |
export version=$(git describe --tags --abbrev=0) | |
echo "CLAMS_VERSION=${version}" >> $GITHUB_OUTPUT | |
- name: "🏷 Prepare docker tags, labels" | |
id: meta | |
uses: docker/metadata-action@v5 | |
env: | |
CLAMS_VERSION: ${{ steps.getversion.outputs.CLAMS_VERSION }} | |
with: | |
images: ${{ env.REGISTRY }}/${{ github.repository }}${{ env.SUFFIX }} | |
tags: | | |
type=pep440,pattern={{version}},value=${{ env.CLAMS_VERSION }} | |
type=ref,event=tag | |
type=ref,event=pr | |
labels: | | |
${{ env.EXISTING_LABELS }} | |
- name: "🔏 Log in to registry" | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: "🏗 Build and push image" | |
uses: docker/build-push-action@v5 | |
env: | |
CLAMS_VERSION: ${{ steps.getversion.outputs.CLAMS_VERSION }} | |
with: | |
context: ${{ env.CONTEXT }} | |
platforms: linux/amd64,linux/arm64 | |
file: ${{ inputs.buildfilename }} | |
tags: ${{ steps.meta.outputs.tags }} | |
# using {{ steps.meta.outputs.labels }} doesn't work with multi-line variable ($EXISTING_LABLES) | |
labels: ${{ env.DOCKER_METADATA_OUTPUT_LABELS }} | |
build-args: | | |
clams_version=$CLAMS_VERSION | |
push: true | |