Simplify usage of the bump CLI workflow

This commit is contained in:
Koen Vlaswinkel
2025-01-23 16:41:52 +01:00
parent f5435783c0
commit 7f35a4b7ed
3 changed files with 112 additions and 39 deletions

View File

@@ -1,25 +1,12 @@
name: Bump CLI version
on:
workflow_dispatch:
inputs:
option:
description: "Option"
required: true
default: 'replace'
type: choice
options:
- prepend
- replace
version:
description: |
The version to prepend to the supported versions file. This should be in the form: `vA.B.C`.
required: false
type: string
pull_request:
branches: [main]
paths:
- .github/actions/create-pr/action.yml
- .github/workflows/bump-cli.yml
- extensions/ql-vscode/scripts/bump-supported-cli-versions.ts
schedule:
- cron: 0 0 */14 * * # run every 14 days
@@ -34,28 +21,31 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
fetch-depth: 1
node-version-file: extensions/ql-vscode/.nvmrc
cache: 'npm'
cache-dependency-path: extensions/ql-vscode/package-lock.json
- name: Install dependencies
working-directory: extensions/ql-vscode
run: |
npm ci
shell: bash
- name: Bump CLI
if: ${{ inputs.option == 'replace' }}
working-directory: extensions/ql-vscode
id: bump-cli
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
scripts/replace-cli-version.sh
- name: Prepend another version
if: ${{ inputs.option == 'prepend' }}
run: |
cat extensions/ql-vscode/supported_cli_versions.json | jq '. |= ["${{ inputs.version }}"] + .' > supported_cli_versions_temp.json
mv supported_cli_versions_temp.json extensions/ql-vscode/supported_cli_versions.json
echo "LATEST_VERSION=${{ inputs.version }}" >> $GITHUB_ENV
echo "PREVIOUS_VERSION=`jq -r '.[1]' extensions/ql-vscode/supported_cli_versions.json`" >> $GITHUB_ENV
npx vite-node scripts/bump-supported-cli-versions.ts
shell: bash
- name: Commit, Push and Open a PR
uses: ./.github/actions/create-pr
with:
token: ${{ secrets.GITHUB_TOKEN }}
base-branch: main
head-branch: github-action/bump-cli
commit-message: Bump CLI version from ${{ env.PREVIOUS_VERSION }} to ${{ env.LATEST_VERSION }} for integration tests
title: Bump CLI Version to ${{ env.LATEST_VERSION }} for integration tests
commit-message: Bump CLI version from ${{ steps.bump-cli.outputs.PREVIOUS_VERSION }} to ${{ steps.bump-cli.outputs.LATEST_VERSION }} for integration tests
title: Bump CLI Version to ${{ steps.bump-cli.outputs.LATEST_VERSION }} for integration tests
body: >
Bumps CLI version from ${{ env.PREVIOUS_VERSION }} to ${{ env.LATEST_VERSION }}
Bumps CLI version from ${{ steps.bump-cli.outputs.PREVIOUS_VERSION }} to ${{ steps.bump-cli.outputs.LATEST_VERSION }}

View File

@@ -0,0 +1,95 @@
import { spawnSync } from "child_process";
import { resolve } from "path";
import { appendFile, outputJson, readJson } from "fs-extra";
import { SemVer } from "semver";
const supportedCliVersionsPath = resolve(
__dirname,
"..",
"supported_cli_versions.json",
);
async function bumpSupportedCliVersions() {
const existingVersions = (await readJson(
supportedCliVersionsPath,
)) as string[];
const release = runGhJSON<Release>([
"release",
"view",
"--json",
"id,name",
"--repo",
"github/codeql-cli-binaries",
]);
// There are two cases:
// - Replace the version if it's the same major and minor version
// - Prepend the version if it's a new major or minor version
const latestSupportedVersion = new SemVer(existingVersions[0]);
const latestReleaseVersion = new SemVer(release.name);
if (latestSupportedVersion.compare(latestReleaseVersion) === 0) {
console.log("No need to update supported CLI versions");
return;
}
if (process.env.GITHUB_OUTPUT) {
await appendFile(
process.env.GITHUB_OUTPUT,
`PREVIOUS_VERSION=${existingVersions[0]}\n`,
{
encoding: "utf-8",
},
);
}
if (
latestSupportedVersion.major === latestReleaseVersion.major &&
latestSupportedVersion.minor === latestReleaseVersion.minor
) {
existingVersions[0] = release.name;
console.log(`Replaced latest supported CLI version with ${release.name}`);
} else {
existingVersions.unshift(release.name);
console.log(`Added latest supported CLI version ${release.name}`);
}
await outputJson(supportedCliVersionsPath, existingVersions, {
spaces: 2,
finalEOL: true,
});
if (process.env.GITHUB_OUTPUT) {
await appendFile(
process.env.GITHUB_OUTPUT,
`LATEST_VERSION=${existingVersions[0]}\n`,
{
encoding: "utf-8",
},
);
}
}
bumpSupportedCliVersions().catch((e: unknown) => {
console.error(e);
process.exit(2);
});
function runGh(args: readonly string[]): string {
const gh = spawnSync("gh", args);
if (gh.status !== 0) {
throw new Error(`Failed to run gh ${args.join(" ")}: ${gh.stderr}`);
}
return gh.stdout.toString("utf-8");
}
function runGhJSON<T>(args: readonly string[]): T {
return JSON.parse(runGh(args));
}
type Release = {
id: string;
name: string;
};

View File

@@ -1,12 +0,0 @@
#!/bin/bash
VERSIONS=$(gh api -H "Accept: application/vnd.github+json" /repos/github/codeql-cli-binaries/releases | jq -r '.[].tag_name' | head -2)
# we are exporting these variables so that we can access these variables in the workflow
LATEST_VERSION=$(echo $VERSIONS | awk '{ print $1 }')
PREVIOUS_VERSION=$(echo $VERSIONS | awk '{ print $2 }')
echo "LATEST_VERSION=$LATEST_VERSION" >> $GITHUB_ENV
echo "PREVIOUS_VERSION=$PREVIOUS_VERSION" >> $GITHUB_ENV
sed -i "s/$PREVIOUS_VERSION/$LATEST_VERSION/g" extensions/ql-vscode/supported_cli_versions.json