Skip to content

Commit 883156f

Browse files
authored
Merge pull request #363 from The-Strategy-Unit/switch_to_uv
Switch to uv
2 parents a262117 + 7ca4870 commit 883156f

20 files changed

+3177
-188
lines changed

.github/workflows/build_container.yaml

Lines changed: 92 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,71 @@ on:
3030
required: true
3131

3232
jobs:
33-
build-and-deploy-ghcr:
33+
34+
build-container:
3435
runs-on: ubuntu-latest
36+
steps:
37+
- name: "Checkout GitHub Action"
38+
uses: actions/checkout@v4
39+
40+
- name: "Login to GitHub Container Registry"
41+
uses: docker/login-action@v3
42+
with:
43+
registry: ghcr.io
44+
username: ${{github.actor}}
45+
password: ${{secrets.GITHUB_TOKEN}}
46+
47+
- name: Set up Docker Buildx
48+
uses: docker/setup-buildx-action@v3
49+
50+
- name: "Build image"
51+
uses: docker/build-push-action@v6
52+
with:
53+
context: .
54+
tags: ghcr.io/the-strategy-unit/nhp_model:pr-${{ github.event.number }}
55+
push: true
56+
cache-from: type=gha
57+
cache-to: type=gha,mode=max
58+
build-args: |
59+
app_version=${{ inputs.app-version }}
60+
data_version=${{ inputs.data-version }}
61+
62+
add-comment-to-pr:
63+
runs-on: ubuntu-latest
64+
if: github.event_name == 'pull_request'
65+
needs: ["build-container"]
66+
steps:
67+
- name: Find Comment
68+
uses: peter-evans/find-comment@v3
69+
id: fc
70+
with:
71+
issue-number: ${{ github.event.pull_request.number }}
72+
comment-author: 'github-actions[bot]'
73+
body-includes: "## ✅ A new build is available"
74+
75+
- name: Comment with container image link
76+
if: github.event_name == 'pull_request'
77+
uses: peter-evans/create-or-update-comment@v4
78+
with:
79+
token: ${{ secrets.GITHUB_TOKEN }}
80+
comment-id: ${{ steps.fc.outputs.comment-id }}
81+
issue-number: ${{ github.event.pull_request.number }}
82+
body: |
83+
## ✅ A new build is available.
84+
85+
You can use the following to use pull the image into your local environment:
86+
87+
``` bash
88+
docker pull ghcr.io/the-strategy-unit/nhp_model:pr-${{ github.event.number }}
89+
```
90+
91+
[Download Artifact](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
92+
edit-mode: replace
93+
94+
deploy-ghcr:
95+
runs-on: ubuntu-latest
96+
if: github.event_name != 'pull_request'
97+
needs: ["build-container"]
3598
steps:
3699
- name: 'Checkout GitHub Action'
37100
uses: actions/checkout@v4
@@ -42,29 +105,30 @@ jobs:
42105
registry: ghcr.io
43106
username: ${{github.actor}}
44107
password: ${{secrets.GITHUB_TOKEN}}
108+
- name: Set up Docker Buildx
109+
uses: docker/setup-buildx-action@v3
45110

46-
- name: 'Pull latest dev container'
47-
run: |
48-
docker pull ghcr.io/the-strategy-unit/nhp_model:dev
49-
50-
- name: 'Build image'
51-
run: |
52-
docker build . -t ghcr.io/the-strategy-unit/nhp_model:${{ inputs.docker-tag }} \
53-
--build-arg app_version=${{ inputs.app-version }} \
54-
--build-arg data_version=${{ inputs.data-version }}
55-
56-
- name: 'Push image'
57-
run: |
58-
docker push ghcr.io/the-strategy-unit/nhp_model:${{ inputs.docker-tag }}
111+
- name: "Build image"
112+
uses: docker/build-push-action@v6
113+
with:
114+
context: .
115+
tags: ghcr.io/the-strategy-unit/nhp_model:${{ inputs.docker-tag }}
116+
push: true
117+
cache-from: type=gha
118+
build-args: |
119+
app_version=${{ inputs.app-version }}
120+
data_version=${{ inputs.data-version }}
59121
60122
- name: 'Push latest'
61123
if: ${{ inputs.latest }}
62124
run: |
63125
docker tag ghcr.io/the-strategy-unit/nhp_model:${{ inputs.docker-tag }} ghcr.io/the-strategy-unit/nhp_model:latest
64126
docker push ghcr.io/the-strategy-unit/nhp_model:latest
65127
66-
build-and-deploy-acr:
128+
deploy-acr:
67129
runs-on: ubuntu-latest
130+
if: github.event_name != 'pull_request'
131+
needs: ["build-container"]
68132
steps:
69133
- name: 'Checkout GitHub Action'
70134
uses: actions/checkout@v4
@@ -81,22 +145,20 @@ jobs:
81145
username: ${{ secrets.REGISTRY_USERNAME }}
82146
password: ${{ secrets.REGISTRY_PASSWORD }}
83147

84-
- name: 'Pull latest dev container'
85-
run: |
86-
docker pull ${{ secrets.REGISTRY_LOGIN_SERVER }}/nhp_model:dev
87-
88-
- name: 'Build and push image'
89-
run: |
90-
# include the storage account env var for the data for acr
91-
docker build . -t ${{ secrets.REGISTRY_LOGIN_SERVER }}/nhp_model:${{ inputs.docker-tag }} \
92-
--build-arg app_version=${{ inputs.app-version }} \
93-
--build-arg data_version=${{ inputs.data-version }} \
94-
--build-arg storage_account=${{ secrets.NHP_STORAGE_ACCOUNT }}
95-
96-
- name: 'Push image'
97-
run: |
98-
docker push ${{ secrets.REGISTRY_LOGIN_SERVER }}/nhp_model:${{ inputs.docker-tag }}
148+
- name: Set up Docker Buildx
149+
uses: docker/setup-buildx-action@v3
99150

151+
- name: "Build image"
152+
uses: docker/build-push-action@v6
153+
with:
154+
context: .
155+
tags: ${{ secrets.REGISTRY_LOGIN_SERVER }}/nhp_model:${{ inputs.docker-tag }}
156+
push: true
157+
cache-from: type=gha
158+
build-args: |
159+
app_version=${{ inputs.app-version }}
160+
data_version=${{ inputs.data-version }}
161+
storage_account=${{ secrets.NHP_STORAGE_ACCOUNT }}
100162
- name: 'Push latest'
101163
if: ${{ inputs.latest }}
102164
run: |

.github/workflows/codecov.yaml

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,17 @@ jobs:
1717
steps:
1818
- uses: actions/checkout@v4
1919

20-
- name: Setup Mambaforge
21-
uses: conda-incubator/setup-miniconda@v3
20+
- name: Install the latest version of uv
21+
uses: astral-sh/setup-uv@v6
2222
with:
23-
miniforge-version: latest
24-
activate-environment: nhp
25-
use-mamba: true
23+
version: "latest"
24+
activate-environment: true
2625

27-
- name: Cache Conda env
28-
uses: actions/cache@v4
29-
id: cache
30-
with:
31-
path: ${{ env.CONDA }}/envs
32-
key: conda-${{ hashFiles('environment.yml') }}-${{ env.CACHE_NUMBER }}
33-
env:
34-
# Increase this value to reset cache if etc/example-environment.yml has not changed
35-
CACHE_NUMBER: 0
36-
37-
- name: Update environment
38-
run: mamba env update -n nhp -f environment.yml
39-
if: steps.cache.outputs.cache-hit != 'true'
26+
- name: Install dependencies
27+
run: uv pip install -e ".[dev]"
4028

4129
- name: Generate Report
42-
run: pytest --cov=. tests/ --ignore=tests --cov-report xml:coverage.xml
30+
run: uv run pytest --cov=. tests/ --ignore=tests --cov-branch --cov-report xml:coverage.xml
4331

4432
- name: Upload Coverage to Codecov
4533
uses: codecov/codecov-action@v4

.github/workflows/deploy_dev.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
on:
2+
pull_request:
3+
types: [opened, synchronize, reopened]
24
push:
35
branches:
46
- main
@@ -18,6 +20,7 @@ jobs:
1820

1921
deploy-dev-schema:
2022
uses: ./.github/workflows/build_schema.yaml
23+
if: github.event_name == 'push'
2124
with:
2225
schema-tag: dev
2326
secrets: inherit
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Remove PR container image
2+
3+
on:
4+
pull_request:
5+
types:
6+
- closed
7+
8+
jobs:
9+
remove-pr-image:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
packages: write
13+
contents: read
14+
steps:
15+
- name: "Remove pr image"
16+
env:
17+
TAG_TO_DELETE: "pr-${{ github.event.pull_request.number }}"
18+
run: |
19+
VERSION_ID=$(gh api /orgs/the-strategy-unit/packages/container/nhp_model/versions \
20+
-H "Accept: application/vnd.github+json" \
21+
--paginate | \
22+
jq -r '.[] | select(.metadata.container.tags[] == "$TAG_TO_DELETE") | .id')
23+
24+
if [ -n "$VERSION_ID" ]; then
25+
echo "Deleting version ID: $VERSION_ID"
26+
gh api \
27+
-X DELETE \
28+
/orgs/the-strategy-unit/packages/container/nhp_model/versions/${VERSION_ID} \
29+
-H "Accept: application/vnd.github+json"
30+
else
31+
echo "Tag '$TAG_TO_DELETE' not found — skipping delete"
32+
fi
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Clean up untagged container images
2+
3+
on:
4+
workflow_dispatch: # allows manual triggering via GitHub UI
5+
schedule:
6+
- cron: '0 1 * * *' # runs at 01:00 UTC every day
7+
8+
jobs:
9+
removed-untagged-images:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
packages: write
13+
contents: read
14+
steps:
15+
- name: "Remove untagged images"
16+
run: |
17+
VERSION_IDS=$(gh api /orgs/the-strategy-unit/packages/container/nhp_model/versions \
18+
-H "Accept: application/vnd.github+json" \
19+
--paginate | \
20+
jq -r '.[] | select(.metadata.container.tags | length == 0) | .id')
21+
22+
for VERSION_ID in $VERSION_IDS; do
23+
echo "Deleting version ID: $VERSION_ID"
24+
gh api \
25+
-X DELETE \
26+
/orgs/the-strategy-unit/packages/container/nhp_model/versions/${VERSION_ID} \
27+
-H "Accept: application/vnd.github+json"
28+
done

0 commit comments

Comments
 (0)