-
Notifications
You must be signed in to change notification settings - Fork 8
152 lines (132 loc) · 6.58 KB
/
release-publish.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
# This workflow is triggered when a pull request is merged and the label 'release' is present.
# It fetches the last draft release, updates it to a non-draft release and sends a Slack message with the release notes.
name: Publish Release
on:
pull_request:
types:
- closed
jobs:
release:
if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'release')
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Install taskfile.dev
uses: arduino/setup-task@v2
with:
version: 3.x
repo-token: ${{ github.token }}
- name: Create release zip file
shell: bash
run: |
task dist
- name: Fetch last draft release
id: fetch-release-draft
shell: bash
run: |
# Call Github releases API and filter draft releases
DRAFT_RELEASE=$(curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}" \
https://api.github.com/repos/${{ github.repository }}/releases | \
jq 'map(select(.draft == true))' \
)
# We should find exactly 1 draft release to be published
if [[ $(echo $DRAFT_RELEASE | jq 'length') -eq 1 ]]
then
echo "Draft release found"
DRAFT_RELEASE=$(echo $DRAFT_RELEASE | jq first)
else
# Fail if more than 1 draft release is found
if [[ $(echo $DRAFT_RELEASE | jq 'length') -gt 1 ]]
then
echo "Unable to publish the release: More than one draft is found"
exit 1
fi
# If no draft release is found, maybe the draft has been manually published by error
# prior to the workflow execution
echo "No draft release found, checking for the latest release..."
LATEST_RELEASE=$(curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}" \
https://api.github.com/repos/${{ github.repository }}/releases/latest
)
# Compare the latest release version with the version in the branch name of the PR
# (which should be release/<version> or hotfix/<version>)
# If they match, we consider the latest release as the draft release
VERSION_FROM_LATEST_RELEASE=$(echo $LATEST_RELEASE | jq -r '.name')
echo "Latest release version: $VERSION_FROM_LATEST_RELEASE"
VERSION_FROM_PR_BRANCH_NAME=$(echo ${{ github.event.pull_request.head.ref }} | sed -E 's/(release|hotfix)\///')
echo "Version found from PR branch name: $VERSION_FROM_PR_BRANCH_NAME"
if [[ "$VERSION_FROM_LATEST_RELEASE" == "$VERSION_FROM_PR_BRANCH_NAME" ]]
then
echo "Version from the latest release matches with the version found in the release/hotfix PR branch name"
DRAFT_RELEASE=$LATEST_RELEASE
else
echo "Unable to publish the release: Latest release does not match with the version found in the PR branch name"
exit 1
fi
fi
# Retrieve name, id and body of the draft release
NAME=$(echo $DRAFT_RELEASE | jq -r '.name')
ID=$(echo $DRAFT_RELEASE | jq '.id')
BODY=$(echo $DRAFT_RELEASE | jq -r '.body')
# Add URLs to GitHub pull requests
PULL_REQUEST_URL_START=${{ github.server_url }}/${{ github.repository }}/pull/
ESCAPED_PULL_REQUEST_URL_START=$(printf '%s\n' "$PULL_REQUEST_URL_START" | sed -e 's/[\/&]/\\&/g')
BODY=$(echo -e "$BODY" | sed -E "s/#([0-9]+)/[#\1](${ESCAPED_PULL_REQUEST_URL_START}\1)/g")
# Add URLs to GitHub profiles
PROFILE_URL_START=${{ github.server_url }}/
ESCAPED_PROFILE_URL_START=$(printf '%s\n' "$PROFILE_URL_START" | sed -e 's/[\/&]/\\&/g')
BODY=$(echo -e "$BODY" | sed -E "s/@([[:alnum:]-]+)/[@\1](${ESCAPED_PROFILE_URL_START}\1)/g")
# Write the output variables
echo "name=$NAME" >> $GITHUB_OUTPUT
echo "id=$ID" >> $GITHUB_OUTPUT
echo "body<<EOF" >> $GITHUB_OUTPUT
echo -e "$BODY" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Add zip file to the release assets
shell: bash
run: |
curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}" \
-H "Content-Type: application/zip" \
-T "dist/alma.zip" \
https://uploads.github.com/repos/${{ github.repository }}/releases/${{ steps.fetch-release-draft.outputs.id }}/assets?name=alma.zip
- name: Publish Github release
uses: actions/github-script@v7
with:
# target_commitish is set to refs/heads/develop by release-drafter as we need to retrieve pull requests merged into develop
# We need to override it to refs/heads/main to point to the last commit of main branch instead of develop branch
script: |
const { owner, repo } = context.repo;
await github.rest.repos.updateRelease({
owner,
repo,
release_id: "${{ steps.fetch-release-draft.outputs.id }}",
draft: false,
make_latest: true,
tag_name: "${{ steps.fetch-release-draft.outputs.name }}",
target_commitish: "refs/heads/main"
});
- name: Format release notes for Slack
uses: LoveToKnow/[email protected]
id: slack-markdown-release-notes
with:
text: |
:tada: New publication of PrestaShop Alma module, **[${{ steps.fetch-release-draft.outputs.name }}](https://github.com/${{ github.repository }}/releases/tag/${{ steps.fetch-release-draft.outputs.name }})**:
${{ steps.fetch-release-draft.outputs.body }}
:warning: This release will be available on PrestaShop marketplace in a few hours
cc <@france.berut> <@khadija.cherif>
- name: Send changelog to Slack
uses: slackapi/[email protected]
with:
method: chat.postMessage
token: ${{ secrets.SLACK_RELEASE_CHANGELOG_BOT_TOKEN }}
payload: |
channel: CR9C57YM6
username: "${{ github.event.sender.login }}"
icon_url: "${{ github.event.sender.avatar_url }}"
text: ${{ toJson(steps.slack-markdown-release-notes.outputs.text) }}