generated from opensafely-core/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 3
120 lines (107 loc) · 3.91 KB
/
build-and-deploy.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
name: Build and publish image
on:
create:
workflow_dispatch:
workflow_run:
workflows:
- CI
branches:
- main
types:
- completed
jobs:
build-and-publish-docker-image:
runs-on: ubuntu-latest
env:
image: ghcr.io/opensafely-core/ehrql
steps:
- uses: actions/checkout@v4
with:
# Required to get previous tags
fetch-depth: 0
# Find the latest tag by fetching the first 20 tags in descending date order,
# and finding the first one that starts with "v"
- name: Get latest version tag
id: latest-version-tag
uses: actions/github-script@v7
with:
script: |
const query = `query($owner:String!, $name:String!) {
repository(owner:$owner, name:$name) {
refs(refPrefix: "refs/tags/", first: 20, orderBy: {field: TAG_COMMIT_DATE, direction: DESC}) {
edges {
node {
name
target {
oid
... on Tag {
message
commitUrl
tagger {
name
email
date
}
}
}
}
}
}
}
}`;
const variables = {
owner: context.repo.owner,
name: context.repo.repo,
}
const result = await github.graphql(query, variables)
console.log(result.repository.refs.edges)
const latestVersionTagEdge = result.repository.refs.edges.find(
tag => tag.node.name.startsWith("v")
)
console.log(latestVersionTagEdge)
if(latestVersionTagEdge){
return latestVersionTagEdge.node.name
} else {
return "no-tag-found"
}
result-encoding: string
- name: Previous commit
id: previoustagcommit
# if we are on a tag, should look like `v0.6.0`
# if we are not on a tag, should look like `v0.6.0-24-gbf6352d`
run: echo "tag_describe=`git describe --tags`" >> "$GITHUB_OUTPUT"
- name: Verify whether we are on a tagged version commit, allows us to short-circuit this workflow
id: taggedcommit
if: ${{ steps.previoustagcommit.outputs.tag_describe == steps.latest-version-tag.outputs.result }}
run: echo "tag=y" >> "$GITHUB_OUTPUT"
# Verify that 'Get latest version tag' returned a version tag
- name: Fail if no version tag found
if: ${{ steps.latest-version-tag.outputs.result == 'no-tag-found' }}
run: |
/bin/false
- name: Install just
uses: opensafely-core/setup-action@v1
with:
install-just: true
# This relies on the tag having a version of the form vX.Y.Z
- name: Build image
if: ${{ startsWith(steps.taggedcommit.outputs.tag, 'y') }}
run: |
PATCH="${{ steps.latest-version-tag.outputs.result }}"
MAJOR="${PATCH%%.*}"
MINOR="${PATCH%.*}"
echo "MAJOR=$MAJOR"
echo "MINOR=$MINOR"
echo "PATCH=$PATCH"
echo "$PATCH" > ehrql/VERSION
just build-ehrql "ehrql" \
--tag ${{ env.image }}:$MAJOR \
--tag ${{ env.image }}:$MINOR \
--tag ${{ env.image }}:$PATCH \
- name: Log into GitHub Container Registry
if: ${{ startsWith(steps.taggedcommit.outputs.tag, 'y') }}
run: docker login https://ghcr.io -u ${{ github.actor }} --password ${{ secrets.GITHUB_TOKEN }}
- name: Push images to GitHub Container Registry
if: ${{ startsWith(steps.taggedcommit.outputs.tag, 'y') }}
run: |
docker push --all-tags ${{ env.image }}