-
Notifications
You must be signed in to change notification settings - Fork 11
203 lines (167 loc) · 7.9 KB
/
Copy pathbump-adp-version.yml
File metadata and controls
203 lines (167 loc) · 7.9 KB
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
name: Bump ADP Version
on:
push:
tags:
- "[0-9]+.[0-9]+.[0-9]+"
workflow_dispatch:
jobs:
bump-version:
runs-on: ubuntu-latest
permissions:
id-token: write
steps:
- name: Get access token from dd-octo-sts
uses: DataDog/dd-octo-sts-action@96a25462dbcb10ebf0bfd6e2ccc917d2ab235b9a # v1.0.4
id: octo-sts
with:
scope: DataDog/saluki
policy: self.bump-adp-version.create-pr
- name: Determine release type and target branch
id: release-info
run: |
TAG_NAME="${{ github.ref_name }}"
if [[ "$TAG_NAME" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
MAJOR="${BASH_REMATCH[1]}"
MINOR="${BASH_REMATCH[2]}"
PATCH="${BASH_REMATCH[3]}"
if [[ "$PATCH" == "0" ]]; then
TARGET_BRANCH="main"
RELEASE_TYPE="minor"
else
TARGET_BRANCH="release/${MAJOR}.${MINOR}.x"
RELEASE_TYPE="patch"
fi
else
# workflow_dispatch without a version tag: default to minor bump on main
TARGET_BRANCH="main"
RELEASE_TYPE="minor"
fi
echo "Release type: $RELEASE_TYPE, target branch: $TARGET_BRANCH"
echo "target_branch=$TARGET_BRANCH" >> $GITHUB_OUTPUT
echo "release_type=$RELEASE_TYPE" >> $GITHUB_OUTPUT
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
token: "${{ steps.octo-sts.outputs.token }}"
ref: ${{ steps.release-info.outputs.target_branch }}
- name: Configure Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Calculate new version
id: version
run: |
CURRENT_VERSION=$(grep -E '^version = "' bin/agent-data-plane/Cargo.toml | head -n 1 | cut -d '"' -f 2)
echo "Current version: $CURRENT_VERSION"
MAJOR=$(echo $CURRENT_VERSION | cut -d '.' -f 1)
MINOR=$(echo $CURRENT_VERSION | cut -d '.' -f 2)
PATCH=$(echo $CURRENT_VERSION | cut -d '.' -f 3)
if [[ "${{ steps.release-info.outputs.release_type }}" == "minor" ]]; then
NEW_MINOR=$((MINOR + 1))
NEW_VERSION="${MAJOR}.${NEW_MINOR}.0"
else
NEW_PATCH=$((PATCH + 1))
NEW_VERSION="${MAJOR}.${MINOR}.${NEW_PATCH}"
fi
echo "New version: $NEW_VERSION"
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
- name: Update version in Cargo.toml
run: |
CURRENT_VERSION="${{ steps.version.outputs.current_version }}"
NEW_VERSION="${{ steps.version.outputs.new_version }}"
sed -i "s/^version = \"$CURRENT_VERSION\"/version = \"$NEW_VERSION\"/" bin/agent-data-plane/Cargo.toml
echo "Updated bin/agent-data-plane/Cargo.toml"
- name: Update Cargo.lock
run: cargo update -p agent-data-plane --quiet
- name: Create remote branch
run: |
BRANCH_NAME=bump-adp-version-${{ steps.version.outputs.new_version }}
git push origin HEAD:refs/heads/${BRANCH_NAME}
- name: Commit Changes
id: commit
run: |
git add bin/agent-data-plane/Cargo.toml Cargo.lock
git commit -m "chore(agent-data-plane): bump version to ${{ steps.version.outputs.new_version }}"
echo "commit_sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
- name: Sign Commit
uses: DataDog/commit-headless@5a0f3876e0fbdd3a86b3e008acf4ec562db59eee # v2.0.1
with:
token: "${{ steps.octo-sts.outputs.token }}"
branch: bump-adp-version-${{ steps.version.outputs.new_version }}
head-sha: ${{ github.sha }}
command: push
commits: "${{ steps.commit.outputs.commit_sha }}"
- name: Create Pull Request
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
github-token: "${{ steps.octo-sts.outputs.token }}"
script: |
const branchName = `bump-adp-version-${{ steps.version.outputs.new_version }}`;
const prTitle = `chore(agent-data-plane): bump version to ${{ steps.version.outputs.new_version }}`;
const prBody = `## Summary
This PR bumps the Agent Data Plane version to ${{ steps.version.outputs.new_version }} for the next development cycle.
### Details of change
- **Previous version:** ${{ steps.version.outputs.current_version }}
- **New version:** ${{ steps.version.outputs.new_version }}
_This PR was automatically generated by the [Bump ADP Version](.github/workflows/bump-adp-version.yml) workflow._
## Change Type
- [ ] Bug fix
- [ ] New feature
- [x] Non-functional (chore, refactoring, docs)
- [ ] Performance`;
const prLabel = 'ci/bump-adp-version';
const targetBranch = '${{ steps.release-info.outputs.target_branch }}';
// Make sure we don't have an open PR for the exact same change.
const { data: exactPRData } = await github.rest.search.issuesAndPullRequests({
q: `repo:${context.repo.owner}/${context.repo.repo} is:pr is:open head:${branchName}`
});
const exactPRs = exactPRData.items;
if (exactPRs.length > 0) {
console.log(`Pull request for this version already exists: ${exactPRs[0].html_url}`);
return;
}
// Create the new pull request.
const updatePR = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: prTitle,
head: branchName,
base: targetBranch,
body: prBody,
});
console.log(`Pull request created: ${updatePR.data.html_url}`);
// Set the right labels on the PR.
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: updatePR.data.number,
labels: [prLabel, 'automated']
});
// Look for other open PRs bumping the ADP version on the same target branch, and close them out so this
// one takes precedence. Scoping by base branch keeps minor bumps (main) and patch bumps (release/*)
// from closing each other.
const { data: labeledPRData } = await github.rest.search.issuesAndPullRequests({
q: `repo:${context.repo.owner}/${context.repo.repo} is:pr is:open label:"${prLabel}" base:${targetBranch}`
});
const obsoleteUpdatePRs = labeledPRData.items;
// Close any existing PRs created from this workflow so that we only have the latest one.
for (const obsoleteUpdatePR of obsoleteUpdatePRs) {
// Don't close the PR we just opened.
if (obsoleteUpdatePR.number == updatePR.data.number) {
continue;
}
console.log(`Closing existing PR: ${obsoleteUpdatePR.html_url}`);
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: obsoleteUpdatePR.number,
state: 'closed'
});
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: obsoleteUpdatePR.number,
body: `Closing this pull request as a newer version bump is available: [update PR](https://github.com/${context.repo.owner}/${context.repo.repo}/pull/${updatePR.data.number})`
});
}