Skip to content

Build and Deploy Apptainer Container #23

Build and Deploy Apptainer Container

Build and Deploy Apptainer Container #23

Workflow file for this run

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: Get container name
run: |
cont_name=$(echo "${GITHUB_REF_NAME:-}" | sed -E 's/^sif-//; s/-.*$//')
echo "Container name is ${cont_name:-}"
if [ ! -r "${cont_name:-}/Singularity" ]; then
echo "No container named ${cont_name:-} found."
exit 1
fi
echo "cont_name=$cont_name" >> $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"
echo "Disk space after:" && df -h
- 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."
echo "Disk space after:" && df -h
- name: Install Apptainer
run: deb=$(curl -w "%{filename_effective}" -LO https://github.com/apptainer/apptainer/releases/download/v1.2.4/apptainer_1.2.4_amd64.deb) && sudo dpkg --install --force-depends "${deb}" && sudo apt-get install --fix-broken --yes --quiet; rm -f "$deb"; unset deb; sudo apt-get clean -yq
- name: Check out code for the container build
uses: actions/checkout@v4
- name: Build Container
run: |
echo "Disk space before:" && df -h
pushd "${cont_name}"
echo "Building ${cont_name}.sif"
apptainer build --fakeroot --fix-perms --warn-unused-build-args --build-arg BOOTSTRAP_SOURCE=oras BOOTSTRAP_REPO="ghcr.io/${{ github.repository }}" ../${cont_name}.sif Singularity
echo "Built ${cont_name}.sif"
popd
apptainer cache clean -f
echo "Cleaned Apptainer cache"
tag="${tag:-latest}"
echo "Tag is $tag."
echo "tag=$tag" >> $GITHUB_ENV
echo "Disk space after:" && df -h
- name: Login and Deploy Container
run: |
[ -r "${cont_name}.sif" ] || 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"