Skip to content
Merged
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
44 changes: 24 additions & 20 deletions scripts/release/shared-commands/download-build-artifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const {join} = require('path');
const theme = require('../theme');
const {exec} = require('child-process-promise');
const {existsSync} = require('fs');
const {existsSync, readFileSync} = require('fs');
const {logPromise} = require('../utils');

if (process.env.GH_TOKEN == null) {
Expand Down Expand Up @@ -39,18 +39,16 @@ function getWorkflowId() {

async function getWorkflowRun(commit) {
const res = await exec(
`curl -L ${GITHUB_HEADERS} https://github.com/api/repos/${OWNER}/${REPO}/actions/workflows/${getWorkflowId()}/runs?head_sha=${commit}&branch=main&exclude_pull_requests=true`
`curl -L ${GITHUB_HEADERS} https://github.com/api/repos/${OWNER}/${REPO}/actions/workflows/${getWorkflowId()}/runs?head_sha=${commit}`
);

const json = JSON.parse(res.stdout);
let workflowRun;
if (json.total_count === 1) {
workflowRun = json.workflow_runs[0];
} else {
workflowRun = json.workflow_runs.find(
run => run.head_sha === commit && run.head_branch === 'main'
);
}
const workflowRun = json.workflow_runs.find(
run =>
run.head_sha === commit &&
run.status === 'completed' &&
run.conclusion === 'success'
);

if (workflowRun == null || workflowRun.id == null) {
console.log(
Expand All @@ -68,14 +66,9 @@ async function getArtifact(workflowRunId, artifactName) {
);

const json = JSON.parse(res.stdout);
let artifact;
if (json.total_count === 1) {
artifact = json.artifacts[0];
} else {
artifact = json.artifacts.find(
_artifact => _artifact.name === artifactName
);
}
const artifact = json.artifacts.find(
_artifact => _artifact.name === artifactName
);

if (artifact == null) {
console.log(
Expand All @@ -87,7 +80,7 @@ async function getArtifact(workflowRunId, artifactName) {
return artifact;
}

async function processArtifact(artifact, releaseChannel) {
async function processArtifact(artifact, commit, releaseChannel) {
// Download and extract artifact
const cwd = join(__dirname, '..', '..', '..');
await exec(`rm -rf ./build`, {cwd});
Expand Down Expand Up @@ -124,6 +117,17 @@ async function processArtifact(artifact, releaseChannel) {
await exec(`cp -r ./build/${sourceDir} ./build/node_modules`, {
cwd,
});

// Validate artifact
const buildSha = readFileSync('./build/COMMIT_SHA', 'utf8').replace(
/[\u0000-\u001F\u007F-\u009F]/g,
''
);
if (buildSha !== commit) {
throw new Error(
`Requested commit sha does not match downloaded artifact. Expected: ${commit}, got: ${buildSha}`
);
}
}

async function downloadArtifactsFromGitHub(commit, releaseChannel) {
Expand All @@ -148,7 +152,7 @@ async function downloadArtifactsFromGitHub(commit, releaseChannel) {
workflowRun.id,
'artifacts_combined'
);
await processArtifact(artifact, releaseChannel);
await processArtifact(artifact, commit, releaseChannel);
return;
} else {
console.log(
Expand Down
Loading