Skip to content

Add GitHub workflow for building and publishing docker image #1

Add GitHub workflow for building and publishing docker image

Add GitHub workflow for building and publishing docker image #1

name: Create and publish a Docker image
# Run this workflow every time a change is pushed to the branch called `release`
on:
push:
branches: ['main']
env:
# GitHub container registry
REGISTRY: ghcr.io
# Name used for Docker image
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push-image:
runs-on: ubuntu-latest
# Set permissions granted to the `GITHUB_TOKEN` for the actions in this job
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
# Checkout the repository
- name: Checkout repository
uses: actions/checkout@v4
- name: Generate XNAT container service labels
id: xnat-command-label
run: |
import json
import os
repo_dir = os.getenv('GITHUB_WORKSPACE')
command_list = []
for file in os.listdir(repo_dir):
if file.endswith(".json"):
with open(file) as f:
command_object = json.load(f)
command_string = json.dumps(command_object)
command_list.append(command_string)
commands = ','.join(command_list)
label = f'org.nrg.commands=[{commands}]'
with open(os.environ['GITHUB_OUTPUT'], 'a') as github_output:
print(f"label={label}", file=github_output)
shell: python
# Log into the container registry
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Extract metadata (tags and labels) to be applied to the image. See [docker/metadata-action](https://github.com/docker/metadata-action#about)
# The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# labels: |
# # set xnat container service label defining commands in this image
# ${{ steps.xnat-command-label.outputs.label }}
tags: |
# set latest tag for default branch
type=raw,value=latest,enable={{is_default_branch}}
# Build the image from the Dockerfile in the repo and push to the container registry.
# The `context` parameter defines the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository.
# Tag and label the image using the output from the metadata step above
- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}