Skip to content

Commit

Permalink
Moving to a GitHub Actions workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
maouw committed Oct 18, 2023
1 parent 29809a9 commit e06af23
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 164 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/apptainer-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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: 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 apt install -y "./$deb"; rm -f "$deb"; unset deb
- name: Check out code for the container build
uses: actions/checkout@v4
- name: Build Container
run: |
cont_name="${GITHUB_REF_NAME#sif-}"
echo "Container name is ${cont_name:-}."
[ -d "${cont_name:-}" ] || exit 1
pushd "${cont_name}" && apptainer build --fakeroot --fix-perms --warn-unused-build-args --build-arg ORAS_REPO="${{ github.repository }}" ../${cont_name}.sif Singularity && popd
tag="${tag:-latest}"
echo "Tag is $tag."
echo "tag=$tag" >> $GITHUB_ENV
echo "cont_name=$cont_name" >> $GITHUB_ENV
popd
- 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
apptainer push "${cont_name}.sif" oras://ghcr.io/${{ github.repository }}/${cont_name}:${tag}
rm -f "${cont_name}.sif"
8 changes: 8 additions & 0 deletions BUILDING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Building the containers on GitHub Actions

The containers are built using GitHub Actions. The workflow is defined in [`.github/workflows/apptainer-image.yml`](.github/workflows/apptainer-image.yml).

The workflow is triggered on every push with the tag `sif-<containername>`. It builds the named container and pushes it to the GitHub Container Registry.

Before containers that depend on other containers can be built, the dependent containers must be built and pushed to the registry. So, if `ubuntu22.04_turbovnc` depends on `ubuntu22.04_interactive`, you must first push a commit with the tag `sif-ubuntu22.04_interactive` for it to be built. Then, you can push a commit with the tag `sif-ubuntu22.04_turbovnc`.

76 changes: 0 additions & 76 deletions Makefile

This file was deleted.

88 changes: 88 additions & 0 deletions scripts/install-freesurfer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/sh
# Install FreeSurfer:

export FREESURFER_VERSION="${FREESURFER_VERSION:-7.4.1}"
FREESURFER_DOWNLOAD_ROOT="https://surfer.nmr.mgh.harvard.edu/pub/dist/freesurfer/${FREESURFER_VERSION}"
export FREESURFER_HOME="${FREESURFER_HOME:-/usr/local/freesurfer/${FREESURFER_VERSION}}"
export FREESURFER_DOWNLOAD_URL="${FREESURFER_DOWNLOAD_URL:-}"

if ! command -v curl >/dev/null 2>&1; then
echo "warning: curl not found!" >&2
exit 1
fi

if [ -z "${FREESURFER_DOWNLOAD_URL:-}" ]; then
if command -v dpkg >/dev/null 2>&1; then
# Is Debian/Ubuntu. Package filename looks like turbovnc_3.0.91_arm64.deb
arch="$(dpkg --print-architecture)"
if [ -r /etc/os-release ]; then
. /etc/os-release
elif [ -r /usr/lib/os-release ]; then
. /usr/lib/os-release
else
echo >&2 "error: cannot determine OS release"
exit 1
fi

case "${ID:-} ${ID_LIKE:-}" in
*ubuntu*) echo "Running on an Ubuntu-like platform, using Ubuntu .deb packages" ;;
*)
echo >&2 "Error: Must be Ubuntu-like"
exit 1
;;
esac
ubuntu_major_version="${VERSION_ID%%.*}"
[ -z "${ubuntu_major_version:-}" ] && {
echo >&2 "error: cannot determine Ubuntu major version"
exit 1
}
filename="freesurfer_ubuntu${ubuntu_major_version}-${FREESURFER_VERSION}_${arch}.deb"
elif command -v yum >/dev/null 2>&1; then
# Is RHEL/CentOS/Rocky. Package filename looks like turbovnc-3.0.91.x86_64.rpm
arch="$(uname -m)"
if [ -r /etc/os-release ]; then
. /etc/os-release
elif [ -r /usr/lib/os-release ]; then
. /usr/lib/os-release
else
echo >&2 "error: cannot determine OS release"
exit 1
fi

case "${ID:-} ${ID_LIKE:-}" in
*rhel* | *centos* | *fedora* | *rocky*) echo "Running on an CentOS-like platform, using CentOS .rpm packages" ;;
*)
echo >&2 "Error: Must be CentOS-like"
exit 1
;;
esac
centos_major_version="${VERSION_ID%%.*}"
[ -z "${centos_major_version:-}" ] && {
echo >&2 "error: cannot determine CentOS major version"
exit 1
}
filename="freesurfer_CentOS${centos_major_version}-${FREESURFER_VERSION}.${arch}.rpm"
else
echo >&2 "error: cannot determine architecture and package extension for this OS"
exit 1
fi
FREESURFER_DOWNLOAD_URL="${FREESURFER_DOWNLOAD_ROOT}/${filename}"
fi

