Skip to content

Commit 0326294

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

File tree

2 files changed

+207
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)