Skip to content

Commit e848315

Browse files
committed
ci(CodeBuild): publish to NPM via CodeBuild
1 parent 01d410e commit e848315

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

codebuild/release/prod-release.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
version: 0.2
2+
3+
batch:
4+
fast-fail: true
5+
build-graph:
6+
# CI
7+
- identifier: nodejs10
8+
buildspec: codebuild/nodejs10.yml
9+
env:
10+
compute-type: BUILD_GENERAL1_MEDIUM
11+
- identifier: nodejs12
12+
buildspec: codebuild/nodejs12.yml
13+
env:
14+
image: aws/codebuild/standard:5.0
15+
compute-type: BUILD_GENERAL1_MEDIUM
16+
17+
# Version the project and push git commits and tags
18+
- identifier: version
19+
depend-on:
20+
- nodejs10
21+
- nodejs12
22+
buildspec: codebuild/release/version.yml
23+
env:
24+
image: aws/codebuild/standard:5.0
25+
26+
# Publish the release to npm
27+
- identifier: publish
28+
depend-on:
29+
- version
30+
buildspec: codebuild/release/publish.yml
31+
env:
32+
image: aws/codebuild/standard:5.0

codebuild/release/publish.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
version: 0.2
2+
3+
env:
4+
variables:
5+
NODE_OPTIONS: "--max-old-space-size=4096"
6+
BRANCH: "mainline-1.x"
7+
secrets-manager:
8+
OTP_SECRET_KEY: npm/aws-crypto-tools-ci-bot/2FA:OTP_SECRET_KEY
9+
NPM_TOKEN: npm/aws-crypto-tools-ci-bot/2FA:NPM_TOKEN
10+
11+
phases:
12+
install:
13+
commands:
14+
- npm ci --unsafe-perm
15+
# Install `otplib` to extract the OTP from the npm 2FA secret
16+
- npm install otplib --no-save
17+
- npm run build
18+
runtime-versions:
19+
nodejs: 12
20+
pre_build:
21+
commands:
22+
- git checkout $BRANCH
23+
build:
24+
commands:
25+
# Extract the otp using the secrets environment variables from above.
26+
# This will wait for the next token. This is because npm uses
27+
# TOTP and the tokens time out after 30 seconds. If the process just
28+
# extracted the token then the lifetime for this token
29+
# would be very random. This will maximize the amount of time
30+
# available on the OTP to publish.
31+
- >-
32+
OTP=`node -e "
33+
auth=require('otplib').authenticator;
34+
setTimeout(() =>
35+
console.log(auth.generate(process.env.OTP_SECRET_KEY)),
36+
auth.timeRemaining() * 1000);
37+
"`
38+
# npm will only expand env vars inside .npmrc
39+
# NOTE the ' this is to keep the env var NPM_TOKEN from expanding!
40+
- echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > .npmrc
41+
# Now we publish to npm.
42+
# This is going to use the OTP generated above and the NPM_TOKEN
43+
# environment variable. This will only publish things that are
44+
# missing from npm. It is therefore safe to run repeatedly.
45+
- npx lerna publish from-package --yes --otp $OTP
46+
# remove after publishing
47+
- rm .npmrc
48+

codebuild/release/version.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: 0.2
2+
3+
env:
4+
variables:
5+
NODE_OPTIONS: "--max-old-space-size=4096"
6+
BRANCH: "mainline-1.x"
7+
# An explicit version bump
8+
VERSION_BUMP: ""
9+
git-credential-helper: yes
10+
11+
phases:
12+
install:
13+
commands:
14+
- npm ci --unsafe-perm
15+
runtime-versions:
16+
nodejs: 12
17+
pre_build:
18+
commands:
19+
- git config --global user.name "aws-crypto-tools-ci-bot"
20+
- git config --global user.email "no-reply@noemail.local"
21+
- git checkout $BRANCH
22+
build:
23+
commands:
24+
# Generate new version and CHANGELOG entry and push it
25+
- npx lerna version --conventional-commits --git-remote origin --yes ${VERSION_BUMP:+$VERSION_BUMP --force-publish}
26+
# Log the commit for posterity
27+
- git log -n 1

0 commit comments

Comments
 (0)