-
Notifications
You must be signed in to change notification settings - Fork 8
211 lines (197 loc) · 7.87 KB
/
cd.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Description
# ===========
# This workflow is triggered each time the CI workflow succeed
# on develop.
# It looks for the first milestone that is completed.
# It must have a title matching the pattern '[0-9]+\.[0-9]+\.[0-9]+'.
# It then merges the develop branch into the master branch
# and tags the master branch with the title of the milestone.
# Finally, it creates a new Release on GitHub where it writes an
# automatically generated changelog.
# It also close the milestone (which is 100% completed).
---
name: CD
# This workflow is triggered when the CI workflow ends
on:
workflow_run:
workflows: ["CI"]
branches: [develop]
types:
- completed
# Variables to configure the workflow
env:
DOCKERFILE_PATH: '.'
DOCKERFILE_FILENAME: 'Dockerfile'
DOCKER_IMAGE_NAME: 'lequal/sonarqube-catlab'
jobs:
# Job to test if a new version is ready to be released
deliverability:
name: Check if a new version must be released
runs-on: ubuntu-20.04
# The job is run only if the CI workflow succeeded
if: github.event.workflow_run.conclusion == 'success'
outputs:
delivery: ${{ steps.version.outputs.delivery }}
version: ${{ steps.version.outputs.version }}
milestone_number: ${{ steps.version.outputs.milestone_number }}
steps:
# Get the version number from the milestone title and that it follows semver
- name: Get and check version
id: version
run: |
milestones=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
https://api.github.com/repos/${{ github.repository }}/milestones \
| jq -r '. | map(select(.open_issues == 0 and .closed_issues > 0 and .state == "open"))')
if [ "$milestones" == "[]" ]
then
echo No milestone completed, not delivering
echo "delivery=forbidden" >> $GITHUB_OUTPUT
exit 0
fi
echo -e "Completed milestones:\n$milestones"
version=$(echo "$milestones" | jq -r '.[0].title')
milestone_number=$(echo "$milestones" | jq -r '.[0].number')
echo Milestone $milestone_number has title $version
echo Checking milestone title is a version number
if ! [[ $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]
then
echo "Malformed milestone title: $version. It must match [0-9]+.[0-9]+.[0-9]+"
exit 1
fi
echo Ready to deliver
echo "version=$version" >> $GITHUB_OUTPUT
echo "milestone_number=$milestone_number" >> $GITHUB_OUTPUT
echo "delivery=allowed" >> $GITHUB_OUTPUT
release:
name: Release a new version of the docker image
runs-on: ubuntu-22.04
needs: deliverability
if: needs.deliverability.outputs.delivery == 'allowed'
steps:
# git clone
- uses: actions/checkout@v4
with:
fetch-depth: 0
# Merge develop into master (fast forward) and tag master
- name: Merge and tag
run: |
git config --global user.name "GitHub Actions"
git config --global user.email [email protected]
git checkout --track origin/master
git merge origin/develop
git tag -a ${{ needs.deliverability.outputs.version }} \
-m "Release ${{ needs.deliverability.outputs.version }}"
git push origin master --follow-tags
# Get the tag of the last release
- name: Get tag of last release
id: last_release
run: |
tags=($(git tag --sort="-v:refname" | grep -P -e '[0-9]+\.[0-9]+\.[0-9]+'))
echo The tag of the previous release is ${tags[1]}
echo "tag=${tags[1]}" >> $GITHUB_OUTPUT
# Generate the changelog since last release
- name: Generate the changelog
uses: charmixer/auto-changelog-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
since_tag: ${{ steps.last_release.outputs.tag }}
# Append a link to the image on Docker Hub to the changelog
- name: Append link to Docker Hub
run: |
cat CHANGELOG.md > changelog.md
echo -e "\nView the image on" \
"[DockerHub](https://hub.docker.com/r/$DOCKER_IMAGE_NAME)." \
>> changelog.md
# Create a new release on GitHub
- name: Create GitHub Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ needs.deliverability.outputs.version }}
release_name: ${{ env.DOCKER_IMAGE_NAME }}:${{ needs.deliverability.outputs.version }}
body_path: changelog.md
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-20.04
needs: deliverability
if: needs.deliverability.outputs.delivery == 'allowed'
steps:
- name: Check out the repo
uses: actions/checkout@v4
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.DOCKER_IMAGE_NAME }}
#Push with current name and version number tag
- name: Build and push Docker image to version number
uses: docker/build-push-action@v5
with:
context: .
file: ${{ env.DOCKERFILE_PATH }}/${{ env.DOCKERFILE_FILENAME }}
push: true
tags: ${{ env.DOCKER_IMAGE_NAME}}:${{ needs.deliverability.outputs.version }}
labels: ${{ steps.meta.outputs.labels }}
#Push with current name and latest tag
- name: Build and push Docker image to lastest repo
uses: docker/build-push-action@v5
with:
context: .
file: ${{ env.DOCKERFILE_PATH }}/${{ env.DOCKERFILE_FILENAME }}
push: true
tags: ${{ env.DOCKER_IMAGE_NAME }}:latest
labels: ${{ steps.meta.outputs.labels }}
#Push description to current Docker Hub
- name: Docker Hub Description
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
repository: ${{ env.DOCKER_IMAGE_NAME }}
#Push with old name and version number tag
- name: Build and push Docker image to version number
uses: docker/build-push-action@v5
with:
context: .
file: ${{ env.DOCKERFILE_PATH }}/${{ env.DOCKERFILE_FILENAME }}
push: true
tags: lequal/sonarqube:${{ needs.deliverability.outputs.version }}
labels: ${{ steps.meta.outputs.labels }}
#Push with old name and latest tag
- name: Build and push Docker image to lastest repo
uses: docker/build-push-action@v5
with:
context: .
file: ${{ env.DOCKERFILE_PATH }}/${{ env.DOCKERFILE_FILENAME }}
push: true
tags: lequal/sonarqube:latest
labels: ${{ steps.meta.outputs.labels }}
#Push description to old Docker Hub
- name: Docker Hub Description
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
repository: lequal/sonarqube
close_milestone:
name: Close milestone
runs-on: ubuntu-20.04
needs:
- deliverability
- release
- push_to_registry
steps:
# Close the milestone (that is now 100% completed)
- name: Close milestone
run: |
curl --request PATCH \
--url https://api.github.com/repos/${GITHUB_REPOSITORY}/milestones/${{ needs.deliverability.outputs.milestone_number }} \
--header "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
--header 'Content-Type: application/json' \
--data '{"state":"closed"}'