JavaScript API

Usage

1
const semanticRelease = require('semantic-release');
2
const {WritableStreamBuffer} = require('stream-buffers');
3
​
4
const stdoutBuffer = WritableStreamBuffer();
5
const stderrBuffer = WritableStreamBuffer();
6
​
7
try {
8
const result = await semanticRelease({
9
// Core options
10
branches: [
11
'+([0-9])?(.{+([0-9]),x}).x',
12
'master',
13
'next',
14
'next-major',
15
{name: 'beta', prerelease: true},
16
{name: 'alpha', prerelease: true}
17
],
18
repositoryUrl: 'https://github.com/me/my-package.git',
19
// Shareable config
20
extends: 'my-shareable-config',
21
// Plugin options
22
githubUrl: 'https://my-ghe.com',
23
githubApiPathPrefix: '/api-prefix'
24
}, {
25
// Run semantic-release from `/path/to/git/repo/root` without having to change local process `cwd` with `process.chdir()`
26
cwd: '/path/to/git/repo/root',
27
// Pass the variable `MY_ENV_VAR` to semantic-release without having to modify the local `process.env`
28
env: {...process.env, MY_ENV_VAR: 'MY_ENV_VAR_VALUE'},
29
// Store stdout and stderr to use later instead of writing to `process.stdout` and `process.stderr`
30
stdout: stdoutBuffer,
31
stderr: stderrBuffer
32
});
33
​
34
if (result) {
35
const {lastRelease, commits, nextRelease, releases} = result;
36
​
37
console.log(`Published ${nextRelease.type} release version ${nextRelease.version} containing ${commits.length} commits.`);
38
​
39
if (lastRelease.version) {
40
console.log(`The last release was "${lastRelease.version}".`);
41
}
42
​
43
for (const release of releases) {
44
console.log(`The release was published with plugin "${release.pluginName}".`);
45
}
46
} else {
47
console.log('No release published.');
48
}
49
​
50
// Get stdout and stderr content
51
const logs = stdoutBuffer.getContentsAsString('utf8');
52
const errors = stderrBuffer.getContentsAsString('utf8');
53
} catch (err) {
54
console.error('The automated release failed with %O', err)
55
}
Copied!

API

semanticRelease([options], [config]) => Promise

Run semantic-release and returns a Promise that resolves to a Result object.

options

Type: Object
semantic-release options.
Can be used to set any core option or plugin options.
Each option, will take precedence over options configured in the configuration file and shareable configurations.

config

Type: Object
semantic-release configuration specific for API usage.
cwd
Type: String Default: process.cwd()
The current working directory to use. It should be configured to the root of the Git repository to release from.
It allows to run semantic-release from a specific path without having to change the local process cwd with process.chdir().
env
Type: Object Default: process.env
The environment variables to use.
It allows to run semantic-release with specific environment variables without having to modify the local process.env.
stdout
Type: stream.Writable Default: process.stdout
The writable stream used to log information.
It allows to configure semantic-release to write logs to a specific stream rather than the local process.stdout.
stderr
Type: stream.Writable Default: process.stderr
The writable stream used to log errors.
It allows to configure semantic-release to write errors to a specific stream rather than the local process.stderr.

Result

Type: Object Boolean
And object with lastRelease, nextRelease, commits and releases if a release is published or false if no release was published.

lastRelease

Type: Object
Information related to the last release found:
Name
Type
Description
version
String
The version of the last release.
gitHead
String
The sha of the last commit being part of the last release.
gitTag
String
The Git tag associated with the last release.
channel
String
The distribution channel on which the last release was initially made available (undefined for the default distribution channel).
Notes: If no previous release is found, lastRelease will be an empty Object.
Example:
1
{
2
gitHead: 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
3
version: '1.0.0',
4
gitTag: 'v1.0.0',
5
channel: 'next'
6
}
Copied!

commits

