1 | # travis-deploy-once
|
2 |
|
3 | Run a deployment script only once in the [Travis](https://travis-ci.org/) test matrix.
|
4 |
|
5 | [](https://travis-ci.org/semantic-release/travis-deploy-once)
|
6 | [](https://codecov.io/gh/semantic-release/travis-deploy-once)
|
7 | [](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 |
|
15 | For 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 |
|
19 | Your 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
|
31 | Usage: travis-deploy-once.js [script]
|
32 | ```
|
33 |
|
34 | ### CLI usage with script argument
|
35 |
|
36 | Run 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 |
|
38 | In `.travis.yml`:
|
39 |
|
40 | ```yaml
|
41 | language: node_js
|
42 | node_js:
|
43 | - 8
|
44 | - 6
|
45 | - 4
|
46 | os:
|
47 | - osx
|
48 | - linux
|
49 | after_success:
|
50 | - npm install -g travis-deploy-once
|
51 | - travis-deploy-once "deploy-script --script-arg script-arg-value"
|
52 | ```
|
53 |
|
54 | The 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 |
|
58 | Return 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 |
|
60 | In `.travis.yml`:
|
61 |
|
62 | ```yaml
|
63 | language: node_js
|
64 | node_js:
|
65 | - 8
|
66 | - 6
|
67 | - 4
|
68 | os:
|
69 | - osx
|
70 | - linux
|
71 | after_success:
|
72 | - npm install -g travis-deploy-once
|
73 | - travis-deploy-once && deploy-script --script-arg script-arg-value
|
74 | ```
|
75 |
|
76 | The 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 |
|
82 | Type: `String`
|
83 | Default: `GH_TOKEN` or `GITHUB_TOKEN` environment variable
|
84 |
|
85 | GitHub OAuth token.
|
86 |
|
87 | #### -b, --buildLeaderId
|
88 |
|
89 | Type: `Number`
|
90 | Default: `BUILD_LEADER_ID` environment variable
|
91 |
|
92 | Define 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 |
|
96 | Type: `Boolean`
|
97 | Default: `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 |
|
103 | Type: `String`
|
104 | Default: `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 |
|
112 | Type: `Boolean`
|
113 |
|
114 | Show help.
|
115 |
|
116 | #### -v, --version
|
117 |
|
118 | Type: `Boolean`
|
119 |
|
120 | Show version number.
|
121 |
|
122 | ## API
|
123 |
|
124 | ### API usage
|
125 |
|
126 | ```bash
|
127 | npm install --save travis-deploy-once
|
128 | ```
|
129 |
|
130 | In the module `my-module`:
|
131 |
|
132 | ```js
|
133 | const deployOnce = require('travis-deploy-once');
|
134 |
|
135 | try {
|
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 |
|
146 | In `.travis.yml`:
|
147 |
|
148 | ```yaml
|
149 | language: node_js
|
150 | node_js:
|
151 | - 8
|
152 | - 6
|
153 | - 4
|
154 | os:
|
155 | - osx
|
156 | - linux
|
157 | after_success:
|
158 | - npm run my-module
|
159 | ```
|
160 |
|
161 | The 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 |
|
165 | Returns 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 |
|
170 | Throws 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 |
|
177 | Type: `Object`
|
178 |
|
179 | ##### githubToken
|
180 |
|
181 | Type: `String`
|
182 | Default: `process.env.GH_TOKEN` or `process.env.GITHUB_TOKEN`
|
183 |
|
184 | GitHub OAuth token.
|
185 |
|
186 | ##### buildLeaderId
|
187 |
|
188 | Type: `Number`
|
189 | Default: `process.env.BUILD_LEADER_ID`
|
190 |
|
191 | Define 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 |
|
195 | Type: `Object`
|
196 |
|
197 | ###### pro
|
198 |
|
199 | Type: `Boolean`
|
200 | Default: `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 |
|
206 | Type: `String`
|
207 | Default: `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`.
|