Skip to content

enhancement(ci): add workflow to automatically bump ADP version after… #1

enhancement(ci): add workflow to automatically bump ADP version after…

enhancement(ci): add workflow to automatically bump ADP version after… #1

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@acaa02eee7e3bb0839e4272dacb37b8f3b58ba80 # v1.0.3
id: octo-sts
with:
scope: DataDog/saluki
policy: self.bump-adp-version.create-pr
- name: Checkout repository
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
with:
token: "${{ steps.octo-sts.outputs.token }}"
ref: main
- 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)
NEW_PATCH=$((PATCH + 1))
NEW_VERSION="${MAJOR}.${MINOR}.${NEW_PATCH}"
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@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.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';
// 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: 'main',
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 for bumping the ADP version, and close them out so this one takes precedence.
const { data: labeledPRData } = await github.rest.search.issuesAndPullRequests({
q: `repo:${context.repo.owner}/${context.repo.repo} is:pr is:open label:"${prLabel}"`
});
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})`
});
}