Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callable workflow for Docker image build #3393

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions .github/workflows/docker-build-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
name: build-image

on:
workflow_call:
inputs:
image-name:
required: true
type: string
build-context:
required: true
type: string
dockerfile:
required: true
type: string
r-version:
required: true
type: string
parent-image:
required: false
default: ''
type: string
model-version:
required: false
default: ''
type: string
platforms:
required: false
default: "linux/amd64"
type: string

jobs:
build:
runs-on: ubuntu-latest
permissions:
packages: write

steps:

- name: lowercase image name
id: name
run: echo "image_name=$(echo ${{ inputs.image-name }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT

- name: set PARENT_IMAGE only if specified
id: parent
shell: bash
run: |
REGISTRY=$(
[[ ${{ github.event_name }} == 'pull_request' ]] &&
echo "ghcr.io/${{ github.actor }}/${{ github.repository }}" ||
echo "docker.io/pecan")
echo "PARENT_IMAGE_IF_SET=$(
[[ -n '${{ inputs.parent-image }}' ]] &&
echo "PARENT_IMAGE=${REGISTRY}/"'${{ inputs.parent-image }}'
)" >> $GITHUB_OUTPUT

- name: set MODEL_VERSION only if specified
id: modelver
shell: bash
run: |
echo "MODEL_VERSION_IF_SET=$(
[[ -n '${{ inputs.model-version }}' ]] &&
echo 'MODEL_VERSION=${{ inputs.model-version }}'
)" >> $GITHUB_OUTPUT

- uses: actions/checkout@v4

# create metadata for image
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
# list of Docker images to use as base name for tags
images: |
name=ghcr.io/${{ github.actor }}/${{ github.repository }}/${{ steps.name.outputs.image_name }}
name=pecan/${{ steps.name.outputs.image_name }},enable=${{ github.event_name != 'pull_request' }}
# generate Docker tags based on the following events/attributes
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}

# setup docker build
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3

- 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 }}"

# login to registries
- name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# build the docker images
- name: Build and push ${{ steps.name.outputs.image_name }}
uses: docker/build-push-action@v6
with:
context: ${{ inputs.build-context }}
file: ${{ inputs.dockerfile }}
push: true
platforms: ${{ inputs.platforms }}
cache-from: type=gha
cache-to: type=gha,mode=max
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=${{ steps.meta.outputs.version }}
IMAGE_VERSION=${{ steps.meta.outputs.version }}
PECAN_VERSION=${{ steps.meta.outputs.version }}
R_VERSION=${{ inputs.r-version }}
${{ steps.parent.outputs.PARENT_IMAGE_IF_SET }}
${{ steps.modelver.outputs.MODEL_VERSION_IF_SET }}
GITHUB_PAT=${{ secrets.GITHUB_TOKEN }}
PECAN_GIT_BRANCH=${{ github.head_ref || github.ref_name }}
PECAN_GIT_CHECKSUM=${{ github.sha }}
PECAN_GIT_DATE=${{ github.event.repository.updated_at }}
Loading
Loading