Skip to content

Commit

Permalink
Add GitHub workflow for building and publishing docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdoel committed Jul 16, 2024
1 parent d3749ed commit 989e15a
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions .github/workflows/docker_publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: Create and publish a Docker image

Check warning on line 1 in .github/workflows/docker_publish.yml

View workflow job for this annotation

GitHub Actions / linting

1:1 [document-start] missing document start "---"

# Run this workflow every time a change is pushed to the branch called `release`
on:
push:
branches: ['main']

Check failure on line 6 in .github/workflows/docker_publish.yml

View workflow job for this annotation

GitHub Actions / linting

6:16 [brackets] forbidden flow sequence

Check failure on line 6 in .github/workflows/docker_publish.yml

View workflow job for this annotation

GitHub Actions / linting

6:16 [quoted-strings] string value is redundantly quoted with double quotes

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)

Check failure on line 44 in .github/workflows/docker_publish.yml

View workflow job for this annotation

GitHub Actions / linting

44:1 [trailing-spaces] trailing spaces
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: |

Check warning on line 66 in .github/workflows/docker_publish.yml

View workflow job for this annotation

GitHub Actions / linting

66:1 [comments-indentation] comment not indented like content
# # 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.

Check failure on line 74 in .github/workflows/docker_publish.yml

View workflow job for this annotation

GitHub Actions / linting

74:161 [line-length] line too long (257 > 160 characters)
# 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 }}

0 comments on commit 989e15a

Please sign in to comment.