UNPKG

6.93 kBMarkdownView Raw
1# travis-deploy-once
2
3Run a deployment script only once in the [Travis](https://travis-ci.org/) test matrix.
4
5[![Travis](https://img.shields.io/travis/semantic-release/travis-deploy-once.svg)](https://travis-ci.org/semantic-release/travis-deploy-once)
6[![Codecov](https://img.shields.io/codecov/c/github/semantic-release/travis-deploy-once.svg)](https://codecov.io/gh/semantic-release/travis-deploy-once)
7[![Greenkeeper badge](https://badges.greenkeeper.io/semantic-release/travis-deploy-once.svg)](https://greenkeeper.io/)
8
9## :warning: Deprecation Notice :warning:
10
11**This library is deprecated and will not be maintained. We recommend to use [Build Stages](https://docs.travis-ci.com/user/build-stages) instead. It’s a clearer and more flexible way to orchestrate jobs within a build.**
12
13## Overview
14
15For Travis builds running multiple jobs (to test with multiple [Node versions](https://docs.travis-ci.com/user/languages/javascript-with-nodejs/#Specifying-Node.js-versions) and/or [OSs](https://docs.travis-ci.com/user/multi-os)), `travis-deploy-once` run some code only once, after all other jobs have completed successfully.
16
17`travis-deploy-once` is usually used in the `after_success` step. But if you want your build to break in case the `travis-deploy-once` script returns an error, you can set it in the `script` or `before_script` step (see [Travis Build Lifecycle](https://docs.travis-ci.com/user/customizing-the-build/#The-Build-Lifecycle)).
18
19Your code will run only on the job identified as the build leader, which is determined as follow, by order of priority:
20- The job with the ID defined in the [-b](#-b---buildleaderid), [--buildLeaderId](#-b---buildleaderid) CLI options or the [buildLeaderId](#buildleaderid) API option or `BUILD_LEADER_ID` environment variable.
21- The job configured with the [latest Node version](https://docs.travis-ci.com/user/languages/javascript-with-nodejs/#Specifying-Node.js-versions) (`node_js: node`).
22- The job configured with the [lts Node version](https://docs.travis-ci.com/user/languages/javascript-with-nodejs/#Specifying-Node.js-versions) (`node_js: lts/*`).
23- The job with the highest node version
24- The job with the highest job number if none of the jobs specify a node version (see [#42](https://github.com/semantic-release/travis-deploy-once/pull/42))
25
26**Note**: If multiple jobs match, the one with the highest job ID (which corresponds to the last one defined in `.travis.yml`) will be identified as the build leader.
27
28## CLI
29
30```bash
31Usage: travis-deploy-once.js [script]
32```
33
34### CLI usage with script argument
35
36Run the `script` passed in the first argument only if the current job is the build leader and all other jobs are successful and return with the exit code of the script. Return with exit code `0` otherwise.
37
38In `.travis.yml`:
39
40```yaml
41language: node_js
42node_js:
43 - 8
44 - 6
45 - 4
46os:
47 - osx
48 - linux
49after_success:
50 - npm install -g travis-deploy-once
51 - travis-deploy-once "deploy-script --script-arg script-arg-value"
52```
53
54The script `deploy-script` will be called only for the node 8 job running on `linux`. It will be passed the arguments `--script-arg script-arg-value`.
55
56### CLI usage without script argument
57
58Return with exit code `0` if the current job is the build leader and all other jobs are successful. Return with exit code `1` otherwise.
59
60In `.travis.yml`:
61
62```yaml
63language: node_js
64node_js:
65 - 8
66 - 6
67 - 4
68os:
69 - osx
70 - linux
71after_success:
72 - npm install -g travis-deploy-once
73 - travis-deploy-once && deploy-script --script-arg script-arg-value
74```
75
76The script `deploy-script` will be called only for the node 8 job running on `linux`. It will be passed the arguments `--script-arg script-arg-value`.
77
78### CLI options
79
80#### -t, --githubToken
81
82Type: `String`
83Default: `GH_TOKEN` or `GITHUB_TOKEN` environment variable
84
85GitHub OAuth token.
86
87#### -b, --buildLeaderId
88
89Type: `Number`
90Default: `BUILD_LEADER_ID` environment variable
91
92Define which Travis job will run the script (build leader). If not defined the build leader will be the Travis job running on the highest Node version.
93
94#### -p, --pro
95
96Type: `Boolean`
97Default: `false`
98
99`true` to use [Travis Pro](https://travis-ci.com), `false` to use [Travis for Open Source](https://travis-ci.org).
100
101#### -u, --travis-url
102
103Type: `String`
104Default: `TRAVIS_URL` environment variable
105
106[Travis Enterprise](https://enterprise.travis-ci.com) URL. If defined, the [-p, --pro](#-p---pro) option will be ignored.
107
108**Note**: This is the URL of the API endpoint, for example `https://travis.example.com/api`.
109
110#### -h, --help
111
112Type: `Boolean`
113
114Show help.
115
116#### -v, --version
117
118Type: `Boolean`
119
120Show version number.
121
122## API
123
124### API usage
125
126```bash
127npm install --save travis-deploy-once
128```
129
130In the module `my-module`:
131
132```js
133const deployOnce = require('travis-deploy-once');
134
135try {
136 const result = await deployOnce({travisOpts: {pro: true}, githubToken: 'xxxxxx', buildLeaderId: 1});
137
138 if (result === true) deployMyThing();
139 if (result === false) console.log('Some job(s) failed');
140 if (result === null) console.log('Did not run as the build leader');
141} catch (err) {
142 // something went wrong, and err will tell you what
143}
144```
145
146In `.travis.yml`:
147
148```yaml
149language: node_js
150node_js:
151 - 8
152 - 6
153 - 4
154os:
155 - osx
156 - linux
157after_success:
158 - npm run my-module
159```
160
161The script `my-module` with be called for each node version on both OSs and `deployMyThing` will be called only for the node 8 job running on `linux`.
162
163### Function `deployOnce([options])`
164
165Returns a `Promise` that resolves to:
166- `true` if the current Travis job is the build leader, the current `script` phase is successful and all other job have completed successfully. => Your code can safely run.
167- `false` if at least one Travis job failed. => Your code should not run.
168- `null` if the current Travis job is **not** the build leader. => Your code should not run, and will be executed on the build leader.
169
170Throws an `Error` if:
171- It doesn't run on Travis.
172- The Github authentication token is missing.
173- The Github authentication token is not authorized with Travis.
174
175#### options
176
177Type: `Object`
178
179##### githubToken
180
181Type: `String`
182Default: `process.env.GH_TOKEN` or `process.env.GITHUB_TOKEN`
183
184GitHub OAuth token.
185
186##### buildLeaderId
187
188Type: `Number`
189Default: `process.env.BUILD_LEADER_ID`
190
191Define which Travis job will run the script (build leader). If not defined the build leader will be the Travis job running on the highest Node version.
192
193##### travisOpts
194
195Type: `Object`
196
197###### pro
198
199Type: `Boolean`
200Default: `false`
201
202`true` to use [Travis Pro](https://travis-ci.com), `false` to use [Travis for Open Source](https://travis-ci.org).
203
204###### enterprise
205
206Type: `String`
207Default: `process.env.TRAVIS_URL`
208
209[Travis Enterprise](https://enterprise.travis-ci.com) URL. If defined, the [pro](#pro) option will be ignored.
210
211**Note**: This is the URL of the API endpoint, for example `https://travis.example.com/api`.