Skip to content

Commit

Permalink
Refactored Version Bump Matrix Workflow
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Wanlin <awanlin@spotify.com>
  • Loading branch information
awanlin committed Jun 28, 2024
1 parent 5b51672 commit 403f7aa
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 45 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/version-bump.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
- next
- main
workspace_input:
description: 'Workspace'
description: 'Workspace (this must be a JSON array)'
required: true
type: string

Expand All @@ -26,11 +26,11 @@ jobs:
strategy:
fail-fast: false
matrix:
workspace: ${{ inputs.workspace_input }}
workspace: ${{ fromJSON(inputs.workspace_input) }}

name: ${{ matrix.workspace }} version:bump
steps:
- name: 'Checkout backstage'
- name: 'Checkout community-plugins'
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
with:
fetch-depth: 1
Expand Down Expand Up @@ -61,11 +61,14 @@ jobs:
${{ runner.os }}-yarn-
- name: yarn install
run: yarn install --immutable
- name: workspace yarn install
working-directory: ./workspaces/${{ matrix.workspace }}
run: yarn install --immutable
# End of yarn setup

- name: 'Set release name'
id: set_release_name
run: node scripts/ci/set-release-name.js
run: node scripts/ci/set-release-name.js ${{ matrix.workspace }}
- name: 'Configure git'
run: |
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
Expand All @@ -79,6 +82,7 @@ jobs:
fi
git checkout -b ${{ matrix.workspace }}/v${{ steps.set_release_name.outputs.release_version }}
- name: Run backstage-cli versions:bump --release ${{ inputs.release_line || 'next' }} command
working-directory: ./workspaces/${{ matrix.workspace }}
run: yarn backstage-cli versions:bump --release ${{ inputs.release_line || 'next' }}
env:
YARN_ENABLE_IMMUTABLE_INSTALLS: false
Expand Down Expand Up @@ -107,15 +111,15 @@ jobs:
github-token: ${{secrets.GH_SERVICE_ACCOUNT_TOKEN}}
script: |
await github.rest.pulls.create({
title: '${{ matrix.workspace }} - Example App version:bump to v${{ steps.set_release_name.outputs.release_version }}',
title: '${{ matrix.workspace }} - version:bump to v${{ steps.set_release_name.outputs.release_version }}',
owner: context.repo.owner,
repo: context.repo.repo,
head: '${{ matrix.workspace }}/v${{ steps.set_release_name.outputs.release_version }}',
base: 'main',
body: [
'Backstage release v${{ steps.set_release_name.outputs.release_version }} has been published, this Pull Request contains the changes to upgrade to this new release',
'Backstage release v${{ steps.set_release_name.outputs.release_version }} has been published, this Pull Request contains the changes to upgrade ${{ matrix.workspace }} to this new release',
' ',
'Please review the changelog before approving, there may be manual changes needed to enable new features:',
'Please review the changelog before approving, there may be manual changes needed:',
' ',
'- Changelog: [v${{ steps.set_release_name.outputs.release_version }}](https://github.com/backstage/backstage/blob/master/docs/releases/v${{ steps.set_release_name.outputs.release_version }}-changelog.md)',
'- Upgrade Helper: [From ${{ steps.set_release_name.outputs.current_version }} to ${{ steps.set_release_name.outputs.release_version }}](https://backstage.github.io/upgrade-helper/?from=${{ steps.set_release_name.outputs.current_version }}&to=${{ steps.set_release_name.outputs.release_version }})',
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@
"@backstage/cli-node": "^0.2.2",
"@changesets/parse": "^0.4.0",
"@octokit/rest": "^20.1.1"
},
"devDependencies": {
"fs-extra": "11.2.0",
"node-fetch": "^2.6.7"
}
}
124 changes: 86 additions & 38 deletions scripts/ci/set-release-name.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,115 @@
#!/usr/bin/env node
/* eslint-disable import/no-extraneous-dependencies */
const path = require('path');
const fs = require('fs-extra');
const fetch = require('node-fetch')
const { EOL } = require('os');
/* eslint-disable @backstage/no-undeclared-imports */
/*
* Copyright 2020 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

async function getBackstageVersion() {
const rootPath = path.resolve(__dirname, '../backstage.json');
return fs.readJson(rootPath).then(_ => _.version);
import path from 'path';
import fs from 'fs-extra';
import fetch from 'node-fetch';
import { EOL } from 'os';

async function getBackstageVersion(workspace) {
const rootPath = path.resolve(`workspaces/${workspace}/backstage.json`);
if (!fs.exists(rootPath)) {
return 'N/A';
}
return fs.readJson(rootPath).then((_) => _.version);
}

async function getLatestRelease() {
const response = await fetch('https://api.github.com/repos/backstage/backstage/releases/latest')
const response = await fetch(
'https://api.github.com/repos/backstage/backstage/releases/latest',
);
const json = await response.json();
return json
return json;
}

async function getLatestPreRelease() {
const response = await fetch('https://api.github.com/repos/backstage/backstage/releases')
const response = await fetch(
'https://api.github.com/repos/backstage/backstage/releases',
);
const json = await response.json();

const preReleasesOnly = json.filter(release => {
return release.prerelease === true
})

const latestPreRelease = preReleasesOnly.sort((a,b) => {return new Date(b.published_at) - new Date(a.published_at)})[0];
const preReleasesOnly = json.filter((release) => {
return release.prerelease === true;
});

const latestPreRelease = preReleasesOnly.sort((a, b) => {
return new Date(b.published_at) - new Date(a.published_at);
})[0];

return latestPreRelease
return latestPreRelease;
}

async function main() {

// Get the workspace
const [script, workspace] = process.argv.slice(1);
if (!workspace) {
throw new Error(`Argument must be ${script} <workspace>`);
}

// Get the current Backstage version from the backstage.json file
const backstageVersion = await getBackstageVersion()
const backstageVersion = await getBackstageVersion(workspace);
// Get the latest Backstage Release from the GitHub API
const latestRelease = await getLatestRelease()
const latestRelease = await getLatestRelease();
// Get the latest Backstage Pre-release from the GitHub API
const latestPreRelease = await getLatestPreRelease()
const latestPreRelease = await getLatestPreRelease();

console.log(`Current Backstage version is: v${backstageVersion}`)
console.log(`Latest Release version is: ${latestRelease.name}, published on: ${latestRelease.published_at}`)
console.log(`Latest Pre-release version is: ${latestPreRelease.name}, published on: ${latestPreRelease.published_at}`)
console.log()
console.log(`Current Backstage version is: v${backstageVersion}`);
console.log(
`Latest Release version is: ${latestRelease.name}, published on: ${latestRelease.published_at}`,
);
console.log(
`Latest Pre-release version is: ${latestPreRelease.name}, published on: ${latestPreRelease.published_at}`,
);
console.log();

const latestReleaseDate = new Date(latestRelease.published_at).getTime()
const latestPreReleaseDate = new Date(latestPreRelease.published_at).getTime()
if (latestReleaseDate > latestPreReleaseDate){
console.log(`Latest Release is newer than latest Pre-release, using Latest Release name ${latestRelease.name}`)
console.log()
const latestReleaseDate = new Date(latestRelease.published_at).getTime();
const latestPreReleaseDate = new Date(
latestPreRelease.published_at,
).getTime();
if (latestReleaseDate > latestPreReleaseDate) {
console.log(
`Latest Release is newer than latest Pre-release, using Latest Release name ${latestRelease.name}`,
);
console.log();

await fs.appendFile(process.env.GITHUB_OUTPUT, `release_version=${latestRelease.name.substring(1)}${EOL}`);
}
else {
console.log(`Latest Release is older than latest Pre-release, using Latest Pre-release name ${latestPreRelease.name}`)
console.log()
await fs.appendFile(
process.env.GITHUB_OUTPUT,
`release_version=${latestRelease.name.substring(1)}${EOL}`,
);
} else {
console.log(
`Latest Release is older than latest Pre-release, using Latest Pre-release name ${latestPreRelease.name}`,
);
console.log();

await fs.appendFile(process.env.GITHUB_OUTPUT, `release_version=${latestPreRelease.name.substring(1)}${EOL}`);
await fs.appendFile(
process.env.GITHUB_OUTPUT,
`release_version=${latestPreRelease.name.substring(1)}${EOL}`,
);
}

await fs.appendFile(process.env.GITHUB_OUTPUT, `current_version=${backstageVersion}${EOL}`);
await fs.appendFile(
process.env.GITHUB_OUTPUT,
`current_version=${backstageVersion}${EOL}`,
);
}

main().catch(error => {
main().catch((error) => {
console.error(error.stack);
process.exit(1);
});

0 comments on commit 403f7aa

Please sign in to comment.