Skip to content

Commit

Permalink
chore(release): fix jenkins artifacting (#988)
Browse files Browse the repository at this point in the history
<!-- For Coveo Employees only. Fill this section.

CDX-764

-->

## Proposed changes

- Fix ESM import (need file-ext)
- Recover tag from release (simpler)
- Infer file architecture from filename instead of 'redoing' oclif logic

## Testing

- [x] Manual Tests: Created a PAT and run it on my machine, checked the
artifacts folder.
  • Loading branch information
louis-bompart authored Oct 18, 2022
1 parent aebdeac commit 8685b37
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 55 deletions.
76 changes: 39 additions & 37 deletions scripts/get-release-artifact-and-commit-sha.mjs
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
import {downloadReleaseAssets} from './github-client';
import {getLastTag} from '@coveo/semantic-monorepo-tools';
import {
existsSync,
mkdirSync,
writeFileSync,
readdirSync,
copyFileSync,
} from 'fs';
import {resolve} from 'path';
getAssetsMetadataFromRelease,
getLastCliRelease,
getTagFromRelease,
} from './github-client.js';
import {mkdirSync, writeFileSync, readdirSync, copyFileSync} from 'fs';
import {resolve, join} from 'path';
import {execSync} from 'node:child_process';
import {spawnSync} from 'child_process';

async function main() {
const tag = await getLastTag('@coveo/cli@');
const release = await getLastCliRelease();
const tag = getTagFromRelease(release);
// This folder structure needs to be respected in order for the CLI update plugin to
// be able to do it's job properly.
const topLevelDirectory = './artifacts';
const subDirectoryForTarball = [
topLevelDirectory,
'versions',
tag.name.substring(1),
tag.commit.sha.substring(0, 7),
].join('/');
const subDirectoryForManifest = [
topLevelDirectory,
'channels',
'stable',
].join('/');
const getSubDirectoryForTarball = ({version, commitSHA}) =>
join(topLevelDirectory, 'versions', version, commitSHA);
const subDirectoryForManifest = join(topLevelDirectory, 'channels', 'stable');
const binariesMatcher =
/^coveo[_-]{1}(?<_version>v?\d+\.\d+\.\d+(-\d+)?)[_.-]{1}(?<_commitSHA>\w{7})[_-]{0,1}(\d+_)?(?<longExt>.*\.(exe|deb|pkg))$/;
/^coveo[_-]{1}(?<version>v?\d+\.\d+\.\d+(-\d+)?)[_.-]{1}(?<commitSHA>\w+)[_-]?(\d+_)?(?<longExt>.*\.(exe|deb|pkg))$/;
const manifestMatcher =
/^coveo-(?<_version>v?\d+\.\d+\.\d+(-\d+)?)-(?<_commitSHA>\w{7})-(?<targetSignature>.*-buildmanifest)$/;
const tarballMatcher = /\.tar\.[gx]z$/;
/^coveo-(?<version>v?\d+\.\d+\.\d+(-\d+)?)-(?<commitSHA>\w+)-(?<targetSignature>.*-buildmanifest)$/;
const tarballMatcher =
/^coveo-v?(?<version>\d+\.\d+\.\d+(-\d+)?)-(?<commitSHA>\w+)-(?<targetSignature>[\w-]+).tar\.[gx]z$/;

[topLevelDirectory, subDirectoryForManifest, subDirectoryForTarball].forEach(
(dir) => {
if (!existsSync(dir)) {
mkdirSync(dir, {recursive: true});
}
}
);

await downloadReleaseAssets(tag.name, (assetName) => {
const determineAssetLocation = (assetName) => {
if (assetName.match(tarballMatcher)) {
console.info(assetName, `--> ${subDirectoryForTarball}`);
return subDirectoryForTarball;
const match = tarballMatcher.exec(assetName);
const location = getSubDirectoryForTarball(match.groups);
console.info(assetName, `--> ${location}`);
return location;
} else if (assetName.match(manifestMatcher)) {
console.info(assetName, `--> ${subDirectoryForManifest}`);
return subDirectoryForManifest;
} else {
console.info(assetName, `--> ${topLevelDirectory}`);
return topLevelDirectory;
}
});
};

writeFileSync('latest-commit', tag.commit.sha);
const assets = await getAssetsMetadataFromRelease(release);
for (const asset of assets) {
console.info(
`Downloading asset ${asset.name} from ${asset.browser_download_url}.\nSize: ${asset.size} ...`
);
const directory = determineAssetLocation(asset.name);
mkdirSync(directory, {recursive: true});
execSync(
`curl -L ${asset.browser_download_url} --output ${directory}/${asset.name}`
);
}
// https://stackoverflow.com/a/1862542
const tagCommit = spawnSync('git', ['rev-list', '-n', '1', tag], {
encoding: 'utf-8',
}).stdout.trim();
writeFileSync('latest-commit', tagCommit);

readdirSync(topLevelDirectory, {withFileTypes: true}).forEach((file) => {
const match = binariesMatcher.exec(file.name);
Expand Down
33 changes: 15 additions & 18 deletions scripts/github-client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/** @type {import("@actions/github")} */
const github = require('@actions/github');
const {execSync} = require('child_process');
const octokit = github.getOctokit(process.env.GITHUB_CREDENTIALS);
const owner = 'coveo';
const repo = 'cli';
Expand Down Expand Up @@ -76,23 +75,19 @@ const createOrUpdateReleaseDescription = async (tag, body) => {
}
};

const downloadReleaseAssets = async (tag, determineAssetLocation) => {
const release = await octokit.rest.repos.getReleaseByTag({repo, owner, tag});
const assets = await octokit.rest.repos.listReleaseAssets({
owner,
repo,
release_id: release.data.id,
});
const getLastCliRelease = () =>
octokit.rest.repos.getLatestRelease({repo, owner});

assets.data.forEach((asset) => {
console.info(
`Downloading asset ${asset.name} from ${asset.browser_download_url}.\nSize: ${asset.size} ...`
);
const directory = determineAssetLocation(asset.name);
execSync(
`curl -L ${asset.browser_download_url} --output ${directory}/${asset.name}`
);
});
const getTagFromRelease = (release) => release.data.tag_name;

const getAssetsMetadataFromRelease = async (release) => {
return (
await octokit.rest.repos.listReleaseAssets({
owner,
repo,
release_id: release.data.id,
})
).data;
};

const getSnykCodeAlerts = () => {
Expand All @@ -114,6 +109,8 @@ module.exports = {
getBaseBranchName,
getLatestTag,
createOrUpdateReleaseDescription,
downloadReleaseAssets,
getAssetsMetadataFromRelease,
getSnykCodeAlerts,
getLastCliRelease,
getTagFromRelease,
};

0 comments on commit 8685b37

Please sign in to comment.