Type: Array<Object>
The list of commit included in the new release. Each commit object has the following properties:
Name
Type
Description
commit
Object
The commit abbreviated and full hash.
commit.long
String
The commit hash.
commit.short
String
The commit abbreviated hash.
tree
Object
The commit abbreviated and full tree hash.
tree.long
String
The commit tree hash.
tree.short
String
The commit abbreviated tree hash.
author
Object
The commit author information.
author.name
String
The commit author name.
author.email
String
The commit author email.
author.short
String
The commit author date.
committer
Object
The committer information.
committer.name
String
The committer name.
committer.email
String
The committer email.
committer.short
String
The committer date.
subject
String
The commit subject.
body
String
The commit body.
message
String
The commit full message (subject and body).
hash
String
The commit hash.
committerDate
String
The committer date.
Example:
1
[
2
{
3
commit: {
4
long: '68eb2c4d778050b0701136ca129f837d7ed494d2',
5
short: '68eb2c4'
6
},
7
tree: {
8
long: '7ab515d12bd2cf431745511ac4ee13fed15ab578',
9
short: '7ab515d'
10
},
11
author: {
12
name: 'Me',
13
14
date: 2018-07-22T20:52:44.000Z
15
},
16
committer: {
17
name: 'Me',
18
19
date: 2018-07-22T20:52:44.000Z
20
},
21
subject: 'feat: a new feature',
22
body: 'Description of the new feature',
23
hash: '68eb2c4d778050b0701136ca129f837d7ed494d2',
24
message: 'feat: a new feature\n\nDescription of the new feature',
25
committerDate: 2018-07-22T20:52:44.000Z
26
}
27
]
Copied!

nextRelease

Type: Object
Information related to the newly published release:
Name
Type
Description
type
String
The semver type of the release (patch, minor or major).
version
String
The version of the new release.
gitHead
String
The sha of the last commit being part of the new release.
gitTag
String
The Git tag associated with the new release.
notes
String
The release notes for the new release.
channel
String
The distribution channel on which the next release will be made available (undefined for the default distribution channel).
Example:
1
{
2
type: 'minor',
3
gitHead: '68eb2c4d778050b0701136ca129f837d7ed494d2',
4
version: '1.1.0',
5
gitTag: 'v1.1.0',
6
notes: 'Release notes for version 1.1.0...',
7
channel : 'next'
8
}
Copied!

releases

Type: Array<Object>
The list of releases published or made available to a distribution channel. Each release object has the following properties:
Name
Type
Description
name
String
Optional. The release name, only if set by the corresponding publish plugin.
url
String
Optional. The release URL, only if set by the corresponding publish plugin.
type
String
The semver type of the release (patch, minor or major).
version
String
The version of the release.
gitHead
String
The sha of the last commit being part of the release.
gitTag
String
The Git tag associated with the release.
notes
String
The release notes for the release.
pluginName
String
The name of the plugin that published the release.
channel
String
The distribution channel on which the release is available (undefined for the default distribution channel).
Example:
1
[
2
{
3
name: 'GitHub release',
4
url: 'https://github.com/me/my-package/releases/tag/v1.1.0',
5
type: 'minor',
6
gitHead: '68eb2c4d778050b0701136ca129f837d7ed494d2',
7
version: '1.1.0',
8
gitTag: 'v1.1.0',
9
notes: 'Release notes for version 1.1.0...',
10
pluginName: '@semantic-release/github'
11
channel: 'next'
12
},
13
{
14
name: 'npm package (@latest dist-tag)',
15
url: 'https://www.npmjs.com/package/my-package',
16
type: 'minor',
17
gitHead: '68eb2c4d778050b0701136ca129f837d7ed494d2',
18
version: '1.1.0',
19
gitTag: 'v1.1.0',
20
notes: 'Release notes for version 1.1.0...',
21
pluginName: '@semantic-release/npm'
22
channel: 'next'
23
}
24
]
Copied!
Last modified 25d ago