Skip to content

Commit 49cb251

Browse files
committed
workflows: Add automation for choco upgrade
Signed-off-by: Vincent T <vtaylor@microsoft.com>
1 parent 22688fa commit 49cb251

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
name: PR for updating Chocolatey
2+
3+
# This action will run after a tag starting with "v" is published
4+
on:
5+
push:
6+
tags:
7+
- 'v*'
8+
workflow_dispatch:
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
choco-update:
15+
permissions:
16+
contents: write # for Git to git push
17+
pull-requests: write # for creating PRs
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Checkout Headlamp
21+
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
22+
with:
23+
token: ${{ secrets.KINVOLK_REPOS_TOKEN }}
24+
# we need the full history for the git tag command, so fetch all the branches
25+
fetch-depth: 0
26+
27+
- name: Configure Git
28+
run: |
29+
user=${{github.actor}}
30+
if [ -z $user ]; then
31+
user=vyncent-t
32+
fi
33+
git config --global user.name "$user"
34+
git config --global user.email "$user@users.noreply.github.com"
35+
36+
# Set up Node.js environment, pay attention to the version
37+
# Some features might not be available in older versions
38+
- name: Create node.js environment
39+
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
40+
with:
41+
node-version: '21'
42+
43+
# Install the app dependencies for the choco script
44+
- name: Install app dependencies
45+
run: |
46+
cd $GITHUB_WORKSPACE/app
47+
npm ci
48+
49+
# MAYBE WE CAN USE THE SAME VERSION HERE?
50+
# We set the latest tag as an environment variable before we use it in the next steps
51+
# note that we have to echo the variable to the environment file to make it available in the next steps
52+
- name: Set latest tag
53+
run: |
54+
echo "Setting latest tag"
55+
latestTag=$(git tag --list --sort=version:refname 'v*' | tail -1)
56+
# Remove the 'v' from the tag
57+
latestTag=${latestTag#v}
58+
echo "LATEST_HEADLAMP_TAG=$latestTag" >> $GITHUB_ENV
59+
echo $latestTag
60+
61+
# Run the choco script
62+
- name: Create nuget package
63+
run: |
64+
echo "Running choco script"
65+
echo "Repository: ${{ github.repository }}"
66+
echo "Workspace: ${GITHUB_WORKSPACE}"
67+
echo $GITHUB_WORKSPACE
68+
pwd
69+
echo "creating nuget pkgs"
70+
cd $GITHUB_WORKSPACE/app/windows/chocolatey
71+
node choco-auto.js $LATEST_HEADLAMP_TAG
72+
echo "Script finished"
73+
74+
# REFACTOR FOR CUSTOM NUGET REPO LATER IF NEEDED
75+
- name: Create PR branch
76+
run: |
77+
user=${{github.actor}}
78+
if [ -z $user ]; then
79+
user=vyncent-t
80+
fi
81+
echo "Creating PR branch"
82+
echo "Repository: ${{ github.repository }}"
83+
echo "Workspace: ${GITHUB_WORKSPACE}"
84+
pwd
85+
ls
86+
git checkout -b "choco-update-$LATEST_HEADLAMP_TAG"
87+
git add .
88+
git commit -s -m "Update choco package $LATEST_HEADLAMP_TAG"
89+
git push origin "choco-update-$LATEST_HEADLAMP_TAG"
90+
env:
91+
GITHUB_TOKEN: ${{ secrets.KINVOLK_REPOS_TOKEN }}
92+
93+
# REFACTOR FOR CUSTOM NUGET REPO LATER IF NEEDED
94+
- name: Create Pull Request
95+
run: |
96+
echo "Create pull request"
97+
echo "continue with the following link"
98+
echo "https://github.com/headlamp-k8s/headlamp/pull/new/choco-update-$LATEST_HEADLAMP_TAG"
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* Usage: node choco-auto.js <projectVersion>
3+
*
4+
* This script is meant to be ran in the workflow to automatically update the Chocolatey package for Headlamp.
5+
* We can run this script manually by following the steps below:
6+
*
7+
* 1. Ensure that Node.js and npm are installed on your system.
8+
* 2. Run this script using the command `node choco-auto.js <projectVersion>`.
9+
*
10+
* Parameters:
11+
* - projectVersion: The version of the project you want to fetch information for (e.g., 0.19.1).
12+
*
13+
* Ensure that you replace `<projectVersion>` with the actual project version, do not include the 'v'.
14+
*
15+
* Note: The `choco-bump.sh` script should be located in the same directory as this script and should add changes to the headlamp.nuspec and chocolateyinstall.ps1 files.
16+
*/
17+
18+
const fs = require('fs');
19+
const path = require('path');
20+
const { exec } = require('child_process');
21+
22+
const args = process.argv.slice(2);
23+
24+
if (!(args.length === 1)) {
25+
console.error('Usage: node choco-auto.js <projectVersion>');
26+
process.exit(1);
27+
}
28+
29+
const projectVersion = args[0];
30+
31+
if (projectVersion.startsWith('v')) {
32+
console.error('Please provide the project version without the "v" prefix.');
33+
console.error('Example: node choco-auto.js 0.19.1');
34+
process.exit(1);
35+
}
36+
37+
// Async function to fetch release information from GitHub API for a given tag
38+
// For this script we are only interested in the checksums for the windows x64 asset
39+
async function fetchGithubReleaseInfo() {
40+
const tag = projectVersion;
41+
const res = await fetch(
42+
`https://api.github.com/repos/headlamp-k8s/headlamp/releases/tags/v${tag}`
43+
);
44+
45+
if (!res.ok) {
46+
console.error(
47+
`Error fetching release information for tag ${tag}: ${res.status} ${res.statusText}`
48+
);
49+
process.exit(1);
50+
}
51+
52+
const resJSON = await res.json();
53+
54+
const checksum = await updateReleaseInfo(resJSON, tag);
55+
return checksum;
56+
}
57+
58+
async function getChecksums(checksumsUrl, fileName) {
59+
const res = await fetch(checksumsUrl);
60+
const checksums = await res.text();
61+
62+
for (const line of checksums.split('\n')) {
63+
const [checksum, filename] = line.split(' ');
64+
65+
if (!filename) {
66+
continue;
67+
}
68+
69+
if (filename === fileName) {
70+
return checksum;
71+
}
72+
}
73+
74+
return '';
75+
}
76+
77+
async function updateReleaseInfo(response, tag) {
78+
const assets = response.assets;
79+
const checksums = assets.find(v => v.name === 'checksums.txt');
80+
const checksumsUrl = checksums.browser_download_url;
81+
// asset for windows x64, can add more assets for other archs
82+
const winx64Asset = assets.find(asset => asset.name === `Headlamp-${tag}-win-x64.exe`);
83+
const browserDownloadName = winx64Asset.name;
84+
85+
const checksum = await getChecksums(checksumsUrl, browserDownloadName);
86+
87+
return checksum;
88+
}
89+
90+
async function runChocoUpdate() {
91+
const checksum = await fetchGithubReleaseInfo();
92+
93+
exec(`./choco-bump.sh ${projectVersion} ${checksum}`, (error, stdout, stderr) => {
94+
if (error) {
95+
console.error(`exec error - source choco-auto.js : ${error}`);
96+
return;
97+
}
98+
99+
console.log(`stdout - source choco-auto.js : ${stdout}`);
100+
console.error(`stderr - source choco-auto.js : ${stderr}`);
101+
});
102+
}
103+
104+
runChocoUpdate();

0 commit comments

Comments
 (0)