-
Notifications
You must be signed in to change notification settings - Fork 0
146 lines (125 loc) · 4.49 KB
/
docker-image.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
name: Docker
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
on:
push:
branches: [ "master", "*" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
paths-ignore:
- 'README.md'
pull_request:
branches: [ "*" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
paths-ignore:
- 'README.md'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
# adapted from https://docs.github.com/en/actions/learn-github-actions/expressions#example-returning-a-json-object
metadata:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.generate-matrix.outputs.matrix }} # contains a list of the module names encoded as json
modulenames: ${{ steps.generate-matrix.outputs.modulenames }} # contains a simple, space-separated list of the module names
steps:
- name: Checkout
uses: actions/checkout@v3
- name: generate matrix
id: generate-matrix
# adapted from https://stackoverflow.com/a/74829694
# modulenames="$(ls *.yml | sed 's/.yml//' | paste -s -d ' ')"
run: |
modulenames="chipseq gwas"
echo "modulenames=$modulenames" >> $GITHUB_OUTPUT
# echo $modulenames | tr ' ' '\n' | sed 's/^/type=gha,scope=builder-/' | paste -s
matrix=$(echo $modulenames | jq -R 'split(" ") | map({modulename: .})')
echo "matrix=$(echo $matrix | jq -c)" >> $GITHUB_OUTPUT
build-module-images:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
needs: [metadata]
strategy:
matrix: ${{ fromJson(needs.metadata.outputs.matrix) }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: prune
run: docker system prune -af
- name: Builder Docker image
id: builder
uses: docker/build-push-action@v3
with:
context: .
file: Dockerfile
push: false
#tags: ${{ steps.meta.outputs.tags }} # TODO: do we need this?
cache-to: type=gha,mode=max
target: builder
builder-args: |
"modulename=${{ matrix.modulename }}"
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
needs: [metadata, build-module-images]
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: prune
run: docker system prune -af
- name: Patch Dockerfile
id: patch-dockerfile
# use sed to add instructions to our Dockerfile
run: |
DOCKERFILE_CODE="# copy each of the modules into the current image"
for module in ${{ needs.metadata.outputs.modulenames }}; do
DOCKERFILE_CODE="$DOCKERFILE_CODE\nCOPY --from=builder-$module /opt/env /opt/conda/envs/$module"
DOCKERFILE_CODE="$DOCKERFILE_CODE\nENV PATH=\"/opt/conda/envs/$module/bin:\${PATH}\""
done
awk -v r="$DOCKERFILE_CODE" '{gsub(/^#KEEP.*$/,r)}1' Dockerfile > Dockerfile.patch
- name: Primary Docker image
id: primary
uses: docker/builder-push-action@v3
with:
context: .
file: Dockerfile.patch
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
target: primary