-
Notifications
You must be signed in to change notification settings - Fork 3
137 lines (121 loc) · 5.3 KB
/
update-jira-tickets.yaml
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
name: Update Jira Tickets
on:
release:
types: [published]
# Allows running manually from the Actions tab
workflow_dispatch:
inputs:
tagName:
description: "Tag Name"
required: true
jobs:
process-release:
env:
JENKINS_USER: ${{ secrets.JENKINS_USER }}
JENKINS_TOKEN: ${{ secrets.JENKINS_TOKEN }}
JIRA_WEBHOOK_URL: ${{ secrets.JIRA_WEBHOOK_URL }}
JENKINS_HOST: ${{ vars.JENKINS_HOST }}
# The max amount of time (in minutes) we should wait for the current Jenkins build to finish. Defaults to 6 hours
JENKINS_BUILD_MAX_WAIT_MINS: ${{ vars.JENKINS_BUILD_MAX_WAIT_MINS || 360 }}
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Get current tag
id: get-tag
run: |
if [ "${{ github.event_name }}" == "release" ]; then
currentTag=${GITHUB_REF#refs/tags/}
else
currentTag="${{ github.event.inputs.tagName }}"
fi
echo "Tag: ${currentTag}"
echo "tag=${currentTag}" >> $GITHUB_OUTPUT
- name: Validate tag
run: |
git fetch -q --tags
if ! git tag --list | grep -qx "${{ steps.get-tag.outputs.tag }}"; then
echo "Not a valid tag, ending workflow"
exit 1
fi
- name: Check if tag is a release candidate
run: |
if [[ ${{ steps.get-tag.outputs.tag }} != *"-rc"* ]]; then
echo "Not a release candidate, ending workflow."
exit 1
fi
- name: Ping Jenkins for previous successful tag
id: ping-jenkins
run: |
repoName=${GITHUB_REPOSITORY##*/}
currentTag="${{ steps.get-tag.outputs.tag }}"
git fetch -q --tags
rcTags=$(git tag --sort=-creatordate | grep -- -rc)
started=false
previousTag=""
for tagName in $rcTags
do
# Skip tags more recent than the current one
if [ "$tagName" != "$currentTag" ] && [ "$started" = false ]; then
continue;
elif [ "$tagName" == "$currentTag" ]; then
started=true;
continue;
fi
jenkinsUrl="${JENKINS_HOST}/job/${repoName}/view/tags/job/${tagName}/lastSuccessfulBuild"
echo "Checking for successful Jenkins build for GitHub tag ${tagName} at ${jenkinsUrl}"
response=$(curl -o /dev/null --silent -w "%{http_code}\n" -u "${JENKINS_USER}:${JENKINS_TOKEN}" "${jenkinsUrl}/api/json")
echo "Response: ${response}"
if [ "$response" == "200" ]; then
echo "Found successful Jenkins build for GitHub tag ${tagName}: ${jenkinsUrl}"
previousTag="${tagName}"
break
fi
done
if [ "${previousTag}" == "" ]; then
echo "No successful Jenkins builds found for any previous tags. Ending workflow"
exit 1
else
echo "tagName=${previousTag}" >> $GITHUB_OUTPUT
fi
- name: Determine Jira tickets from commit messages
id: jira-tickets
run: |
currentTag="${{ steps.get-tag.outputs.tag }}"
previousTag="${{ steps.ping-jenkins.outputs.tagName }}"
git fetch --unshallow
commitMessages=$(git log --pretty=%B $previousTag..$currentTag)
echo "Commit messages since the last release: ${commitMessages}"
jiraTickets=$(echo "$commitMessages" | grep -io 'M2-[0-9]\+' | tr '[:lower:]' '[:upper:]' | sort | uniq | tr '\n' ' ')
echo "Jira tickets since the last release: ${jiraTickets}"
echo "tickets=${jiraTickets}" >> $GITHUB_OUTPUT
- name: Periodically ping Jenkins for current tag build status
env:
REPO_URL: "${{ github.server_url }}/${{ github.repository }}"
run: |
repoName=${GITHUB_REPOSITORY##*/}
currentTag="${{ steps.get-tag.outputs.tag }}"
echo "Waiting for current build to finish.."
start_time=$(date +%s)
while true; do
current_time=$(date +%s)
elapsed_time_mins=$(( (current_time - start_time) / 60 ))
# Break out of the loop if we've been waiting longer than 6 hours
if [ $elapsed_time_mins -ge $JENKINS_BUILD_MAX_WAIT_MINS ]; then
echo "Timed out waiting for build to finish"
exit 1
fi
echo -n "."
result=$(curl --silent -u "${JENKINS_USER}:${JENKINS_TOKEN}" --connect-timeout 10 -m 10 "${JENKINS_HOST}/job/${repoName}/view/tags/job/${currentTag}/lastBuild/api/json" | jq -r .result)
if [[ "$result" == "SUCCESS" ]]; then
echo "Build successful! Submitting ticket numbers to Jira"
tickets="${{ steps.jira-tickets.outputs.tickets }}"
json="{ \"issues\": $(echo "${tickets}" | jq -R -s -c 'split(" ")[:-1]'), \"data\": { \"tag\": \"${currentTag}\", \"repository\": \"${REPO_URL}\" } }"
curl -X POST -H 'Content-Type: application/json' --url "${JIRA_WEBHOOK_URL}" --data "$json"
break
elif [[ "$result" != "null" ]]; then
echo "Build failed, ending workflow"
exit 1
fi
sleep 60
done