Skip to content

Commit 9b2f444

Browse files
committed
CI: Refactor CircleCI config to use newer machines/images and anchors
1 parent 5b46125 commit 9b2f444

File tree

1 file changed

+94
-61
lines changed

1 file changed

+94
-61
lines changed

.circleci/config.yml

Lines changed: 94 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
1+
# reusable anchors
2+
_machine_defaults: &machine_defaults
3+
environment:
4+
TZ: "/usr/share/zoneinfo/America/Los_Angeles"
5+
SCRATCH: "/scratch"
6+
machine:
7+
image: ubuntu-2204:current
8+
docker_layer_caching: true
9+
working_directory: /tmp/src/sdcflows
10+
resource_class: large
11+
12+
_python_defaults: &python_defaults
13+
docker:
14+
- image: cimg/python:3.10.9
15+
working_directory: /tmp/src/sdcflows
16+
17+
_docker_auth: &docker_auth
18+
name: Docker authentication
19+
command: |
20+
if [[ -n $DOCKER_PAT ]]; then
21+
echo "$DOCKER_PAT" | docker login -u $DOCKER_USER --password-stdin
22+
fi
23+
24+
_setup_docker_registry: &setup_docker_registry
25+
name: Set up Docker registry
26+
command: |
27+
if [[ -f /tmp/images/registry.tar.gz ]]; then
28+
echo "Loading saved registry image"
29+
docker load < /tmp/images/registry.tar.gz
30+
else
31+
echo "Pulling registry image from DockerHub"
32+
docker pull registry:2
33+
fi
34+
docker run -d -p 5000:5000 --restart=always --name=registry \
35+
-v /tmp/docker:/var/lib/registry registry:2
36+
37+
_pull_from_registry: &pull_from_registry
38+
name: Pull and tag image from local registry
39+
command: |
40+
docker pull localhost:5000/sdcflows
41+
docker tag localhost:5000/sdcflows nipreps/sdcflows:latest
42+
143
version: 2.1
244
orbs:
3-
docker: circleci/docker@1.6.0
45+
docker: circleci/docker@2.1.4
446

547
jobs:
648
cache_test_data:
@@ -126,11 +168,8 @@ jobs:
126168
- /tmp/freesurfer
127169

