This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
244 lines (217 loc) · 9.44 KB
/
create_release_draft.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
name: Create Release Draft
on:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: "0 3 * * 1-5" # run integration tests at 3 AM, monday to friday (1-5)
workflow_dispatch: # run only when triggered manually
inputs:
branch:
description: "Branch to create release draft from (e.g., master, release-x.y.z)"
required: true
default: "master"
defaults:
run:
shell: bash
jobs:
prepare-release:
name: "Prepare release"
runs-on: ubuntu-20.04
outputs:
BRANCH: ${{ steps.determine_branch.outputs.BRANCH }}
steps:
- name: Check out code.
uses: actions/[email protected]
- name: Create downloads folder
run: mkdir ~/downloads
- name: Cache downloads paths
uses: actions/cache@v3
with:
path: |
~/downloads
key: ${{ runner.os }}-${{ matrix.CLOUD_PROVIDER }}-downloads
- name: Determine Target Branch for Release draft
id: determine_branch
run: |
if [[ "${{ github.event.inputs.branch }}" != "" ]]; then
BRANCH=${{ github.event.inputs.branch }}
echo "Using $BRANCH as target branch for release draft"
else
echo "Determining branch based on what triggered this workflow"
if [[ "${GITHUB_REF}" == "refs/heads"* ]]; then
echo "This is a push to a local branch -> using branch name"
BRANCH=${GITHUB_REF#refs/heads/}
echo "Branch Name: $BRANCH"
else
if [[ "${GITHUB_REF}" == "refs/pull/"* ]]; then
# usually the format for PRs is: refs/pull/1234/merge
echo "::error::This is a Pull Request, and PRs are not supported yet"
exit 1
else
echo "::error This is neither a push, nor a PR, probably something else... Exiting"
exit 1
fi
fi
fi
echo "##[set-output name=BRANCH;]$(echo ${BRANCH})"
#######################################################################
# This job publishes a draft release based on the previous steps #
#######################################################################
publish-draft-release:
name: Publish Draft release
needs: prepare-release
runs-on: ubuntu-20.04
env:
BRANCH: ${{ needs.prepare-release.outputs.BRANCH }}
REPO_SLUG: "keptn-contrib/dynatrace-service"
steps:
- name: Check out code
uses: actions/[email protected]
- name: Debug - Output Branch
run: echo $BRANCH
- name: Load CI Environment from .ci_env
id: load_ci_env
uses: c-py/action-dotenv-to-setenv@v4
with:
env-file: .ci_env
- name: Find latest successful run ID
id: last_run_id
run: |
RUN_ID=$(curl -sLH 'Accept: application/vnd.github.v3+json' -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "api.github.com/repos/$REPO_SLUG/actions/workflows/CI.yml/runs" | jq --arg branch "$BRANCH" -c '[.workflow_runs[] | select( .conclusion == "success" and .head_branch == $branch)][0] | .id')
echo "::set-output name=RUN_ID::$RUN_ID"
- name: Download all artifacts from last successful build of specified branch
uses: dawidd6/[email protected]
with:
github_token: ${{secrets.GITHUB_TOKEN}}
# Required, workflow file name or ID
workflow: CI.yml
run_id: ${{ steps.last_run_id.outputs.RUN_ID }}
# Optional, directory where to extract artifact
path: ./dist
- name: Load Build-Config Environment from ./dist/build-config/build-config.env
id: load_build_env
uses: c-py/action-dotenv-to-setenv@v4
with:
env-file: ./dist/build-config/build-config.env
- name: Overwrite VERSION String for nightly builds
run: |
if [[ "$BRANCH" == "master" ]]; then
# use VERSION.DATETIME for the cli version (e.g., nightly build)
VERSION=${VERSION}.${DATETIME}
# overwrite VERSION
echo "VERSION=${VERSION}" >> $GITHUB_ENV
fi
- name: DEBUG Build-Config
run: |
echo VERSION=${VERSION}
echo BRANCH=${BRANCH}
# Part of this job is to check if a releasenotes file exists and to use it as the release message
- name: Try getting release notes
id: get_releasenotes
run: |
filename="releasenotes/releasenotes_V${VERSION}.md"
if [[ -f "$filename" ]]; then
echo "Found releasenotes in $filename"
else
echo "::warning Could not find release notes $filename, using releasenotes_develop"
filename="releasenotes/releasenotes_develop.md"
fi
echo "##[set-output name=RELEASENOTES_FILENAME;]$(echo ${filename})"
# Create release message by concatenating releasenotes with the test/build report
- name: Create a Release message
env:
RELEASENOTES_FILENAME: ${{ steps.get_releasenotes.outputs.RELEASENOTES_FILENAME }}
run: |
# print CI information
echo "" >> release-notes.txt
echo "<details><summary>CI Information</summary>" >> release-notes.txt
echo "<p>" >> release-notes.txt
echo "" >> release-notes.txt
echo "* **BUILD ${VERSION} ON ${BRANCH}**" >> release-notes.txt
echo "* **DATETIME**: $DATETIME" >> release-notes.txt
echo "* **GIT_SHA**: $GIT_SHA" >> release-notes.txt
echo "* **ACTION RUN**: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" >> release-notes.txt
echo "" >> release-notes.txt
echo "" >> release-notes.txt
echo "" >> release-notes.txt
echo "</p>" >> release-notes.txt
echo "</details>" >> release-notes.txt
- name: Abort if (published) release already exists
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
let tag = "${{ env.VERSION }}";
console.log(`Trying to fetch release by tag '${tag}'...`);
try {
release = await github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: tag
});
console.log("::error Release already exists... Aborting!");
core.setFailed("Release already exists... Aborting!");
return 1; // failure
} catch (error) {
// release not found -> okay
console.log("No release found -> okay");
return 0;
}
- name: Delete draft release if it already exists
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
let tag = "${{ env.VERSION }}";
console.log(`Trying to fetch DRAFT release by tag '${tag}'...`);
try {
// get all releases (including drafts)
let releases = await github.rest.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo
});
// find a draft release
let release = releases.data.find(release => release.tag_name === tag);
if (release) {
// found a release with specified tag; check if it is a draft release
if (release.draft) {
// found a draft release
console.log(`🥳 Found a draft release with id ${release.id}, deleting it`);
console.log(`Release Tag Name: ${release.tag_name}`);
console.log(`Release Draft: ${release.draft}`);
console.log(`Release URL: ${release.html_url}`);
console.log(`::error::Draft release found, please delete it manually!`);
return 1;
// ToDo: Commented out as there seems to be a problem with deleting releases
/*github.repos.deleteRelease({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release.id
});*/
console.log("Deleted! -> okay");
} else {
console.log("::error Release with tag found, but it's not a draft release");
core.setFailed("Release with tag found, but it's not a draft release");
}
} else {
console.log("No draft release found -> okay");
}
return 0;
} catch (error) {
// error happened
console.log("An error happened while trying to fetch all releases:");
console.log(error);
throw error;
}
- name: Draft Release with Releasenotes
id: create_draft_release
uses: softprops/action-gh-release@v1
with:
name: ${{ env.VERSION }}
tag_name: ${{ env.VERSION }}
draft: true # we only want to publish draft releases here - a human should review it and push the button eventually
prerelease: true # mark as pre-release for now, a human can always change it to a non-prerelease afterwards
body_path: release-notes.txt
files: |
dist/installer/*.tgz
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}