Skip to content

Commit

Permalink
build(alpha-test): sets up alpha testing on uber applications (uber#3573
Browse files Browse the repository at this point in the history
)

* feat(alpha-test): wip

* wip

* fix(baseui-alpha-task): use create_revision util

* chore(alpha-tests): cleans up script

* fix(alpha-test): lint

* fix(alpha-test): propagate bk api through docker-compose
  • Loading branch information
chasestarr authored Aug 5, 2020
1 parent f74a1b0 commit 23de386
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 21 deletions.
8 changes: 4 additions & 4 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ steps:
queue: workers

- name: deploy-npm-latest
command: 'TAG=latest node scripts/publish-to-npm.js'
command: 'node scripts/publish-stable-release.js'
if: build.branch == "master" && build.message =~ /^Release v[0-9]+\.[0-9]+\.[0-9]+/
plugins:
'docker-compose#v3.0.3':
run: baseui
agents:
queue: workers

- name: deploy-npm-alpha
command: 'GIT_COMMIT=$BUILDKITE_COMMIT TAG=alpha node scripts/publish-to-npm.js'
if: build.message =~ /^Release v[0-9]+\.[0-9]+\.[0-9]+/
- name: alpha-test-web-code
command: 'node scripts/alpha-test-web-code.js'
if: build.branch != "master" && build.message =~ /^Release v[0-9]+\.[0-9]+\.[0-9]+/
plugins:
'docker-compose#v3.0.3':
run: baseui
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ services:
- CODECOV_TOKEN
- CI=true
- BUILDKITE
- BUILDKITE_API_TOKEN
- BUILDKITE_BRANCH
- BUILDKITE_BUILD_NUMBER
- BUILDKITE_JOB_ID
Expand Down
105 changes: 105 additions & 0 deletions scripts/alpha-test-web-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright (c) 2018-2020 Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/

// Sets local package.json to current baseui version.

/* eslint-env node */
// @flow

const fetch = require('node-fetch').default;
const publishToNpm = require('./publish-to-npm.js');

const BUILDKITE_TASK_RUNNER_URL =
'https://api.buildkite.com/v2/organizations/uber/pipelines/web-code-task-runner/builds';

function wait(ms) {
return new Promise(res => setTimeout(res, ms));
}

async function createBuild(token, version) {
const body = {
commit: 'HEAD',
branch: 'master',
message: '[alpha-test-baseui] Triggered from CI',
meta_data: {task: 'baseui-alpha-test'},
env: {
TASK: 'baseui-alpha-test',
TASK_ENV: `PACKAGE_VERSION=baseui@${version}`,
},
};

const createBuildResponse = await fetch(BUILDKITE_TASK_RUNNER_URL, {
method: 'post',
body: JSON.stringify(body),
headers: {Authorization: `Bearer ${token}`},
});

if (!createBuildResponse.ok) {
throw new Error(
`Failed to create build. Status: ${createBuildResponse.status}. Message: ${createBuildResponse.statusText}`,
);
}

const json = await createBuildResponse.json();
return json;
}

async function getBuild(token, number) {
const getBuildResponse = await fetch(
`${BUILDKITE_TASK_RUNNER_URL}/${number}`,
{headers: {Authorization: `Bearer ${token}`}},
);

if (!getBuildResponse.ok) {
throw new Error(
`Failed to create build. Status: ${getBuildResponse.status}. Message: ${getBuildResponse.statusText}`,
);
}

const json = await getBuildResponse.json();
return json;
}

async function main() {
const buildkiteToken = process.env.BUILDKITE_API_TOKEN;

if (!buildkiteToken) {
throw new Error(
'Failed to alpha test web-code. BUILDKITE_API_TOKEN env var not set.',
);
}

const {version} = publishToNpm({
tag: 'alpha',
commit: process.env.BUILDKITE_COMMIT,
});

const {web_url, number} = await createBuild(buildkiteToken, version);

console.log(`View alpha build CI checks at ${web_url}.`);

// eslint-disable-next-line no-constant-condition
while (true) {
await wait(60 * 1000);
const {state} = await getBuild(buildkiteToken, number);

if (state === 'passed') {
console.log('Alpha build passed CI checks.');
break;
} else if (state === 'running' || state === 'scheduled') {
console.log(
`Alpha build CI checks are ${state}. Waiting 60s before polling again.`,
);
} else {
throw new Error(
`Alpha build CI checks failed with ${state || 'UNDEFINED'} state.`,
);
}
}
}

main();
14 changes: 14 additions & 0 deletions scripts/publish-stable-release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Copyright (c) 2018-2020 Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/

// Sets local package.json to current baseui version.

/* eslint-env node */
// @flow

const publishToNpm = require('./publish-to-npm.js');
publishToNpm({tag: 'latest'});
33 changes: 16 additions & 17 deletions scripts/publish-to-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,39 +71,38 @@ function publishEslintPlugin(tag) {
});
}

function main() {
if (!process.env.TAG) {
throw new Error(
`Must specify a TAG environment variable. Use 'alpha' for preleases or 'latest' for stable releases.`,
);
}
const rootPackageJSONPath = path.resolve(ROOT_DIR, 'package.json');

module.exports = function publishToNpm(params /*: any */) {
const {tag, commit} = params;

const tag = process.env.TAG;
if (tag !== ALPHA_TAG && tag !== LATEST_TAG) {
throw new Error(
`NPM tag ${tag} must be either ${ALPHA_TAG} or ${LATEST_TAG}.`,
);
}

if (tag === ALPHA_TAG) {
console.log('--- Updating package.json version to alpha.');
const commitHash = process.env.GIT_COMMIT;
if (!commitHash) {
throw new Error('No GIT_COMMIT environment variable set.');
console.log('+++ Updating package.json version to alpha.');
if (!commit) {
throw new Error(
'Must provide a commit param to publish an alpha release.',
);
}
const packageJSONPath = path.resolve(ROOT_DIR, 'package.json');
const packageJSON = readJSONFile(packageJSONPath);
const shortHash = commitHash.slice(-7);
const packageJSON = readJSONFile(rootPackageJSONPath);
const shortHash = commit.slice(-7);
packageJSON.version = `0.0.0-alpha-${shortHash}`;
console.log(
`Updated package.json version to ${packageJSON.version} for alpha release.`,
);
fs.writeFileSync(packageJSONPath, JSON.stringify(packageJSON, null, 2));
fs.writeFileSync(rootPackageJSONPath, JSON.stringify(packageJSON, null, 2));
}

writeNpmTokenFromEnv();
publishBaseui(tag);
publishEslintPlugin(tag);
}

main();
// returns the final version so that alpha-test-web-code script can kick off a test with the release
const packageJSON = readJSONFile(rootPackageJSONPath);
return packageJSON.version;
};

0 comments on commit 23de386

Please sign in to comment.