Skip to content

Commit 7fb0246

Browse files
authored
add apptainer github actions scripts to build container (#22)
* add apptainer github actions scripts to build container * temporarily build container on test branch to be able to add container to profile without merging to main * add apptainer profile to nextflow config * remove test branch from build and push workflow after initial build * add missing parentheses in profile * bump pipeline version
1 parent 6c425a4 commit 7fb0246

6 files changed

Lines changed: 138 additions & 1 deletion

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
mkdir -p artifacts
3+
mkdir -p wave_images
4+
5+
for env_yaml in environments/*.yml; do
6+
image_name=$(head -n 1 $env_yaml | cut -d ' ' -f 2)
7+
echo "building image ${image_name} from file ${env_yaml}..."
8+
wave \
9+
--conda-file ${env_yaml} \
10+
--singularity \
11+
--freeze \
12+
--await \
13+
--output json \
14+
| python -m json.tool \
15+
| tee wave_images/${image_name}.json
16+
echo "done building image ${image_name}"
17+
cp wave_images/${image_name}.json artifacts/
18+
done
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
wget https://raw.githubusercontent.com/apptainer/apptainer/main/tools/install-unprivileged.sh
4+
chmod +x install-unprivileged.sh
5+
mkdir -p /opt/apptainer
6+
./install-unprivileged.sh /opt/apptainer
7+
echo "/opt/apptainer/bin" >> $GITHUB_PATH
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
wget https://github.com/seqeralabs/wave-cli/releases/download/v1.4.1/wave-1.4.1-linux-x86_64
4+
mv wave-1.4.1-linux-x86_64 wave
5+
chmod +x wave
6+
mkdir -p /opt/wave/bin
7+
mv wave /opt/wave/bin
8+
echo "/opt/wave/bin" >> $GITHUB_PATH
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import glob
5+
import json
6+
import os
7+
import subprocess
8+
9+
10+
def pull_image(image_url, destination_image_file):
11+
"""
12+
Pull the image file from external server
13+
"""
14+
apptainer_pull_cmd = [
15+
"apptainer",
16+
"pull",
17+
destination_image_file,
18+
image_url,
19+
]
20+
subprocess.run(apptainer_pull_cmd)
21+
22+
23+
def push_image(source_image_file, image_url):
24+
"""
25+
Push apptainer image to destination image repository
26+
"""
27+
28+
apptainer_push_cmd = [
29+
"apptainer",
30+
"push",
31+
source_image_file,
32+
image_url,
33+
]
34+
subprocess.run(apptainer_push_cmd)
35+
36+
37+
def main(args):
38+
repo_owner = os.environ['GITHUB_REPOSITORY_OWNER'].lower()
39+
40+
wave_jsons = glob.glob(os.path.join(args.wave_jsons_dir, "*.json"))
41+
for wave_json in wave_jsons:
42+
with open(wave_json, 'r') as f:
43+
w = json.load(f)
44+
pull_image_url = w['containerImage']
45+
image_name_with_version = pull_image_url.split('/')[-1]
46+
image_name, image_version = image_name_with_version.split(':')
47+
pull_destination = os.path.join(args.images_dir, f"{image_name}--{image_version}.img")
48+
pull_image(pull_image_url, pull_destination)
49+
50+
push_image_url = f"oras://ghcr.io/{repo_owner}/{image_name}:{image_version}"
51+
push_image(pull_destination, push_image_url)
52+
53+
54+
if __name__ == '__main__':
55+
parser = argparse.ArgumentParser()
56+
parser.add_argument('--wave-jsons-dir')
57+
parser.add_argument('--images-dir')
58+
args = parser.parse_args()
59+
main(args)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Build and Push Container Images
2+
3+
on:
4+
push:
5+
tags:
6+
- v*
7+
workflow_dispatch:
8+
9+
env:
10+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
11+
GHCR_USERNAME: ${{ secrets.GHCR_USERNAME }}
12+
13+
jobs:
14+
build_and_push:
15+
runs-on: ubuntu-latest
16+
permissions:
17+
packages: write
18+
contents: read
19+
steps:
20+
- uses: actions/checkout@v4
21+
- name: Install Apptainer
22+
run: |
23+
.github/scripts/install_apptainer.sh
24+
- name: Check Apptainer installation
25+
run: apptainer --version
26+
- name: Install Wave CLI
27+
run: |
28+
.github/scripts/install_wave-cli.sh
29+
- name: Check wave installation
30+
run: wave --version
31+
- name: Build images
32+
run: |
33+
.github/scripts/build_container_images_wave.sh
34+
- name: Push images
35+
run: |
36+
echo ${GITHUB_TOKEN} | apptainer registry login -u ${GHCR_USERNAME} --password-stdin oras://ghcr.io
37+
.github/scripts/push_container_images_wave.py --wave-jsons-dir wave_images --images-dir wave_images

nextflow.config

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
manifest {
22
author = 'Sherrie Wang, Dan Fornika'
33
name = 'BCCDC-PHL/cgmlst_clustering-nf'
4-
version = '0.1.1'
4+
version = '0.1.2'
55
description = 'BCCDC-PHL cgMLST and clustering'
66
mainScript = 'main.nf'
77
nextflowVersion = '>=20.01.0'
@@ -23,4 +23,12 @@ profiles {
2323
conda.cacheDir = params.cache
2424
}
2525
}
26+
27+
apptainer {
28+
apptainer.enabled = true
29+
process.container = "oras://ghcr.io/bccdc-phl/cgmlst-clustering:2117e4b6a7943865"
30+
if (params.cache){
31+
apptainer.cacheDir = params.cache
32+
}
33+
}
2634
}

0 commit comments

Comments
 (0)