Skip to content

Prepare release

Prepare release #62

name: Prepare release
env:
GIT_AUTHOR_EMAIL: "packages@datadoghq.com"
GIT_AUTHOR_NAME: "ci.datadog-sync-cli"
on:
workflow_dispatch:
inputs:
version:
description: New version number (e.g. '1.2.3' without the 'v' prefix)
jobs:
prepare_release:
name: Create release PR
runs-on: ubuntu-latest
permissions:
id-token: write
steps:
- name: Get access token
uses: DataDog/dd-octo-sts-action@acaa02eee7e3bb0839e4272dacb37b8f3b58ba80 # v1.0.3
id: octo-sts
with:
scope: DataDog/datadog-sync-cli
policy: self.prepare-release.create-pr
- name: Checkout code
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
with:
fetch-depth: 0
- name: Setup Git
run: |
git config user.name "${GIT_AUTHOR_NAME}"
git config user.email "${GIT_AUTHOR_EMAIL}"
- name: Calculate version
id: get_version
run: |
if [ "${VERSION}" = "" ] ; then
LATEST_TAG=$(git describe --tags --abbrev=0)
NEXT_TAG=$(echo ${LATEST_TAG} | awk '{split($0, a, "."); print a[1] "." a[2] + 1 "." a[3]}')
echo "version=$NEXT_TAG" >> $GITHUB_OUTPUT
else
echo "version=$VERSION" >> $GITHUB_OUTPUT
fi
env:
VERSION: ${{ github.event.inputs.version }}
- name: Create PR
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
RELEASE_VERSION: ${{ steps.get_version.outputs.version }}
with:
github-token: ${{ steps.octo-sts.outputs.token }}
script: |
const { data: notes } = await github.rest.repos.generateReleaseNotes({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: process.env.RELEASE_VERSION,
});
const today = new Date().toJSON().slice(0, 10);
const header = [`# Changelog\n\n## ${process.env.RELEASE_VERSION} / ${today}\n`];
const changes = header.concat(notes.body.split("\n").slice(3));
const { data: content } = await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: "CHANGELOG.md",
});
const rawContent = Buffer.from(content.content, "base64")
.toString("utf-8")
.split("\n");
const newContent = changes.concat(rawContent.slice(1)).join("\n");
const { data: main } = await github.rest.git.getRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "heads/main",
});
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/heads/release/${process.env.RELEASE_VERSION}`,
sha: main.object.sha,
});
const { data: commit } = await github.rest.repos.createOrUpdateFileContents({
owner: context.repo.owner,
repo: context.repo.repo,
message: "Update CHANGELOG",
content: Buffer.from(newContent).toString("base64"),
path: "CHANGELOG.md",
branch: `release/${process.env.RELEASE_VERSION}`,
sha: content.sha,
});
const { data: pr } = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
head: `release/${process.env.RELEASE_VERSION}`,
base: "main",
title: `Release ${process.env.RELEASE_VERSION}`,
body: "Update CHANGELOG",
});
await github.rest.issues.addLabels({
issue_number: pr.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ["changelog/no-changelog"],
});