-
Notifications
You must be signed in to change notification settings - Fork 0
120 lines (108 loc) · 4.13 KB
/
auto-release.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: Auto Release
on:
push:
tags:
- 'v*'
permissions:
contents: write
jobs:
create-release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Get tag name
id: get_tag
shell: bash
run: echo "TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
- name: Get base version
id: get_base_version
shell: bash
run: |
BASE_VERSION=$(echo "${{ steps.get_tag.outputs.TAG }}" | cut -d. -f1)
echo "BASE_VERSION=${BASE_VERSION}" >> $GITHUB_OUTPUT
- name: Get previous tag
id: get_previous_tag
shell: bash
run: |
PREVIOUS_TAG=$(git describe --tags --abbrev=0 "${{ steps.get_tag.outputs.TAG }}"^ || echo "")
echo "PREVIOUS_TAG=${PREVIOUS_TAG}" >> $GITHUB_OUTPUT
- name: Generate changelog
id: generate_changelog
shell: bash
run: |
if [ -n "${{ steps.get_previous_tag.outputs.PREVIOUS_TAG }}" ]; then
CHANGELOG=$(git log --pretty=format:"- %s" "${{ steps.get_previous_tag.outputs.PREVIOUS_TAG }}..${{ steps.get_tag.outputs.TAG }}")
else
CHANGELOG=$(git log --pretty=format:"- %s" "${{ steps.get_tag.outputs.TAG }}")
fi
echo "CHANGELOG<<EOF" >> $GITHUB_OUTPUT
echo "$CHANGELOG" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create or update base version tag
shell: bash
run: |
git config user.name "${{ github.actor }}"
git config user.email "${{ github.actor }}@users.noreply.github.com"
git tag -f "${{ steps.get_base_version.outputs.BASE_VERSION }}" "${GITHUB_SHA}"
git push origin --force "${{ steps.get_base_version.outputs.BASE_VERSION }}"
- name: Create or Update Base Release
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const baseVersion = `${{ steps.get_base_version.outputs.BASE_VERSION }}`;
const fullVersion = `${{ steps.get_tag.outputs.TAG }}`;
const changelog = `${{ steps.generate_changelog.outputs.CHANGELOG }}`;
const owner = context.repo.owner;
const repo = context.repo.repo;
const tagName = baseVersion;
try {
// Try to get the existing release
const { data: release } = await github.rest.repos.getReleaseByTag({
owner,
repo,
tag: tagName
});
// If it exists, update it
await github.rest.repos.updateRelease({
owner,
repo,
release_id: release.id,
tag_name: tagName,
name: `Base Release ${baseVersion}`,
body: `This is the base release for ${baseVersion}. Latest version: ${fullVersion}\n\nChangelog:\n${changelog}`,
draft: false,
prerelease: false
});
} catch (error) {
if (error.status === 404) {
// If it doesn't exist, create it
await github.rest.repos.createRelease({
owner,
repo,
tag_name: tagName,
name: `Base Release ${baseVersion}`,
body: `This is the base release for ${baseVersion}. Latest version: ${fullVersion}\n\nChangelog:\n${changelog}`,
draft: false,
prerelease: false
});
} else {
throw error;
}
}
- name: Create Full Version Release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ steps.get_tag.outputs.TAG }}
name: Release ${{ steps.get_tag.outputs.TAG }}
body: |
Changes in this Release:
${{ steps.generate_changelog.outputs.CHANGELOG }}
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}