Skip to content

Commit

Permalink
workflows: Add automation for choco upgrade
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent T <[email protected]>
  • Loading branch information
vyncent-t committed Dec 16, 2024
1 parent 22688fa commit 49cb251
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 0 deletions.
98 changes: 98 additions & 0 deletions .github/workflows/pr-to-update-choco.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: PR for updating Chocolatey

# This action will run after a tag starting with "v" is published
on:
push:
tags:
- 'v*'
workflow_dispatch:

permissions:
contents: read

jobs:
choco-update:
permissions:
contents: write # for Git to git push
pull-requests: write # for creating PRs
runs-on: ubuntu-latest
steps:
- name: Checkout Headlamp
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
token: ${{ secrets.KINVOLK_REPOS_TOKEN }}
# we need the full history for the git tag command, so fetch all the branches
fetch-depth: 0

- name: Configure Git
run: |
user=${{github.actor}}
if [ -z $user ]; then
user=vyncent-t
fi
git config --global user.name "$user"
git config --global user.email "[email protected]"
# Set up Node.js environment, pay attention to the version
# Some features might not be available in older versions
- name: Create node.js environment
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: '21'

# Install the app dependencies for the choco script
- name: Install app dependencies
run: |
cd $GITHUB_WORKSPACE/app
npm ci
# MAYBE WE CAN USE THE SAME VERSION HERE?
# We set the latest tag as an environment variable before we use it in the next steps
# note that we have to echo the variable to the environment file to make it available in the next steps
- name: Set latest tag
run: |
echo "Setting latest tag"
latestTag=$(git tag --list --sort=version:refname 'v*' | tail -1)
# Remove the 'v' from the tag
latestTag=${latestTag#v}
echo "LATEST_HEADLAMP_TAG=$latestTag" >> $GITHUB_ENV
echo $latestTag
# Run the choco script
- name: Create nuget package
run: |
echo "Running choco script"
echo "Repository: ${{ github.repository }}"
echo "Workspace: ${GITHUB_WORKSPACE}"
echo $GITHUB_WORKSPACE
pwd
echo "creating nuget pkgs"
cd $GITHUB_WORKSPACE/app/windows/chocolatey
node choco-auto.js $LATEST_HEADLAMP_TAG
echo "Script finished"
# REFACTOR FOR CUSTOM NUGET REPO LATER IF NEEDED
- name: Create PR branch
run: |
user=${{github.actor}}
if [ -z $user ]; then
user=vyncent-t
fi
echo "Creating PR branch"
echo "Repository: ${{ github.repository }}"
echo "Workspace: ${GITHUB_WORKSPACE}"
pwd
ls
git checkout -b "choco-update-$LATEST_HEADLAMP_TAG"
git add .
git commit -s -m "Update choco package $LATEST_HEADLAMP_TAG"
git push origin "choco-update-$LATEST_HEADLAMP_TAG"
env:
GITHUB_TOKEN: ${{ secrets.KINVOLK_REPOS_TOKEN }}

# REFACTOR FOR CUSTOM NUGET REPO LATER IF NEEDED
- name: Create Pull Request
run: |
echo "Create pull request"
echo "continue with the following link"
echo "https://github.com/headlamp-k8s/headlamp/pull/new/choco-update-$LATEST_HEADLAMP_TAG"
104 changes: 104 additions & 0 deletions app/windows/chocolatey/choco-auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Usage: node choco-auto.js <projectVersion>
*
* This script is meant to be ran in the workflow to automatically update the Chocolatey package for Headlamp.
* We can run this script manually by following the steps below:
*
* 1. Ensure that Node.js and npm are installed on your system.
* 2. Run this script using the command `node choco-auto.js <projectVersion>`.
*
* Parameters:
* - projectVersion: The version of the project you want to fetch information for (e.g., 0.19.1).
*
* Ensure that you replace `<projectVersion>` with the actual project version, do not include the 'v'.
*
* 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.
*/

const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');

const args = process.argv.slice(2);

if (!(args.length === 1)) {
console.error('Usage: node choco-auto.js <projectVersion>');
process.exit(1);
}

const projectVersion = args[0];

if (projectVersion.startsWith('v')) {
console.error('Please provide the project version without the "v" prefix.');
console.error('Example: node choco-auto.js 0.19.1');
process.exit(1);
}

// Async function to fetch release information from GitHub API for a given tag
// For this script we are only interested in the checksums for the windows x64 asset
async function fetchGithubReleaseInfo() {
const tag = projectVersion;
const res = await fetch(
`https://api.github.com/repos/headlamp-k8s/headlamp/releases/tags/v${tag}`
);

if (!res.ok) {
console.error(
`Error fetching release information for tag ${tag}: ${res.status} ${res.statusText}`
);
process.exit(1);
}

const resJSON = await res.json();

const checksum = await updateReleaseInfo(resJSON, tag);
return checksum;
}

async function getChecksums(checksumsUrl, fileName) {
const res = await fetch(checksumsUrl);
const checksums = await res.text();

for (const line of checksums.split('\n')) {
const [checksum, filename] = line.split(' ');

if (!filename) {
continue;
}

if (filename === fileName) {
return checksum;
}
}

return '';
}

async function updateReleaseInfo(response, tag) {
const assets = response.assets;
const checksums = assets.find(v => v.name === 'checksums.txt');
const checksumsUrl = checksums.browser_download_url;
// asset for windows x64, can add more assets for other archs
const winx64Asset = assets.find(asset => asset.name === `Headlamp-${tag}-win-x64.exe`);
const browserDownloadName = winx64Asset.name;

const checksum = await getChecksums(checksumsUrl, browserDownloadName);

return checksum;
}

async function runChocoUpdate() {
const checksum = await fetchGithubReleaseInfo();

exec(`./choco-bump.sh ${projectVersion} ${checksum}`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error - source choco-auto.js : ${error}`);
return;
}

console.log(`stdout - source choco-auto.js : ${stdout}`);
console.error(`stderr - source choco-auto.js : ${stderr}`);
});
}

runChocoUpdate();

0 comments on commit 49cb251

Please sign in to comment.