Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(release): fix jenkins artifacting #988

Merged
merged 6 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)$/;
louis-bompart marked this conversation as resolved.
Show resolved Hide resolved
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,
};