feat: simplify getting version

This commit is contained in:
Eli Bosley
2024-10-24 12:07:12 -04:00
parent a929c7e3b3
commit 812053d7a4
3 changed files with 16 additions and 30 deletions

View File

@@ -70,12 +70,13 @@ jobs:
id: vars
run: |
echo "GIT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "API_VERSION=$(cat package.json | jq -r '.version')" >> $GITHUB_OUTPUT
echo "IS_TAGGED=$(git describe --tags --abbrev=0 --exact-match || echo '')" >> $GITHUB_OUTPUT
echo "PACKAGE_LOCK_VERSION=$(jq -r '.version' package-lock.json)" >> $GITHUB_OUTPUT
echo "API_VERSION=$([[ -n "$IS_TAGGED" ]] && echo "$PACKAGE_LOCK_VERSION" || echo "${PACKAGE_LOCK_VERSION}+${GIT_SHA}")" >> $GITHUB_OUTPUT
- name: Build inside of the docker container
id: build-pack-binary
run: |
docker run --rm -v ${{ github.workspace }}/api/deploy/release:/app/deploy/release -e GIT_SHA=${{ steps.vars.outputs.GIT_SHA }} -e API_VERSION=${{ steps.vars.outputs.API_VERSION }} -e IS_TAGGED=${{ steps.vars.outputs.IS_TAGGED }} builder npm run build-and-pack
docker run --rm -v ${{ github.workspace }}/api/deploy/release:/app/deploy/release -e API_VERSION=${{ steps.vars.outputs.API_VERSION }} builder npm run build-and-pack
- name: Set Hashes
id: set-hashes

View File

@@ -2,7 +2,7 @@
import { exit } from 'process';
import { cd, $ } from 'zx';
import getTags from './get-tags.mjs';
import getTags, { getDeploymentVersion } from './get-deployment-version.mjs';
try {
// Enable colours in output
@@ -39,19 +39,13 @@ try {
assert: { type: 'json' },
}).then((pkg) => pkg.default);
const tags = getTags(process.env);
// Decide whether to use full version or just tag
const isTaggedRelease = tags.isTagged;
const gitShaShort = tags.shortSha;
const deploymentVersion = isTaggedRelease ? version : `${version}+${gitShaShort}`;
const deploymentVersion = getDeploymentVersion(process.env, version);
// Create deployment package.json
await $`echo ${JSON.stringify({
...rest,
name,
version: deploymentVersion,
...rest,
})} > ./deploy/pre-pack/package.json`;
// # Create final tgz
@@ -64,9 +58,6 @@ try {
// Move unraid-api.tgz to release directory
await $`mv unraid-api-${deploymentVersion}.tgz ../release`;
// Set API_VERSION output based on this command
await $`echo "::set-output name=API_VERSION::${deploymentVersion}"`;
} catch (error) {
// Error with a command
if (Object.keys(error).includes('stderr')) {

View File

@@ -3,20 +3,19 @@ import { execSync } from 'child_process';
const runCommand = (command) => {
try {
return execSync(command, { stdio: 'pipe' }).toString().trim();
} catch(error) {
} catch (error) {
console.log('Failed to get value from tag command: ', command, error.message);
return;
}
};
const getTags = (env = process.env) => {
if (env.GIT_SHA) {
console.log(`Using env vars for git tags: ${env.GIT_SHA} ${env.IS_TAGGED}`)
return {
shortSha: env.GIT_SHA,
isTagged: Boolean(env.IS_TAGGED)
}
export const getDeploymentVersion = (env = process.env, packageVersion) => {
if (env.API_VERSION) {
console.log(`Using env var for version: ${env.API_VERSION}`);
return env.API_VERSION;
} else if (env.GIT_SHA && env.IS_TAGGED) {
console.log(`Using env vars for git tags: ${env.GIT_SHA} ${env.IS_TAGGED}`);
return env.IS_TAGGED ? packageVersion : `${packageVersion}+${env.GIT_SHA}`;
} else {
const gitShortSHA = runCommand('git rev-parse --short HEAD');
const isCommitTagged = runCommand('git describe --tags --abbrev=0 --exact-match') !== undefined;
@@ -25,11 +24,6 @@ const getTags = (env = process.env) => {
console.error('Failed to get git short SHA');
process.exit(1);
}
return {
shortSha: gitShortSHA,
isTagged: isCommitTagged
}
return isCommitTagged ? packageVersion : `${packageVersion}+${gitShaShort}`;
}
}
export default getTags;
};