Skip to content

Build OCI Image

Build OCI Image #8

Workflow file for this run

name: Build
run-name: Build OCI Image
on:
push:
branches: ['main']
tags: ['v*']
env:
GH_REGISTRY: ghcr.io # Github Container Registry
FULL_IMAGE_NAME: ${{ github.repository }} # full image name: owner/image
jobs:
build-oci-image:
# https://github.com/actions/runner-images
runs-on: ubuntu-latest
# https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs
permissions:
contents: read # for actions/checkout
packages: write # for OCI build
steps:
# debug
- run: echo "πŸŽ‰ The job was automatically triggered by a ${{ github.event_name }} event from ${{ github.actor }}."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "πŸ”Ž The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
# checkout
- name: Check out repository code
uses: actions/checkout@v4
with:
fetch-depth: 0 # all history for tags and branches, needed to resolve tag->branch name
# https://docs.docker.com/build/ci/github-actions/multi-platform/
# QEMU for software emulation of multiple platforms
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
# Docker buildx/buildkit for multi-platform builds
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# debug
- run: echo "πŸ’‘ The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "πŸ–₯️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."
# debug github context and env
- name: Dump env
run: env | sort
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"
- name: calculate branch name IF tag
if: github.ref_type == 'tag' # 'branch' if regular push
run: |
echo "GITHUB_REF_NAME = $GITHUB_REF_NAME"
echo "GITHUB_SHA = $GITHUB_SHA"
short_sha=`git rev-parse --short $GITHUB_SHA`
echo "short SHA = $short_sha"
echo candidates for branch name
git for-each-ref | grep ^$GITHUB_SHA | grep origin | grep -v HEAD
branch_name=`git for-each-ref | grep ^$GITHUB_SHA | grep origin | grep -v HEAD | head -n1 | sed "s/.*\///"`
echo tag $GITHUB_REF_NAME is on branch $branch_name
# Github container registry credentials
- name: Log in to the Github Container registry ${{ env.GH_REGISTRY }} as ${{ github.actor }}
uses: docker/login-action@v2
with:
registry: ${{ env.GH_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# tags and labels
- name: Extract metadata (tags, labels) for image ${{ env.FULL_IMAGE_NAME }}
id: meta
uses: docker/metadata-action@v4
with:
# https://docs.github.com/en/actions/publishing-packages/publishing-docker-images
images: |
${{ env.GH_REGISTRY }}/${{ env.FULL_IMAGE_NAME }}
tags: |
type=semver,pattern={{version}}
type=ref,event=pr
type=ref,event=branch
# in addition to full semantic version (x.y.z) would also create (x.y)
#type=semver,pattern={{major}}.{{minor}}
- name: Get fresh build arguments
shell: bash
run: echo -e "BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S')\nGITREF=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
id: get_buildargs
# build OCI image and push to Github Container Registry
- name: build and push docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
platforms: linux/amd64,linux/arm64,linux/arm/v7
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
MY_VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
MY_BUILTBY=github-action
BUILD_TIME=${{ steps.get_buildargs.outputs.BUILD_TIME }}
GITREF=${{ steps.get_buildargs.outputs.GITREF }}
GITREF_long="${{ github.sha }}"