Skip to content

Commit

Permalink
Automatically generate Docker label for XNAT container service as par…
Browse files Browse the repository at this point in the history
…t of the GitHub build
  • Loading branch information
tomdoel committed Aug 21, 2024
1 parent 6a360d8 commit 9f2e6d1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
17 changes: 9 additions & 8 deletions .github/workflows/docker_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ jobs:
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)
print(f"nrglabel=[{commands}]", file=github_output)
shell: python

# Log into the container registry
Expand All @@ -59,22 +58,22 @@ jobs:

# 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.
# The current version does not allow for labels to be added containing the # symbol, so we add the
# custom nrg label in the build-push-action below
- 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 }}
# labels: 'org.nrg.commands=${{ steps.xnat-command-label.outputs.nrglabel }}'
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
# 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
Expand All @@ -84,4 +83,6 @@ jobs:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
labels: |
${{ steps.meta.outputs.labels }}
org.nrg.commands=${{ steps.xnat-command-label.outputs.nrglabel }}
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,3 @@ ENV PATH=/opt/venv/bin:$PATH

# Default command to execute
CMD ["share-to-genetic"]

LABEL org.nrg.commands="[{\"name\": \"share-to-genetic\", \"description\": \"Share subject to genetic project\", \"label\": \"Share subject to genetic project\", \"version\": \"1.0\", \"schema-version\": \"1.0\", \"type\": \"docker\", \"command-line\": \"share_subject_to_genetic_project #SUBJECTID#\", \"image\": \"ghcr.io/ucl-mirsg/drc-containers:latest\", \"override-entrypoint\": true, \"mounts\": [], \"inputs\": [{\"name\": \"SUBJECTID\", \"description\": \"The subject ID\", \"type\": \"string\", \"user-settable\": false, \"required\": true, \"replacement-key\": \"#SUBJECTID#\"}], \"outputs\": [], \"xnat\": [{\"name\": \"share-subject-to-genetic\", \"label\": \"Share subject to genetic project\", \"description\": \"Shares subject to the corresponding genetic project\", \"contexts\": [\"xnat:subjectData\"], \"external-inputs\": [{\"name\": \"subject\", \"description\": \"Input subject\", \"type\": \"Subject\", \"required\": true, \"load-children\": false}], \"derived-inputs\": [{\"name\": \"subject-id\", \"type\": \"string\", \"derived-from-wrapper-input\": \"subject\", \"derived-from-xnat-object-property\": \"id\", \"provides-value-for-command-input\": \"SUBJECTID\", \"user-settable\": false, \"required\": true}], \"output-handlers\": []}]}]"
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ array and the first argument is the function being executed

If you need to add additional python module dependencies, you will need to
configure these in `pyproject.toml` in the `[project]` section, e.g.:

```toml
[project]
dependencies = [
Expand Down Expand Up @@ -70,14 +71,22 @@ your container.
[XNAT documentation](https://wiki.xnat.org/container-service/json-command-definition)
for full details.

### 4. Run `generate_docker_label.py`
### 4. Commit and push the changes to the repository

GitHub actions will build and publish the Docker image, and create the
`org.nrg.commands` Docker label using the json command definition files in the
repository.

This generates a Docker label for the XNAT container service.
### For local testing: `generate_docker_label.py`

Add this to the end of the Dockerfile (including the `LABEL` statement and
replacing any existing label).
This script generates a Docker label for the XNAT constainer service which is
equivalent to the one created by the GitHub Actions workflow. You do not need to
run this script unless you wish to do so for testing purposes.

### 5. Commit and push the changes to the repository
If you wish to add this label to your local docker image, you can add this to
the end of your Dockerfile (including the `LABEL` statement and replacing any
existing label). However, you should not check the label into the repository
since the label is generated by the GitHub Actions workflow.

---

Expand Down
5 changes: 4 additions & 1 deletion generate_docker_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ def main():
with open(file) as f:
command_object = json.load(f)
command_string = (
json.dumps(command_object).replace('"', r"\"").replace("$", r"\$")
json.dumps(command_object)
.replace("\\", "\\\\")
.replace('"', '\\"')
.replace("$", "\\$")
)
command_list.append(command_string)

Expand Down

0 comments on commit 9f2e6d1

Please sign in to comment.