1 | # JavaScript API
|
2 |
|
3 | ## Usage
|
4 |
|
5 | ```js
|
6 | const semanticRelease = require('semantic-release');
|
7 | const {WritableStreamBuffer} = require('stream-buffers');
|
8 |
|
9 | const stdoutBuffer = WritableStreamBuffer();
|
10 | const stderrBuffer = WritableStreamBuffer();
|
11 |
|
12 | try {
|
13 | const result = await semanticRelease({
|
14 | // Core options
|
15 | branches: [
|
16 | '+([0-9])?(.{+([0-9]),x}).x',
|
17 | 'master',
|
18 | 'next',
|
19 | 'next-major',
|
20 | {name: 'beta', prerelease: true},
|
21 | {name: 'alpha', prerelease: true}
|
22 | ],
|
23 | repositoryUrl: 'https://github.com/me/my-package.git',
|
24 | // Shareable config
|
25 | extends: 'my-shareable-config',
|
26 | // Plugin options
|
27 | githubUrl: 'https://my-ghe.com',
|
28 | githubApiPathPrefix: '/api-prefix'
|
29 | }, {
|
30 | // Run semantic-release from `/path/to/git/repo/root` without having to change local process `cwd` with `process.chdir()`
|
31 | cwd: '/path/to/git/repo/root',
|
32 | // Pass the variable `MY_ENV_VAR` to semantic-release without having to modify the local `process.env`
|
33 | env: {...process.env, MY_ENV_VAR: 'MY_ENV_VAR_VALUE'},
|
34 | // Store stdout and stderr to use later instead of writing to `process.stdout` and `process.stderr`
|
35 | stdout: stdoutBuffer,
|
36 | stderr: stderrBuffer
|
37 | });
|
38 |
|
39 | if (result) {
|
40 | const {lastRelease, commits, nextRelease, releases} = result;
|
41 |
|
42 | console.log(`Published ${nextRelease.type} release version ${nextRelease.version} containing ${commits.length} commits.`);
|
43 |
|
44 | if (lastRelease.version) {
|
45 | console.log(`The last release was "${lastRelease.version}".`);
|
46 | }
|
47 |
|
48 | for (const release of releases) {
|
49 | console.log(`The release was published with plugin "${release.pluginName}".`);
|
50 | }
|
51 | } else {
|
52 | console.log('No release published.');
|
53 | }
|
54 |
|
55 | // Get stdout and stderr content
|
56 | const logs = stdoutBuffer.getContentsAsString('utf8');
|
57 | const errors = stderrBuffer.getContentsAsString('utf8');
|
58 | } catch (err) {
|
59 | console.error('The automated release failed with %O', err)
|
60 | }
|
61 | ```
|
62 |
|
63 | ## API
|
64 |
|
65 | ### semanticRelease([options], [config]) => Promise<Result>
|
66 |
|
67 | Run **semantic-release** and returns a `Promise` that resolves to a [Result](#result) object.
|
68 |
|
69 | #### options
|
70 |
|
71 | Type: `Object`
|
72 |
|
73 | **semantic-release** options.
|
74 |
|
75 | Can be used to set any [core option](../usage/configuration.md#configuration) or [plugin options](../usage/plugins.md#configuration).
|
76 |
|
77 | Each option, will take precedence over options configured in the [configuration file](../usage/configuration.md#configuration) and [shareable configurations](../usage/configuration.md#extends).
|
78 |
|
79 | #### config
|
80 |
|
81 | Type: `Object`
|
82 |
|
83 | **semantic-release** configuration specific for API usage.
|
84 |
|
85 | ##### cwd
|
86 |
|
87 | Type: `String`<br>
|
88 | Default: `process.cwd()`
|
89 |
|
90 | The current working directory to use. It should be configured to the root of the Git repository to release from.
|
91 |
|
92 | It allows to run **semantic-release** from a specific path without having to change the local process `cwd` with `process.chdir()`.
|
93 |
|
94 | ##### env
|
95 |
|
96 | Type: `Object`<br>
|
97 | Default: `process.env`
|
98 |
|
99 | The environment variables to use.
|
100 |
|
101 | It allows to run **semantic-release** with specific environment variables without having to modify the local `process.env`.
|
102 |
|
103 | ##### stdout
|
104 |
|
105 | Type: [`stream.Writable`](https://nodejs.org/api/stream.html#stream_writable_streams)<br>
|
106 | Default: `process.stdout`
|
107 |
|
108 | The [writable stream](https://nodejs.org/api/stream.html#stream_writable_streams) used to log information.
|
109 |
|
110 | It allows to configure **semantic-release** to write logs to a specific stream rather than the local `process.stdout`.
|
111 |
|
112 | ##### stderr
|
113 |
|
114 | Type: [`stream.Writable`](https://nodejs.org/api/stream.html#stream_writable_streams)<br>
|
115 | Default: `process.stderr`
|
116 |
|
117 | The [writable stream](https://nodejs.org/api/stream.html#stream_writable_streams) used to log errors.
|
118 |
|
119 | It allows to configure **semantic-release** to write errors to a specific stream rather than the local `process.stderr`.
|
120 |
|
121 | ### Result
|
122 |
|
123 | Type: `Object` `Boolean`<br>
|
124 |
|
125 | And object with [`lastRelease`](#lastrelease), [`nextRelease`](#nextrelease), [`commits`](#commits) and [`releases`](#releases) if a release is published or `false` if no release was published.
|
126 |
|
127 | #### lastRelease
|
128 |
|
129 | Type: `Object`
|
130 |
|
131 | Information related to the last release found:
|
132 |
|
133 | | Name | Type | Description |
|
134 | |---------|----------|-------------------------------------------------------------------------------------------------------------------------------------|
|
135 | | version | `String` | The version of the last release. |
|
136 | | gitHead | `String` | The sha of the last commit being part of the last release. |
|
137 | | gitTag | `String` | The [Git tag](https://git-scm.com/book/en/v2/Git-Basics-Tagging) associated with the last release. |
|
138 | | channel | `String` | The distribution channel on which the last release was initially made available (`undefined` for the default distribution channel). |
|
139 |
|
140 | **Notes**: If no previous release is found, `lastRelease` will be an empty `Object`.
|
141 |
|
142 | Example:
|
143 | ```js
|
144 | {
|
145 | gitHead: 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
|
146 | version: '1.0.0',
|
147 | gitTag: 'v1.0.0',
|
148 | channel: 'next'
|
149 | }
|
150 | ```
|
151 |
|
152 | #### commits
|
153 |
|
154 | Type: `Array<Object>`
|
155 |
|
156 | The list of commit included in the new release.<br>
|
157 | Each commit object has the following properties:
|
158 |
|
159 | | Name | Type | Description |
|
160 | |-----------------|----------|-------------------------------------------------|
|
161 | | commit | `Object` | The commit abbreviated and full hash. |
|
162 | | commit.long | `String` | The commit hash. |
|
163 | | commit.short | `String` | The commit abbreviated hash. |
|
164 | | tree | `Object` | The commit abbreviated and full tree hash. |
|
165 | | tree.long | `String` | The commit tree hash. |
|
166 | | tree.short | `String` | The commit abbreviated tree hash. |
|
167 | | author | `Object` | The commit author information. |
|
168 | | author.name | `String` | The commit author name. |
|
169 | | author.email | `String` | The commit author email. |
|
170 | | author.short | `String` | The commit author date. |
|
171 | | committer | `Object` | The committer information. |
|
172 | | committer.name | `String` | The committer name. |
|
173 | | committer.email | `String` | The committer email. |
|
174 | | committer.short | `String` | The committer date. |
|
175 | | subject | `String` | The commit subject. |
|
176 | | body | `String` | The commit body. |
|
177 | | message | `String` | The commit full message (`subject` and `body`). |
|
178 | | hash | `String` | The commit hash. |
|
179 | | committerDate | `String` | The committer date. |
|
180 |
|
181 | Example:
|
182 | ```js
|
183 | [
|
184 | {
|
185 | commit: {
|
186 | long: '68eb2c4d778050b0701136ca129f837d7ed494d2',
|
187 | short: '68eb2c4'
|
188 | },
|
189 | tree: {
|
190 | long: '7ab515d12bd2cf431745511ac4ee13fed15ab578',
|
191 | short: '7ab515d'
|
192 | },
|
193 | author: {
|
194 | name: 'Me',
|
195 | email: 'me@email.com',
|
196 | date: 2018-07-22T20:52:44.000Z
|
197 | },
|
198 | committer: {
|
199 | name: 'Me',
|
200 | email: 'me@email.com',
|
201 | date: 2018-07-22T20:52:44.000Z
|
202 | },
|
203 | subject: 'feat: a new feature',
|
204 | body: 'Description of the new feature',
|
205 | hash: '68eb2c4d778050b0701136ca129f837d7ed494d2',
|
206 | message: 'feat: a new feature\n\nDescription of the new feature',
|
207 | committerDate: 2018-07-22T20:52:44.000Z
|
208 | }
|
209 | ]
|
210 | ```
|
211 |
|
212 | #### nextRelease
|
213 |
|
214 | Type: `Object`
|
215 |
|
216 | Information related to the newly published release:
|
217 |
|
218 | | Name | Type | Description |
|
219 | |---------|----------|-------------------------------------------------------------------------------------------------------------------------------|
|
220 | | type | `String` | The [semver](https://semver.org) type of the release (`patch`, `minor` or `major`). |
|
221 | | version | `String` | The version of the new release. |
|
222 | | gitHead | `String` | The sha of the last commit being part of the new release. |
|
223 | | gitTag | `String` | The [Git tag](https://git-scm.com/book/en/v2/Git-Basics-Tagging) associated with the new release. |
|
224 | | notes | `String` | The release notes for the new release. |
|
225 | | channel | `String` | The distribution channel on which the next release will be made available (`undefined` for the default distribution channel). |
|
226 |
|
227 | Example:
|
228 | ```js
|
229 | {
|
230 | type: 'minor',
|
231 | gitHead: '68eb2c4d778050b0701136ca129f837d7ed494d2',
|
232 | version: '1.1.0',
|
233 | gitTag: 'v1.1.0',
|
234 | notes: 'Release notes for version 1.1.0...',
|
235 | channel : 'next'
|
236 | }
|
237 | ```
|
238 |
|
239 | #### releases
|
240 |
|
241 | Type: `Array<Object>`
|
242 |
|
243 | The list of releases published or made available to a distribution channel.<br>
|
244 | Each release object has the following properties:
|
245 |
|
246 | | Name | Type | Description |
|
247 | |------------|----------|----------------------------------------------------------------------------------------------------------------|
|
248 | | name | `String` | **Optional.** The release name, only if set by the corresponding `publish` plugin. |
|
249 | | url | `String` | **Optional.** The release URL, only if set by the corresponding `publish` plugin. |
|
250 | | type | `String` | The [semver](https://semver.org) type of the release (`patch`, `minor` or `major`). |
|
251 | | version | `String` | The version of the release. |
|
252 | | gitHead | `String` | The sha of the last commit being part of the release. |
|
253 | | gitTag | `String` | The [Git tag](https://git-scm.com/book/en/v2/Git-Basics-Tagging) associated with the release. |
|
254 | | notes | `String` | The release notes for the release. |
|
255 | | pluginName | `String` | The name of the plugin that published the release. |
|
256 | | channel | `String` | The distribution channel on which the release is available (`undefined` for the default distribution channel). |
|
257 |
|
258 | Example:
|
259 | ```js
|
260 | [
|
261 | {
|
262 | name: 'GitHub release',
|
263 | url: 'https://github.com/me/my-package/releases/tag/v1.1.0',
|
264 | type: 'minor',
|
265 | gitHead: '68eb2c4d778050b0701136ca129f837d7ed494d2',
|
266 | version: '1.1.0',
|
267 | gitTag: 'v1.1.0',
|
268 | notes: 'Release notes for version 1.1.0...',
|
269 | pluginName: '@semantic-release/github'
|
270 | channel: 'next'
|
271 | },
|
272 | {
|
273 | name: 'npm package (@latest dist-tag)',
|
274 | url: 'https://www.npmjs.com/package/my-package',
|
275 | type: 'minor',
|
276 | gitHead: '68eb2c4d778050b0701136ca129f837d7ed494d2',
|
277 | version: '1.1.0',
|
278 | gitTag: 'v1.1.0',
|
279 | notes: 'Release notes for version 1.1.0...',
|
280 | pluginName: '@semantic-release/npm'
|
281 | channel: 'next'
|
282 | }
|
283 | ]
|
284 | ```
|