echo "Downloading ${TURBOVNC_DOWNLOAD_URL}..."
dlpath=$(curl -w "%{filename_effective}" -fLO "${TURBOVNC_DOWNLOAD_URL}")
trap 'rm -f "${dlpath:-}"' INT QUIT TERM EXIT

if [ -r "${dlpath}" ]; then
if command -v dpkg >/dev/null 2>&1; then
# Is Debian/Ubuntu
dpkg --install --force-depends "${dlpath:-}" && apt-get install --fix-broken --yes --quiet && export success=1 || echo >&2 "warning: failed to install ${dlpath} via yum"
elif command -v yum >/dev/null 2>&1; then
# Is RHEL/CentOS/Rocky
yum install -y -q "${dlpath}" && export success=1 || echo >&2 "warning: failed to install ${dlpath} via yum"
else
echo >&2 "Cannot determine package manager for this OS"
fi
fi

[ -n "${success:-}" ] && exit 0 || exit 1
47 changes: 19 additions & 28 deletions install-turbovnc.sh → scripts/install-turbovnc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ TURBOVNC_VERSION=${TURBOVNC_VERSION:-3.0.91}
TURBOVNC_FILES_ROOT="${TURBOVNC_FILES_ROOT:-}"
TURBOVNC_DOWNLOAD_ROOT="${TURBOVNC_DOWNLOAD_ROOT:-https://sourceforge.net/projects/turbovnc/files}"
TURBOVNC_DOWNLOAD_URL="${TURBOVNC_DOWNLOAD_URL:-}"
TURBOVNC_TMPDIR="${TURBOVNC_TMPDIR:-./}"

if [ "${TURBOVNC_VERSION}" = "3.0.91" ] && [ -z "${TURBOVNC_FILES_ROOT:-}" ]; then
TURBOVNC_FILES_ROOT="3.0.91 (3.1 beta2)"
fi

if ! command -v curl >/dev/null 2>&1; then
echo "warning: curl not found!" >&2
exit 1
fi

# url-encode the files root:
files_root_enc=$(echo "${TURBOVNC_FILES_ROOT}" | curl -Gs -w '%{url_effective}' --data-urlencode @- ./ | sed "s/%0[aA]$//;s/^[^?]*?\(.*\)/\1/;s/[+]/%20/g")

Expand All @@ -30,33 +34,20 @@ if [ -z "${TURBOVNC_DOWNLOAD_URL:-}" ]; then
TURBOVNC_DOWNLOAD_URL="${TURBOVNC_DOWNLOAD_ROOT}/${files_root_enc}/${filename}"
fi

download_path="${TURBOVNC_TMPDIR}/${filename}"

if command -v curl >/dev/null 2>&1; then
echo "Downloading TurboVNC from ${TURBOVNC_DOWNLOAD_URL} to ${download_path} via curl..."
curl -o "${download_path}" -fsSL "${TURBOVNC_DOWNLOAD_URL}" || {
echo >&2 "error: failed to download ${TURBOVNC_DOWNLOAD_URL}"
exit 1
}
elif command -v wget >/dev/null 2>&1; then
echo "Downloading TurboVNC from ${TURBOVNC_DOWNLOAD_URL} to ${download_path} via wget..."
wget -O "${download_path}" "${TURBOVNC_DOWNLOAD_URL}" || {
echo >&2 "error: failed to download ${TURBOVNC_DOWNLOAD_URL}"
exit 1
}
else
echo >&2 "error: curl or wget not found"
exit 1
fi
echo "Downloading ${TURBOVNC_DOWNLOAD_URL}..."
dlpath=$(curl -w "%{filename_effective}" -fLO "${TURBOVNC_DOWNLOAD_URL}")
trap 'rm -f "${dlpath:-}"' INT QUIT TERM EXIT