128170
build_n_pytest:
129-
machine:
130-
image: ubuntu-2004:202107-02
171+
<<: *machine_defaults
131172
working_directory: /tmp/tests
132-
environment:
133-
TZ: "/usr/share/zoneinfo/America/Los_Angeles"
134173
steps:
135174
- restore_cache:
136175
keys:
@@ -140,45 +179,53 @@ jobs:
140179
- build-v2-
141180
paths:
142181
- /tmp/docker
182+
- docker/install-docker-credential-helper
183+
- run: *docker_auth
184+
- run: *setup_docker_registry
143185
- run:
144-
name: Set-up a Docker registry
186+
name: Pull Ubuntu/jammy image
145187
command: |
146-
docker run -d -p 5000:5000 --restart=always --name=registry \
147-
-v /tmp/docker:/var/lib/registry registry:2
188+
set +e
189+
docker pull localhost:5000/ubuntu
190+
success=$?
191+
set -e
192+
if [[ "$success" = "0" ]]; then
193+
echo "Pulling from local registry"
194+
docker tag localhost:5000/ubuntu ubuntu:jammy
195+
else
196+
echo "Pulling from Docker Hub"
197+
docker pull ubuntu:jammy
198+
docker tag ubuntu:jammy localhost:5000/ubuntu
199+
docker push localhost:5000/ubuntu
200+
fi
148201
- run:
149-
name: Pull images
202+
name: Pull SDCFlows Docker image
150203
command: |
151204
set +e
152-
docker pull localhost:5000/ubuntu
205+
docker pull localhost:5000/sdcflows
153206
success=$?
154207
set -e
155208
if [[ "$success" = "0" ]]; then
156209
echo "Pulling from local registry"
157-
docker tag localhost:5000/ubuntu ubuntu:xenial-20201030
158-
docker pull localhost:5000/sdcflows
159210
docker tag localhost:5000/sdcflows nipreps/sdcflows:latest
160211
docker tag localhost:5000/sdcflows nipreps/sdcflows
161212
else
162213
echo "Pulling from Docker Hub"
163-
docker pull ubuntu:xenial-20201030
164-
docker tag ubuntu:xenial-20201030 localhost:5000/ubuntu
165-
docker push localhost:5000/ubuntu
166214
docker pull nipreps/sdcflows:latest
167215
fi
168216
- checkout:
169217
path: /tmp/src/sdcflows
170218
- run:
171219
name: Build Docker image
220+
working_directory: /tmp/src/sdcflows
172221
no_output_timeout: 60m
173222
command: |
174-
cd /tmp/src/sdcflows
175-
export PY3=$(pyenv versions | grep '3\.' |
176-
sed -e 's/.* 3\./3./' -e 's/ .*//')
223+
export PY3=$( pyenv versions | awk '/^\* 3/ { print $2 }' )
177224
pyenv local $PY3
178-
python3 -m pip install "setuptools ~= 45.0" "setuptools_scm >= 6.2" "pip>=10.0.1"
225+
python3 -m pip install --upgrade setuptools setuptools_scm pip
179226
180227
# Get version, update files.
181-
THISVERSION=$( python3 setup.py --version )
228+
THISVERSION=$( python3 -m setuptools_scm )
182229
if [[ ${THISVERSION:0:1} == "0" ]] ; then
183230
echo "WARNING: latest git tag could not be found"
184231
echo "Please, make sure you fetch all tags from upstream with"
@@ -194,6 +241,20 @@ jobs:
194241
--build-arg VERSION="${CIRCLE_TAG:-$THISVERSION}" . \
195242
| tee build-output.log
196243
echo "${CIRCLE_TAG:-$THISVERSION}" >> /tmp/.local-version.txt
244+
- run:
245+
name: Check Docker image
246+
working_directory: /tmp/src/sdcflows
247+
command: |
248+
export PY3=$( pyenv versions | awk '/^\* 3/ { print $2 }' )
249+
pyenv local $PY3
250+
# Get version, update files.
251+
THISVERSION=$( python3 -m setuptools_scm )
252+
BUILT_VERSION=$( docker run --rm --entrypoint=python nipreps/sdcflows:latest -c "import sdcflows; print(sdcflows.__version__)" )
253+
BUILT_VERSION=${BUILT_VERSION%$'\r'}
254+
echo "VERSION: \"$THISVERSION\""
255+
echo "BUILT: \"$BUILT_VERSION\""
256+
set -e
257+
test "$BUILT_VERSION" = "$THISVERSION"
197258
- run:
198259
name: Docker push to local registry
199260
no_output_timeout: 40m
@@ -209,16 +270,6 @@ jobs:
209270
key: build-v2-{{ .Branch }}-{{ epoch }}
210271
paths:
211272
- /tmp/docker
212-
- run:
213-
name: Check version packaged in Docker image
214-
command: |
215-
docker run --rm -v /tmp:/tmp -v /tmp/src/sdcflows/.circleci/version.py:/usr/share/version.py \
216-
--entrypoint=python nipreps/sdcflows /usr/share/version.py
217-
THISVERSION=$( head -n1 /tmp/.local-version.txt )
218-
INSTALLED_VERSION=$( head -n1 /tmp/.docker-version.txt )
219-
echo "VERSION: \"${THISVERSION}\""
220-
echo "INSTALLED: \"${INSTALLED_VERSION}\""
221-
test "${INSTALLED_VERSION}" = "${THISVERSION}"
222273

223274
- restore_cache:
224275
keys:
@@ -239,8 +290,8 @@ jobs:
239290
- workdir-v2-
240291
- run:
241292
name: Refreshing cached intermediate results
293+
working_directory: /tmp/src/sdcflows
242294
command: |
243-
cd /tmp/src/sdcflows
244295
COMMIT_MSG=$( git log --format=oneline -n 1 $CIRCLE_SHA1 )
245296
set +e
246297
do_refresh="$( echo "${COMMIT_MSG}" | grep -i -E '\[refresh[ _]?cache\]' )"
@@ -295,12 +346,11 @@ jobs:
295346

