Skip to content

Commit

Permalink
feat(release): allow specifying npm dist-tag per branch (#1314)
Browse files Browse the repository at this point in the history
Adds `npmDistTag` to release branch definition which will be configured in the release workflow triggered for this branch.

Implementation notes: to support this we needed to refactor the publisher so that when its jobs are rendered, they have the branch context. This also means that now we can ensire that when a publish command is executed manually, it will be done only on the correct release branch.

The root `npmDistTag` setting applies to the default branch.

BREAKING CHANGE: `npmDistTag` only effects publishing (and supported for each release branch) setting and so it is no longer set in `package.json`.
* `workflow.addJobsLater()` is no longer supported. Use `postSynthesis` hooks and call `.addJobs()` as needed.
* `publish:xxxx` tasks for automated releases are not defined by default since normally they should only be executed from within workflows. Set `publishingTasks: true` to create them.

---
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
Elad Ben-Israel committed Dec 9, 2021
1 parent 4d2d4c2 commit 78bf2e4
Show file tree
Hide file tree
Showing 17 changed files with 960 additions and 490 deletions.
62 changes: 0 additions & 62 deletions .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

152 changes: 85 additions & 67 deletions API.md

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/cdk/jsii-project.ts
Expand Up @@ -209,7 +209,6 @@ export class JsiiProject extends TypeScriptProject {

if (options.releaseToNpm != false) {
this.publisher?.publishToNpm({
distTag: this.package.npmDistTag,
registry: this.package.npmRegistry,
npmTokenSecret: this.package.npmTokenSecret,
});
Expand Down
24 changes: 1 addition & 23 deletions src/github/workflows.ts
Expand Up @@ -52,7 +52,6 @@ export class GithubWorkflow extends Component {

private events: workflows.Triggers = { };
private jobs: Record<string, workflows.Job> = { };
private _providers = new Array<IJobProvider>();

constructor(github: GitHub, name: string, options: GithubWorkflowOptions = {}) {
super(github.project);
Expand Down Expand Up @@ -108,33 +107,12 @@ export class GithubWorkflow extends Component {
};
}

/**
* Add jobs from a dynamic source. Useful if a component creates jobs that
* may not be all available until project synthesis time.
*
* @param provider Source of jobs
*/
public addJobsLater(provider: IJobProvider) {
this._providers.push(provider);
}

private renderWorkflow() {
const allJobs = { ...this.jobs };

for (const provider of this._providers) {
for (const [name, job] of Object.entries(provider.renderJobs())) {
if (name in allJobs) {
throw new Error(`A job named ${name} already exists in workflow ${this.name}`);
}
allJobs[name] = job;
}
}

return {
name: this.name,
on: snakeCaseKeys(this.events),
concurrency: this.concurrency,
jobs: renderJobs(allJobs),
jobs: renderJobs(this.jobs),
};
}
}
Expand Down
29 changes: 1 addition & 28 deletions src/javascript/node-package.ts
Expand Up @@ -13,7 +13,6 @@ import { exec, isTruthy, sorted, writeFile } from '../util';

const UNLICENSED = 'UNLICENSED';
const DEFAULT_NPM_REGISTRY_URL = 'https://registry.npmjs.org/';
const DEFAULT_NPM_TAG = 'latest';
const GITHUB_PACKAGES_REGISTRY = 'npm.pkg.github.com';
const DEFAULT_NPM_TOKEN_SECRET = 'NPM_TOKEN';
const DEFAULT_GITHUB_TOKEN_SECRET = 'GITHUB_TOKEN';
Expand Down Expand Up @@ -233,24 +232,6 @@ export interface NodePackageOptions {
*/
readonly licensed?: boolean;

/**
* Tags can be used to provide an alias instead of version numbers.
*
* For example, a project might choose to have multiple streams of development
* and use a different tag for each stream, e.g., stable, beta, dev, canary.
*
* By default, the `latest` tag is used by npm to identify the current version
* of a package, and `npm install <pkg>` (without any `@<version>` or `@<tag>`
* specifier) installs the latest tag. Typically, projects only use the
* `latest` tag for stable release versions, and use other tags for unstable
* versions such as prereleases.
*
* The `next` tag is used by some projects to identify the upcoming version.
*
* @default "latest"
*/
readonly npmDistTag?: string;

/**
* The base URL of the npm package registry.
*
Expand Down Expand Up @@ -364,11 +345,6 @@ export class NodePackage extends Component {
*/
public readonly license?: string;

/**
* npm distribution tag
*/
public readonly npmDistTag: string;

/**
* npm registry (e.g. `https://registry.npmjs.org`). Use `npmRegistryHost` to get just the host name.
*/
Expand Down Expand Up @@ -421,9 +397,8 @@ export class NodePackage extends Component {
this.project.annotateGenerated(`/${this.lockFile}`);

const {
npmDistTag, npmAccess, npmRegistry, npmRegistryUrl, npmTokenSecret, codeArtifactOptions,
npmAccess, npmRegistry, npmRegistryUrl, npmTokenSecret, codeArtifactOptions,
} = this.parseNpmOptions(options);
this.npmDistTag = npmDistTag;
this.npmAccess = npmAccess;
this.npmRegistry = npmRegistry;
this.npmRegistryUrl = npmRegistryUrl;
Expand Down Expand Up @@ -788,7 +763,6 @@ export class NodePackage extends Component {
}

return {
npmDistTag: options.npmDistTag ?? DEFAULT_NPM_TAG,
npmAccess,
npmRegistry: npmr.hostname + this.renderNpmRegistryPath(npmr.pathname!),
npmRegistryUrl: npmr.href,
Expand Down Expand Up @@ -1005,7 +979,6 @@ export class NodePackage extends Component {
// omit values if they are the same as the npm defaults
return resolveJson({
registry: this.npmRegistryUrl !== DEFAULT_NPM_REGISTRY_URL ? this.npmRegistryUrl : undefined,
tag: this.npmDistTag !== DEFAULT_NPM_TAG ? this.npmDistTag : undefined,
access: this.npmAccess !== defaultNpmAccess(this.packageName) ? this.npmAccess : undefined,
}, { omitEmpty: true });
}
Expand Down
14 changes: 0 additions & 14 deletions src/javascript/node-project.ts
Expand Up @@ -360,16 +360,6 @@ export class NodeProject extends GitHubProject {
*/
public readonly antitamper: boolean;

/**
* @deprecated use `package.npmDistTag`
*/
protected readonly npmDistTag: string;

/**
* @deprecated use `package.npmRegistry`
*/
protected readonly npmRegistry: string;

/**
* The package manager to use.
*
Expand Down Expand Up @@ -424,9 +414,6 @@ export class NodeProject extends GitHubProject {

this.addLicense(options);

this.npmDistTag = this.package.npmDistTag;
this.npmRegistry = this.package.npmRegistry;

if (options.npmignoreEnabled ?? true) {
this.npmignore = new IgnoreFile(this, '.npmignore');
}
Expand Down Expand Up @@ -623,7 +610,6 @@ export class NodeProject extends GitHubProject {

if (options.releaseToNpm ?? false) {
this.release.publisher.publishToNpm({
distTag: this.package.npmDistTag,
registry: this.package.npmRegistry,
npmTokenSecret: this.package.npmTokenSecret,
codeArtifactOptions: {
Expand Down

0 comments on commit 78bf2e4

Please sign in to comment.