if command -v dpkg >/dev/null 2>&1; then
# Is Debian/Ubuntu
apt install -y -q --install-suggests --install-recommends "${download_path}" || echo >&2 "warning: failed to install ${TURBOVNC_DOWNLOAD_PATH}"
elif command -v yum >/dev/null 2>&1; then
# Is RHEL/CentOS/Rocky
yum install -y -q "${download_path}" || echo >&2 "warning: failed to install ${TURBOVNC_DOWNLOAD_PATH}"
else
echo >&2 "error: cannot determine package manager for this OS"
if [ -r "${dlpath}" ]; then
if command -v dpkg >/dev/null 2>&1; then
# Is Debian/Ubuntu
dpkg --install --force-depends "${dlpath:-}" && apt-get install --fix-broken --yes --quiet && export success=1
elif command -v yum >/dev/null 2>&1; then
# Is RHEL/CentOS/Rocky
yum install -y -q "${dlpath}" && export success=1 || echo >&2 "warning: failed to install ${dlpath} via yum"
else
echo >&2 "Cannot determine package manager for this OS"
fi
fi

rm -f "${download_path}"
[ -n "${success:-}" ] && exit 0 || exit 1
60 changes: 15 additions & 45 deletions ubuntu22.04_freesurfer/Singularity
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Bootstrap: localimage
From: {{CONTAINERDIR}}/ubuntu22.04_turbovnc.sif
Bootstrap: oras
From: oras://{{ORAS_REPO}}/ubuntu22.04_turbovnc:latest

%arguments
CONTAINERDIR=../
ORAS_REPO=ghcr.io/uw-psych/hyakvnc_apptainer
FREESURFER_DEB=freesurfer.deb
FREESURFER_VERSION=7.4.1
FREESURFER_HOME=/usr/local/freesurfer/{{FREESURFER_VERSION}}
Expand All @@ -11,16 +11,8 @@ From: {{CONTAINERDIR}}/ubuntu22.04_turbovnc.sif
%help
This is an Apptainer container of Ubuntu 22.04 with TurboVNC and FreeSurfer installed.

%setup
f="{{FREESURFER_DEB}}"
if [ -r "$f" ]; then
echo "Using pre-downloaded freesurfer binary package at ${f:-}!"
cp "${f:-}" "${APPTAINER_ROOTFS}/freesurfer.deb"
else
echo "Did not find pre-downloaded freesurfer binary package at location ${f:-}"
fi

%files
../scripts/install-freesurfer.sh /opt/install-freesurfer.sh
freeview.desktop /usr/share/applications/freeview.desktop

%environment freesurfer
Expand All @@ -41,51 +33,29 @@ From: {{CONTAINERDIR}}/ubuntu22.04_turbovnc.sif
export MNI_PERL5LIB="$MINC_LIB_DIR/perl5/5.8.5" \
export PATH="$FREESURFER_HOME/bin:$FREESURFER_HOME/tktools:$MINC_BIN_DIR:$PATH"
export SHELL="/bin/bash"
export LC_ALL='C.UTF-8'
export LANG='C.UTF-8'

%post
# Enable exit on error:
set -e

# Trace shell commands:
set -x

export FREESURFER_HOME="{{FREESURFER_HOME}}"
export DEBIAN_FRONTEND=noninteractive
export LC_ALL='C.UTF-8'
export LANG='C.UTF-8'

# Enable exit on error:
set -e

# Update apt:
apt-get update --yes --quiet

# Download freesurfer binary package if not exists:
if [ ! -r /freesurfer.deb ]; then
echo "Not using pre-downloaded freesurfer binary package!"
FREESURFER_DL_URL="{{FREESURFER_DL_URL}}"
if [ -n "${FREESURFER_DL_URL:-}" ]; then
echo "Downloading freesurfer binary package from "${FREESURFER_DL_URL}"..."
curl -L -o /freesurfer.deb "${FREESURFER_DL_URL}" || exit 1
else
echo "FREESURFER_DL_URL is not set. Don't know where to download freesurfer binary package from. Exiting."
exit 1
fi
else
echo "Using pre-downloaded freesurfer binary package."
fi

# Disable exit on error so dpkg --install can run without exiting due to dependencies:
set +e

# Create necessary directories:
mkdir -p "$FREESURFER_HOME"

# Install freesurfer:
dpkg --install /freesurfer.deb

# Fix dependencies:
apt-get --fix-broken install --yes --quiet --no-install-recommends

# Enable exit on error:
set -e
/opt/install-freesurfer.sh && echo "Installed freesurfer" || exit 1

# Clean up freesurfer binary package:
rm -f /freesurfer.deb && echo "Removed freesurfer binary download"
# Remove freesurfer installer:
rm -f /opt/install-freesurfer.sh

# Clean up packages:
apt-get clean --yes --quiet && rm -rf /var/lib/apt/lists/*
1 change: 0 additions & 1 deletion ubuntu22.04_freesurfer/freesurfer.deb

This file was deleted.

Loading

0 comments on commit e06af23

Please sign in to comment.