Skip to content

Merge pull request #128 from umccr/JMarzec-patch-2 #48

Merge pull request #128 from umccr/JMarzec-patch-2

Merge pull request #128 from umccr/JMarzec-patch-2 #48

name: build-docker-container-and-push-to-ghcrio
on:
push:
tags:
# Tags with a pre-v prefix will have a '-dev' at the end of their suffix
- 'v*' # Push events to matching v*, i.e v1.0, v20.15.10 etc
- 'pre-v*' # Push events to matching pre-v, i.ve pre-v1.0, pre-v20.15.10
jobs:
get_parameters:
runs-on: ubuntu-latest
steps:
# Standard checkout step
- name: Checkout code
id: git_checkout
uses: actions/checkout@v3
# Get tag name, from /ref/heads/<tag> to <tag>
- name: Get Tag Name
id: get_tag_name
shell: bash
run: |
# Get tag name but strip out 'v' or pre-v from start
tag_name="$(basename ${{ github.ref }})"
if [[ "${tag_name}" =~ ^pre-v* ]]; then
# Strip pre-v from tag and add '-dev' to tag name
tag_name="${tag_name#pre-v}-dev"
elif [[ "${tag_name}" =~ ^v* ]]; then
tag_name="${tag_name#v}"
else
echo "Error: tag-name had neither 'pre-v' prefix nor 'v' prefix. Exiting"
exit 1
fi
# Output the tag name
echo "tag_name=${tag_name}" >> "${GITHUB_OUTPUT}"
# Get repository name in lower
- name: get lowercase repo name
id: get_lowercase_repo_name
run: echo "repo_name=$(tr '[:upper:]' '[:lower:]' <<< "${{ github.repository }}")" >> "${GITHUB_OUTPUT}"
# Set platforms
- name: Set platforms
id: set_platforms
run: echo 'platforms=["linux/amd64"]' >> "${GITHUB_OUTPUT}"
outputs:
tag_name: ${{ steps.get_tag_name.outputs.tag_name }}
repo_name: ${{ steps.get_lowercase_repo_name.outputs.repo_name }}
platforms: ${{ steps.set_platforms.outputs.platforms }}
docker_scatter_build:
needs: get_parameters
runs-on: ubuntu-latest
strategy:
matrix:
platform: ${{ fromJSON(needs.get_parameters.outputs.platforms) }}
fail-fast: false # Don't fail if one image fails to run
steps:
# Standard checkout step
- name: Checkout code
id: git_checkout
uses: actions/checkout@v3
# Platform to tag suffix
- name: Platform to tag suffix
id: platform_to_tag_suffix
run: echo "platform_tag=$( echo ${{ matrix.platform }} | tr '/' '-' )" >> "${GITHUB_OUTPUT}"
# Use qemu to perform multiplatform builds
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
# Use docker buildx to build multi-platform containers
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
use: true
install: true
config-inline: |
[worker.oci]
max-parallelism = 2
# Log in to GitHub Container registry
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Build and push docker images
- name: Build and Push Docker Image
uses: docker/build-push-action@v4
with:
context: .
file: envm/Dockerfile
platforms: ${{ matrix.platform }}
provenance: false
push: true
tags: ghcr.io/${{ needs.get_parameters.outputs.repo_name }}:${{ needs.get_parameters.outputs.tag_name }}-${{ steps.platform_to_tag_suffix.outputs.platform_tag }}
tag_manifest:
needs: [ get_parameters, docker_scatter_build ]
runs-on: ubuntu-latest
steps:
# Standard checkout step
- name: Checkout code
id: git_checkout
uses: actions/checkout@v3
# Log in to GitHub Container registry
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Create manifest file with docker
- name: Create and push manifest file
id: create_and_push_manifest_file
shell: bash
run: |
# Create and push a manifest file
# Install jq
echo "Installing jq binary" 1>&2
sudo apt-get update -y -qq
sudo apt-get install -y -qq \
jq
# Set manfest name as var
echo "Setting manifest name as 'ghcr.io/${{ needs.get_parameters.outputs.repo_name }}:${{ needs.get_parameters.outputs.tag_name }}'" 1>&2
manifest_name="ghcr.io/${{ needs.get_parameters.outputs.repo_name }}:${{ needs.get_parameters.outputs.tag_name }}"
# Get platform tags from platforms
# linux/amd64 to linux-amd64 etc.
echo "Reading platform suffixes from get_parameters step" 1>&2
readarray -t TAGS_ARRAY < <(jq -rc '.[] | sub("/"; "-")' <<< '${{ needs.get_parameters.outputs.platforms }}')
# Create the manifest file
echo "Creating the manifest file" 1>&2
docker manifest create \
"${manifest_name}" \
${TAGS_ARRAY[@]/#/${manifest_name}-}
# Push the manifest file
echo "Pushing the multi-platform manifest file" 1>&2
docker manifest push "${manifest_name}"
update_renv_lock:
needs: [ get_parameters, tag_manifest ]
runs-on: ubuntu-latest
steps:
# Install jq (for querying branch name)
- name: install jq
id: install_jq
shell: bash
run: |
sudo apt-get update -y
sudo apt-get install jq -y
# Get branch name from event path
- name: get branch name
id: get_branch_name
shell: bash
run: |
# Get reference
ref="$( \
jq --raw-output \
'if .head_ref != null then
.head_ref
else .base_ref
end' \
< "${GITHUB_EVENT_PATH}" \
)"
# Set output
echo "branch_name=${ref%refs/heads/}" >> "${GITHUB_OUTPUT}"
# Not so standard checkout step
# Need to checkout PR
- name: Checkout code
id: git_checkout
uses: actions/checkout@v3
with:
# Otherwise just in a detached head state
ref: ${{ steps.get_branch_name.outputs.branch_name }}
# Update renv.lock with that generated inside the docker image created
- name: Update renv lock file
id: update_renv_lock_file
shell: bash
run: |
docker run --rm \
"ghcr.io/${{ needs.get_parameters.outputs.repo_name }}:${{ needs.get_parameters.outputs.tag_name }}" \
cat /rmd_files/renv.lock > rmd_files/renv.lock
# Commit and push configuration files
- name: Commit renv file
id: commit_renv_file
uses: EndBug/add-and-commit@v9
with:
# Add just the renv lock file
add: "rmd_files/renv.lock"
# Have the github actions logo show
default_author: github_actions
# The commit message to show
message: "Updating renv.lock file after latest build - (Autogenerated github actions commit)"
# Push
push: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Fast forward tag
- name: Fast Forward tag
id: fast_forward_tag
shell: bash
run: |
# Fast forwarding tag
git tag --force "${{ github.ref_name }}"
git push --tags --force