Skip to content

Commit 9745480

Browse files
authored
feat(npm): publish to AWS CodeArtifact (#150)
Publishing to npm packages to AWS CodeArtifact requires a `NPM_TOKEN` which is valid only for 12 hours. AWS CLI can be used to create this temporary `NPM_TOKEN`. To automate publishing to AWS CodeArtifact, the `aws codeartifact login` command is added to the npm release script. It will be automatically executed when `NPM_REGISTRY` contains a AWS CodeArtifact URL. It is necessary to provide the credentials for AWS CLI, e.g. by using the environment variable. Other AWS CLI credential types are supported. For example AWS CLI will automatically use Instance profile credentials if the script is executed on EC2. The changes were tested manually by providing environment variables and CLI credentials file. The example npm package was successfully published to AWS CodeArtifact. This feature was implemented as discussed with @eladb in projen/projen#986.
1 parent d8e25e6 commit 9745480

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ You can also execute individual publishers:
4343

4444
## npm
4545

46-
Publishes all `*.tgz` files from `DIR` to [npmjs](npmjs.com) or [GitHub Packages](https://github.com/features/packages).
46+
Publishes all `*.tgz` files from `DIR` to [npmjs](npmjs.com), [GitHub Packages](https://github.com/features/packages) or [AWS CodeArtifact](https://aws.amazon.com/codeartifact/).
47+
48+
If AWS CodeArtifact is used as npm registry, a temporary npm authorization token is created using AWS CLI. Therefore, it is necessary to provide the necessary [configuration settings](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html), e.g. by passing access key ID and secret access key to this script.
4749

4850
**Usage:**
4951

@@ -57,9 +59,11 @@ npx jsii-release-npm [DIR]
5759

5860
|Option|Required|Description|
5961
|------|--------|-----------|
60-
|`NPM_TOKEN`|Required|Registry authentication token (either [npm.js publishing token](https://docs.npmjs.com/creating-and-viewing-authentication-tokens) or a [GitHub personal access token](https://help.github.com/en/packages/using-github-packages-with-your-projects-ecosystem/configuring-npm-for-use-with-github-packages#authenticating-to-github-packages))|
61-
|`NPM_REGISTRY`|Optional|The registry URL (defaults to "registry.npmjs.org"). Use "npm.pkg.github.com" to publish to GitHub Packages|
62+
|`NPM_TOKEN`|Optional|Registry authentication token (either [npm.js publishing token](https://docs.npmjs.com/creating-and-viewing-authentication-tokens) or a [GitHub personal access token](https://help.github.com/en/packages/using-github-packages-with-your-projects-ecosystem/configuring-npm-for-use-with-github-packages#authenticating-to-github-packages)), not used for AWS CodeArtifact|
63+
|`NPM_REGISTRY`|Optional|The registry URL (defaults to "registry.npmjs.org"). Use "npm.pkg.github.com" to publish to GitHub Packages. Use repository endpoint for AWS CodeAtifact, e.g. "my-domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/npm/my_repo/".|
6264
|`NPM_DIST_TAG`|Optional|Registers the published package with the given [dist-tag](https://docs.npmjs.com/cli/dist-tag) (e.g. `next`, default is `latest`)|
65+
|`AWS_ACCESS_KEY_ID`|Optional|If AWS CodeArtifact is used as registry, an AWS access key can be spedified.|
66+
|`AWS_SECRET_ACCESS_KEY`|Optional|Secret access key that belongs to the AWS access key.|
6367

6468
## Maven
6569

bin/jsii-release-npm

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,23 @@ set -eu
99
#
1010
# DIR: directory where npm tarballs are found (default is `dist/js`).
1111
#
12-
# NPM_TOKEN (required): registry authentication token (either from npmjs or a GitHub personal access token)
12+
# NPM_TOKEN (optional): registry authentication token (either from npmjs or a GitHub personal access token), not used for AWS CodeArtifact
1313
# NPM_REGISTRY (optional): the registry URL (defaults to "registry.npmjs.org")
14+
# AWS_ACCESS_KEY_ID (optional): If AWS CodeArtifact is used as registry, an AWS access key can be spedified.
15+
# AWS_SECRET_ACCESS_KEY (optional): Secret access key that belongs to the AWS access key.
1416
#
1517
###
1618

1719
dir="${1:-"dist/js"}"
1820

19-
if [ -z "${NPM_TOKEN:-}" ]; then
21+
22+
if ! [ -z "${NPM_REGISTRY:-}" ] && [[ $NPM_REGISTRY =~ .codeartifact.*.amazonaws.com ]]; then
23+
codeartifact_account="$(echo $NPM_REGISTRY | cut -d. -f1 | rev | cut -d- -f1 | rev)"
24+
codeartifact_subdomain="$(echo $NPM_REGISTRY | cut -d. -f1)"
25+
codeartifact_domain="$(echo $codeartifact_subdomain | cut -b -$((${#codeartifact_subdomain}-${#codeartifact_account}-1)))"
26+
codeartifact_region="$(echo $NPM_REGISTRY | cut -d. -f4)"
27+
NPM_TOKEN=`aws codeartifact get-authorization-token --domain $codeartifact_domain --domain-owner $codeartifact_account --region $codeartifact_region --query authorizationToken --output text`
28+
elif [ -z "${NPM_TOKEN:-}" ]; then
2029
echo "NPM_TOKEN is required"
2130
exit 1
2231
fi

0 commit comments

Comments
 (0)