-
-
Notifications
You must be signed in to change notification settings - Fork 41
190 lines (183 loc) · 8.77 KB
/
on_push.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Copyright (C) 2022-2023 C-PAC Developers
# This file is part of C-PAC.
# C-PAC is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
# C-PAC is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with C-PAC. If not, see <https://www.gnu.org/licenses/>.
name: Build and test C-PAC
on:
push:
jobs:
check-updated-preconfigs:
name: Check if preconfigs need updated
outputs:
phase_one: ${{ steps.rebuild.outputs.phase_one }}
rebuild_phase_one: ${{ steps.rebuild.outputs.rebuild_phase_one }}
phase_two: ${{ steps.rebuild.outputs.phase_two }}
rebuild_phase_two: ${{ steps.rebuild.outputs.rebuild_phase_two }}
phase_three: ${{ steps.rebuild.outputs.phase_three }}
rebuild_phase_three: ${{ steps.rebuild.outputs.rebuild_phase_three }}
runs-on: ubuntu-latest
steps:
- name: Check out C-PAC
uses: actions/checkout@v3
with:
fetch-depth: 2
- uses: actions/setup-python@v4
with:
python-version: '3.9'
cache: 'pip'
- name: Check if version updated
id: version_updated
run: |
git config --global user.email "CMI_CPAC_Support@childmind.org"
git config --global user.name "Theodore (Machine User)"
GITHUB_BRANCH=$(echo ${GITHUB_REF} | cut -d '/' -f 3-)
export PYTHONPATH=$PWD
pip install -q wheel
pip install -q nipype numpy matplotlib pandas pathvalidate pytz pyyaml voluptuous
python ./CPAC/utils/configuration/yaml_template.py
if [[ ! -z $(git diff origin/${GITHUB_BRANCH}) ]]
then
git add CPAC/resources/configs
git commit -m ":bulb: Update comments based on default preconfig"
fi
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
if [[ ! "$COMMIT_MESSAGE" == *"Update version to"* ]]
then
cd CPAC
VERSION=$(python -c "from info import __version__; print(('.'.join(('.'.join(__version__[::-1].split('-')[1].split('.')[1:])[::-1], __version__.split('-')[1])) if '-' in __version__ else __version__).split('+', 1)[0])")
cd ..
echo "v${VERSION}" > version
find ./CPAC/resources/configs -name "*.yml" -exec sed -i -r "s/^(# [Vv]ersion ).*$/# Version ${VERSION}/g" {} \;
git add version
VERSIONS=($(git diff origin/${GITHUB_BRANCH} -- version | tail -n 2))
export PATTERN="(declare|typeset) -a"
if [[ "$(declare -p VERSIONS)" =~ $PATTERN ]]
then
for DOCKERFILE in $(find ./.github/Dockerfiles -name "*.Dockerfile")
do
export IFS=""
for LINE in $(grep "FROM ghcr\.io/fcp\-indi/c\-pac/.*\-${VERSIONS[0]:1}" ${DOCKERFILE})
do
echo "Updating stage tags in ${DOCKERFILE}"
sed -i "s/\-${VERSIONS[0]:1}/\-${VERSIONS[1]:1}/g" ${DOCKERFILE}
done
done
unset IFS
fi
if [[ ! -z $(git diff origin/${GITHUB_BRANCH}) ]]
then
git add CPAC/resources/configs .github/Dockerfiles
git commit -m ":bookmark: Update version to ${VERSION} ($COMMIT_MESSAGE)" || true
git push origin HEAD:${GITHUB_BRANCH} || true
fi
cd ..
fi
if [[ "${GITHUB_REF_TYPE}" == "tag" ]]
then
cd $HOME/work/C-PAC/C-PAC
for DOCKERFILE in $(ls .github/Dockerfiles/C-PAC.develop-*.Dockerfile)
do
if [[ "$DOCKERFILE" =~ .*C-PAC.develop-(.*)-(bionic|xenial).Dockerfile ]]
then
cp $DOCKERFILE variant-${BASH_REMATCH[1]}.Dockerfile
else
cp $DOCKERFILE Dockerfile
fi
done
git add *ockerfile
git commit -m ":truck: Copy develop Dockerfiles to root directory \
\
[skip ci]"
git push origin HEAD:${GITHUB_BRANCH} || true
fi
- name: Get changed files since last commit
uses: tj-actions/changed-files@v35.7.6
id: changed-files
with:
since_last_remote_commit: "true"
files: .github/Dockerfiles/*
json: "true"
- name: Determine stages to rebuild
env:
MESSAGE: ${{ github.event.head_commit.message }}
id: rebuild
run: |
# initialize phase arrays
declare -a PHASE_ONE PHASE_TWO PHASE_THREE REBUILD_PHASE_ONE REBUILD_PHASE_TWO REBUILD_PHASE_THREE
# turn JSON array into BASH array
CHANGED_FILES=( $(echo ${{ steps.changed-files.outputs.all_changed_files }} | sed -e 's/\[//g' -e 's/\]//g' -e 's/\,/ /g') )
# loop through stages to maybe rebuild
for STAGE in $(cat ${GITHUB_WORKSPACE}/.github/stage_requirements/phase_one.txt)
do
PHASE_ONE+=($STAGE)
# check commit message for [rebuild STAGE] or if STAGE has changed
if [[ "${MESSAGE}" == *"[rebuild ${STAGE}]"* ]] || [[ " ${CHANGED_FILES[*]} " =~ " ${STAGE} " ]]
then
REBUILD_PHASE_ONE+=($STAGE)
fi
done
for STAGE in $(cat ${GITHUB_WORKSPACE}/.github/stage_requirements/phase_two.txt)
do
PHASE_TWO+=($STAGE)
if [[ "${MESSAGE}" == *"[rebuild ${STAGE}]"* ]] || [[ " ${CHANGED_FILES[*]} " =~ " ${STAGE} " ]]
then
REBUILD_PHASE_TWO+=($STAGE)
fi
done
for STAGE in $(cat ${GITHUB_WORKSPACE}/.github/stage_requirements/phase_three.txt)
do
PHASE_THREE+=($STAGE)
if [[ "${MESSAGE}" == *"[rebuild ${STAGE}]"* ]] || [[ "${MESSAGE}" == *"[rebuild base-${STAGE}]"* ]] || [[ " ${CHANGED_FILES[*]} " =~ " ${STAGE} " ]]
then
REBUILD_PHASE_THREE+=($STAGE)
fi
done
# add base stages based on their dependencies
BASES=("${PHASE_THREE[@]}" standard)
if [[ "${MESSAGE}" == *"[rebuild standard]"* ]] || [[ "${MESSAGE}" == *"[rebuild base-standard]"* ]] || [[ " ${CHANGED_FILES[*]} " =~ " standard " ]]
then
REBUILD_PHASE_THREE+=(standard)
fi
for BASE in $BASES
do
for STAGE in $(cat ${GITHUB_WORKSPACE}/.github/stage_requirements/${BASE}.txt)
do
if ([[ " ${REBUILD_PHASE_ONE[*]} " =~ " ${STAGE} " ]] || [[ " ${REBUILD_PHASE_TWO[*]} " =~ " ${STAGE} " ]]) && [[ ! " ${REBUILD_PHASE_THREE[*]} " =~ " ${STAGE} " ]]
then
REBUILD_PHASE_THREE+=($BASE)
fi
done
done
# send stages to rebuild as JSON strings to job outputs
phase_one=$(jq --compact-output --null-input '$ARGS.positional' --args -- ${PHASE_ONE[@]})
rebuild_phase_one=$(jq --compact-output --null-input '$ARGS.positional' --args -- ${REBUILD_PHASE_ONE[@]})
phase_two=$(jq --compact-output --null-input '$ARGS.positional' --args -- ${PHASE_TWO[@]})
rebuild_phase_two=$(jq --compact-output --null-input '$ARGS.positional' --args -- ${REBUILD_PHASE_TWO[@]})
phase_three=$(jq --compact-output --null-input '$ARGS.positional' --args -- ${PHASE_THREE[@]})
rebuild_phase_three=$(jq --compact-output --null-input '$ARGS.positional' --args -- ${REBUILD_PHASE_THREE[@]})
echo "phase_one=${phase_one}" >> $GITHUB_OUTPUT
echo "rebuild_phase_one=${rebuild_phase_one}" >> $GITHUB_OUTPUT
echo "phase_two=${phase_two}" >> $GITHUB_OUTPUT
echo "rebuild_phase_two=${rebuild_phase_two}" >> $GITHUB_OUTPUT
echo "phase_three=${phase_three}" >> $GITHUB_OUTPUT
echo "rebuild_phase_three=${rebuild_phase_three}" >> $GITHUB_OUTPUT
build-stages:
name: Build multistage image stages
needs: check-updated-preconfigs
uses: ./.github/workflows/build_and_test.yml
secrets: inherit
with:
phase_one: ${{ needs.check-updated-preconfigs.outputs.phase_one }}
rebuild_phase_one: ${{ needs.check-updated-preconfigs.outputs.rebuild_phase_one }}
phase_two: ${{ needs.check-updated-preconfigs.outputs.phase_two }}
rebuild_phase_two: ${{ needs.check-updated-preconfigs.outputs.rebuild_phase_two }}
phase_three: ${{ needs.check-updated-preconfigs.outputs.phase_three }}
rebuild_phase_three: ${{ needs.check-updated-preconfigs.outputs.rebuild_phase_three }}