296347
- run:
297348
name: Submit unit test coverage
349+
working_directory: /tmp/src/sdcflows
298350
command: |
299-
export PY3=$(pyenv versions | grep '3\.' |
300-
sed -e 's/.* 3\./3./' -e 's/ .*//')
351+
export PY3=$( pyenv versions | awk '/^\* 3/ { print $2 }' )
301352
pyenv local $PY3
302353
python3 -m pip install codecov
303-
cd /tmp/src/sdcflows
304354
python3 -m codecov --file /tmp/tests/unittests.xml \
305355
--flags unittests -e CIRCLE_JOB
306356
@@ -326,8 +376,7 @@ jobs:
326376
command: |
327377
python -m venv /tmp/venv
328378
source /tmp/venv/bin/activate
329-
python -m pip install -U build "setuptools >= 45" wheel "setuptools_scm >= 6.2" \
330-
setuptools_scm_git_archive pip twine docutils
379+
python -m pip install -U pip setuptools_scm
331380
pip install --no-cache-dir -r docs/requirements.txt
332381
- run:
333382
name: Build only this commit
@@ -340,9 +389,7 @@ jobs:
340389
path: ~/docs/
341390

342391
deploy_docker:
343-
machine:
344-
image: ubuntu-2004:202107-02
345-
working_directory: /tmp/src/
392+
<<: *machine_defaults
346393
steps:
347394
- restore_cache:
348395
keys:
@@ -352,40 +399,30 @@ jobs:
352399
- build-v2-
353400
paths:
354401
- /tmp/docker
355-
- run:
356-
name: Set-up a Docker registry
357-
command: |
358-
docker run -d -p 5000:5000 --restart=always --name=registry \
359-
-v /tmp/docker:/var/lib/registry registry:2
360-
- run:
361-
name: Pull images from local registry
362-
command: |
363-
docker pull localhost:5000/sdcflows
364-
docker tag localhost:5000/sdcflows nipreps/sdcflows:latest
402+
- docker/install-docker-credential-helper
403+
- run: *docker_auth
404+
- run: *setup_docker_registry
405+
- run: *pull_from_registry
365406
- run:
366407
name: Deploy to Docker Hub
367408
no_output_timeout: 40m
368409
command: |
369410
if [[ -n "$DOCKER_PAT" ]]; then
370-
docker login -u $DOCKER_USER -p $DOCKER_PAT
371411
docker push nipreps/sdcflows:latest
372412
docker tag nipreps/sdcflows nipreps/sdcflows:$CIRCLE_TAG
373413
docker push nipreps/sdcflows:$CIRCLE_TAG
374414
fi
375415
376416
test_package:
377-
docker:
378-
- image: cimg/python:3.8.5
379-
working_directory: /tmp/src/sdcflows
417+
<<: *python_defaults
380418
steps:
381419
- checkout
382420
- run:
383421
name: Prepare environment & build
384422
command: |
385423
python -m venv /tmp/buildenv
386424
source /tmp/buildenv/bin/activate
387-
python -m pip install -U build "setuptools >= 45" wheel "setuptools_scm >= 6.2" \
388-
setuptools_scm_git_archive pip twine docutils
425+
python -m pip install -U build twine setuptools_scm
389426
python -m build -s -w
390427
python -m twine check dist/*
391428
- store_artifacts:
@@ -399,24 +436,20 @@ jobs:
399436
command: |
400437
source /tmp/buildenv/bin/activate
401438
THISVERSION=$( python -m setuptools_scm )
402-
python -m pip install dist/*.tar.gz
439+
python -m pip install dist/*.whl
403440
mkdir empty
404441
cd empty
405442
INSTALLED=$( python -c 'import sdcflows; print(sdcflows.__version__)' )
406443
test "${CIRCLE_TAG:-$THISVERSION}" == "$INSTALLED"
407444
408445
deploy_pypi:
409-
docker:
410-
- image: cimg/python:3.8.5
411-
working_directory: /tmp/src/sdcflows
446+
<<: *python_defaults
412447
steps:
413448
- attach_workspace:
414449
at: /tmp/src/sdcflows
415450
- run:
416451
name: Upload to Pypi
417452
command: |
418-
python -m venv /tmp/upload
419-
source /tmp/upload/bin/activate
420453
python -m pip install twine
421454
python -m twine check dist/*
422455
python -m twine upload dist/* --non-interactive

0 commit comments

Comments
 (0)