Build and Deploy Apptainer Container #41
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Singularity Build | |
on: | |
push: | |
tags: | |
- sif-*#* | |
jobs: | |
build-and-push-image: | |
runs-on: ubuntu-latest | |
name: Build Apptainer image | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Check out code for the container build | |
uses: actions/checkout@v4 | |
- name: Get container name | |
run: | | |
cont_name=$(echo "${GITHUB_REF_NAME:-}" | sed -E 's/^sif-//; s/#.*$//') | |
echo "Container name is ${cont_name:-}" | |
[ -d "${cont_name:-}" ] || { echo "No container named ${cont_name:-} found."; exit 1; } | |
echo "cont_name=$cont_name" >> $GITHUB_ENV | |
ref_name=${{ github.ref_name }} | |
echo "ref_name is ${ref_name}." | |
img_tag"$(echo "${ref_name:-"sif-}" | sed -E '/.*#.*/!d; s/(.*)#(.+$)/\2/;1q')" | |
img_tag="${img_tag:-}" | |
echo "img_tag is ${img_tag}." | |
echo "img_tag=$img_tag" >> $GITHUB_ENV | |
- name: Delete GitHub AGENT_TOOLSDIRECTORY to free space | |
run: | | |
echo "Disk space before:" && df -h | |
sudo rm -rf /opt/hostedtoolcache; echo "Done removing /opt/hostedtoolcache" | |
- name: Delete .NET, Android, Haskell tools to free space | |
run: | | |
echo "Disk space before:" && df -h | |
sudo rm -rf /usr/share/dotnet /opt/ghc /usr/local/lib/android && echo "Done removing .NET, Android, and Haskell tools." | |
- name: Install Apptainer | |
uses: maouw/github-action-apptainer-install@v1 | |
# Cache curl downloads | |
- uses: actions/cache@v3 | |
id: cache-setup-downloads | |
with: | |
path: | | |
**/*.deb | |
key: ${{ runner.os }} | |
- name: Build Container | |
run: | | |
echo "Disk space before:" && df -h | |
pushd "${cont_name}" | |
echo "Building ${cont_name}.sif" | |
if grep -q 'From:.*hyakvnc-.*' Singularity; then | |
BASE_IMAGE_NAME="$(sed -E '/^\s*From:.*(hyakvnc-.*)/!d; s/.*(hyakvnc-[.A-Za-z0-9_-]+).*/\1/;1q' Singularity)" | |
echo "Base image name is $BASE_IMAGE_NAME" | |
BASE_IMAGE_TAG="$(git tag --sort=committerdate --list '*'"${BASE_IMAGE_NAME:-}"'*' | sed -E '/.*#.*/!d; s/(.*)#(.+$)/\2/;1q')" | |
fi | |
BASE_IMAGE_TAG="${BASE_IMAGE_TAG:-latest}" | |
[ -n "${BASE_IMAGE_NAME:-}" ] && echo "Base image name is $BASE_IMAGE_NAME" | |
echo "Base image tag is ${BASE_IMAGE_TAG}" | |
export IMAGE_TAG="${img_tag:-}" | |
apptainer build --warn-unused-build-args --force --build-arg BOOTSTRAP_SOURCE=oras --build-arg BOOTSTRAP_FROM_REPO="ghcr.io/${{ github.repository }}" --build-arg BOOTSTRAP_FROM_SUFFIX=":${BASE_IMAGE_TAG}" ../${cont_name}.sif Singularity | |
echo "Built ${cont_name}.sif" | |
popd | |
apptainer cache clean -f | |
echo "Cleaned Apptainer cache" | |
echo "Disk space after:" && df -h | |
- name: Login and Deploy Container | |
run: | | |
[ -r "${cont_name}.sif" ] || { echo "No container named ${cont_name}.sif found."; exit 1; } | |
apptainer remote login -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }} oras://ghcr.io | |
echo "Pushing ${cont_name}.sif to ghcr.io/${{ github.repository }}/${cont_name}:${tag}" | |
apptainer push "${cont_name}.sif" oras://ghcr.io/${{ github.repository }}/${cont_name}:${tag} | |
echo "Pushed ${cont_name}.sif to ghcr.io/${{ github.repository }}/${cont_name}:${tag}" | |
rm -f "${cont_name}.sif" |