This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
146 lines (140 loc) · 5.51 KB
/
on-merge.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
name: On merge
on:
pull_request:
branches: [main]
types: [closed]
jobs:
publish:
name: 'create version and deploy'
runs-on: ubuntu-latest
if: >
github.event.pull_request.merged == true && (
contains(github.event.pull_request.labels.*.name, 'Action: patch bump')
|| contains(github.event.pull_request.labels.*.name, 'Action: minor bump')
|| contains(github.event.pull_request.labels.*.name, 'Action: major bump')
|| contains(github.event.pull_request.labels.*.name, 'Action: beta bump')
)
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
ref: main
token: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/setup-node@v3
with:
node-verion: '12.x'
- name: Git config
run: |
git config user.name "CI Grafbase"
git config user.email "[email protected]"
- name: Extract version label
id: extract-version-label
uses: actions/github-script@v6
with:
github-token: ${{ secrets.DEPLOY_TOKEN }}
script: |
const versionLabels = {
'Action: patch bump': 'patch',
'Action: minor bump': 'minor',
'Action: major bump': 'major',
'Action: beta bump': 'beta',
};
const versionLabelsNames = Object.keys(versionLabels);
const labels = context.payload.pull_request.labels;
const versionLabelsPresent = labels
.filter(label => versionLabelsNames.includes(label.name))
console.log(`::debug ::${versionLabelsPresent.length} matching labels`);
if (versionLabelsPresent.length > 1) {
throw new Error('Pull request should have only one version label');
}
if (versionLabelsPresent.length === 1) {
const versionBump = versionLabels[versionLabelsPresent[0].name];
console.log(`::set-output name=versionBump::${versionBump}`)
return;
}
- name: Bumb classic version
if: steps.extract-version-label.outputs.versionBump != 'beta'
env:
BUMP_TYPE: ${{ steps.extract-version-label.outputs.versionBump }}
run: |
npm install -g semver
OLD_VERSION=$(cat Cargo.toml | grep "version" | head -n 1 | cut -d'"' -f2)
NEW_VERSION=$(semver -i $BUMP_TYPE $OLD_VERSION)
sed -i '0,/version.*$/s/version = "'"$OLD_VERSION"'"/version = "'"$NEW_VERSION"'"/' Cargo.toml
npm add --no-save remark-cli@^6.0.0 @jarrodldavis/remark-changelog-version-bump@^0.1.1
./node_modules/.bin/remark CHANGELOG.md -o --use @jarrodldavis/changelog-version-bump=version:\"$NEW_VERSION\"
git add CHANGELOG.md Cargo.toml
git commit -m "$NEW_VERSION"
git tag "v$NEW_VERSION"
- name: Push on main
id: push
run: |
git push
git push --tags
COMMIT_ID=$(git rev-parse HEAD)
echo "::set-output name=ref::$COMMIT_ID"
VERSION=$(cat Cargo.toml | grep "version" | head -n 1 | cut -d'"' -f2)
echo "::set-output name=version::$VERSION"
VERSION=$(cat Cargo.toml | grep "name" | head -n 1 | cut -d'"' -f2)
echo "::set-output name=name::$NAME"
- name: Create deployment
id: deployment
uses: actions/github-script@v2
env:
REF: ${{ steps.push.outputs.ref }}
VERSION: ${{ steps.push.outputs.VERSION }}
NAME: "opentelemetry-datadog-cloudflare"
VERSION_BUMP: ${{ steps.extract-version-labels.outputs.versionBump }}
with:
github-token: ${{ secrets.WORKFLOW_TOKEN }}
previews: 'ant-man-preview,flash-preview'
script: |
const ref = process.env.REF;
const name = process.env.NAME;
const version = process.env.VERSION;
const tag = process.env.VERSION_BUMP === 'beta' ? 'beta' : 'latest';
const { data: deployment } = await github.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `v${version}`,
environment: 'Crates',
auto_merge: false,
required_contexts: [],
payload: {
version,
name,
ref,
tag,
},
description: `Deploy ${name}@${version} on crates.io`,
production_environment: true,
});
- name: Extract release changelog
id: extract-changelog
if: steps.extract-version-label.outputs.versionBump != 'beta'
run: |
VERSION=$(cat Cargo.toml | grep "version" | head -n 1 | cut -d'"' -f2)
VERSION_PART=$(sed -n "/## \[$VERSION\]/,/## \[/{/## \[/d;p;}" CHANGELOG.md)
VERSION_PART="${VERSION_PART//'%'/'%25'}"
VERSION_PART="${VERSION_PART//$'\n'/'%0A'}"
VERSION_PART="${VERSION_PART//$'\r'/'%0D'}"
echo VERSION_PART=$VERSION_PART
echo "::set-output name=version-part::$VERSION_PART"
- name: Create Release
if: steps.extract-version-label.outputs.versionBump != 'beta'
uses: actions/github-script@v2
env:
VERSION: ${{ steps.push.outputs.version }}
BODY: ${{ steps.extract-changelog.outputs.version-part }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
await github.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: `v${process.env.VERSION}`,
name: process.env.TAG,
body: process.env.BODY,
draft: false,
prerelease: false,
})