You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: user/job-lifecycle.md
+79-78Lines changed: 79 additions & 78 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,54 +5,53 @@ redirect_from:
5
5
- /user/build-lifecycle/
6
6
---
7
7
8
-
Travis CI provides a default build environment and a default set of phases for each programming language. A virtual machine is created with the build environment for your job, your repository is cloned into it, optional addons are installed and then your build phases are run.
8
+
Travis CI provides a default build environment and a default set of phases for each programming language. It creates a virtual machine with the build environment for your job, clones your repository into it, installs optional add-ons, and runs your build phases as specified.
9
9
10
-
Keep reading to see how you can customize any phase in this process, via your `.travis.yml` and have a look at the [Travis CI Build Config Reference](https://config.travis-ci.com/).
10
+
Custom [build stages](user/build-stages) will probably be used later for more complex build definitions. Understanding how each job goes through the job lifecycle phases is important, as you may use these to customize your build definitions.
11
11
12
-
## The Build
12
+
In this section, learn the job lifecycle process and how to customize any phase of the build process.
13
13
14
-
The `.travis.yml` file describes the build process. A *build* in Travis CI is a sequence of [stages](/user/build-stages/). Each *stage* consists of jobs run in parallel.
14
+
> **Note:** Before continuing, ensure you understand our basic terminology. See our [core concepts documentation](/user/for-beginners/#build-jobs-stages-and-phases) and have a look at the [Travis CI Build Config Reference](https://config.travis-ci.com/) for more information.
15
15
16
-
## The Job Lifecycle
16
+
## Job Lifecycle Phases
17
17
18
-
Each *job* is a sequence of [phases](/user/for-beginners/#builds-jobs-stages-and-phases). The *main phases* are:
18
+
The ‘.travis.yml’ file describes the build process for Travis CI. A *build* in Travis CI consists of at least one unnamed stage or a sequence of [stages](/user/build-stages/). Each *stage* consists of one or more *jobs* running parallel inside the stage.
19
19
20
-
1.`install` - install any dependencies required
21
-
2.`script` - run the build script
20
+

22
21
23
-
Travis CI can run custom commands in the phases:
24
-
1.`before_install` - before the install phase
25
-
1.`before_script` - before the script phase
26
-
1.`after_script` - after the script phase.
27
-
1.`after_success` - when the build *succeeds* (e.g. building documentation), the result is in `TRAVIS_TEST_RESULT` environment variable
28
-
1.`after_failure` - when the build *fails* (e.g. uploading log files), the result is in `TRAVIS_TEST_RESULT` environment variable
22
+
Each *job* is a sequence of *phases*, which are sequential steps that form a job. The following table shows the main phases that users can add to jobs.
| Travis CI can run these optional commands in this phase:<br>- [`apt addons`](/user/installing-dependencies/#installing-packages-with-the-apt-addon)<br>- `before_cache` (if and only if caching is effective)<br>- [`cache components`](/user/caching/) | Travis CI can run the following custom commands in this main phase:<br>- `install`: installs any dependencies required.<br> - `before_install`: before the install phase.<br> - `before_script`: before the script phase.<br> - `after_script`: after the script phase.<br> - `after_success`: when the build *succeeds* (e.g., building documentation), the result is in the `TRAVIS_TEST_RESULT` environment variable.<br> - `after_failure`: when the build *fails* (e.g., uploading log files), the result is in the `TRAVIS_TEST_RESULT` environment variable. | Travis CI can run the following custom commands in this main phase:<br>- `script`: run the build script.<br> - `before_install`: before the install phase.<br> - `before_script`: before the script phase.<br> - `after_script`: after the script phase.<br> - `after_success`: when the build *succeeds* (e.g., building documentation), the result is in the `TRAVIS_TEST_RESULT` environment variable.<br> - `after_failure`: when the build *fails* (e.g., uploading log files), the result is in the `TRAVIS_TEST_RESULT` environment variable. | Travis CI can run custom commands in the phases:<br>- `deploy`: optional deployment phase.<br>- `before_deploy`: (if and only if deployment is active)<br>- `after_deploy`: (if and only if deployment is active) |
31
27
32
-
The complete sequence of phases of a job is the lifecycle. The steps are:
28
+
> **Note**: A *build* can be composed of many jobs.
1. OPTIONAL `before_cache` (if and only if caching is effective)
41
-
1.`after_success` or `after_failure`
42
-
1. OPTIONAL `before_deploy` (if and only if deployment is active)
43
-
1. OPTIONAL `deploy`
44
-
1. OPTIONAL `after_deploy` (if and only if deployment is active)
45
-
1.`after_script`
30
+
The following is an example of the complete sequence of phases of a job lifecycle.
46
31
47
-
> A *build* can be composed of many jobs.
32
+
```yaml
33
+
`apt addons:`//optional
34
+
`cache components:`//optional
35
+
`before_install:`
36
+
`install:`
37
+
`before_script:`
38
+
`script:`
39
+
`before_cache:`//optional (if and only if caching is effective)
40
+
`after_success`or `after_failure`
41
+
`before_deploy:`//optional (if and only if deployment is active)
42
+
`deploy:`//optional
43
+
`after_deploy:`//optional (if and only if deployment is active)
44
+
`after_script:`
45
+
```
46
+
{: data-file=".travis.yml"}
48
47
49
48
50
-
## Customizing the Installation Phase
49
+
## Customize the Installation Phase
51
50
52
-
The default dependency installation commands depend on the project language.
53
-
For instance, Java builds either use Maven or Gradle, depending on which build file is present in the repository. Ruby projects use Bundler when a Gemfile is present in the repository.
51
+
The default dependency installation commands depend on the chosen project language.
52
+
For instance, *Java* builds use Maven or Gradle, depending on which build file is in the repository. *Ruby* projects use Bundler when a Gemfile is in the repository.
54
53
55
-
You can specify your own script to install your project dependencies:
54
+
Install your project dependencies by specifying a script as follows:
> When using custom scripts, they should be executable (for example, using `chmod +x`) and contain a valid shebang line such as `/usr/bin/env sh`, `/usr/bin/env ruby`, or `/usr/bin/env python`.
63
62
64
-
You can also provide multiple steps, for instance to install both ruby and node dependencies:
63
+
You can also provide multiple steps, for instance, to install both ruby and node dependencies and update your file as shown below:
65
64
66
65
```yaml
67
66
install:
@@ -70,31 +69,32 @@ install:
70
69
```
71
70
{: data-file=".travis.yml"}
72
71
73
-
When one of the steps in the install fails, the build stops immediately and is marked as [errored](#breaking-the-build).
72
+
If a step fails during installation, the build stops immediately and is marked as [errored](#breaking-the-build).
73
+
74
74
75
75
You can also use `apt-get` or `snap` to [install dependencies](/user/installing-dependencies/)
76
76
77
-
### Skipping the Installation Phase
77
+
### Skip the Installation Phase
78
78
79
-
Skip the installation step entirely by adding the following to your `.travis.yml`:
79
+
Skip the installation step entirely by adding the following to your `.travis.yml` file:
80
80
81
81
```yaml
82
82
install: skip
83
83
```
84
84
{: data-file=".travis.yml"}
85
85
86
-
## Customizing the Build Phase
86
+
## Customize the Build Phase
87
87
88
-
The default build command depends on the project language. Ruby projects use `rake`, the common denominator for most Ruby projects.
88
+
The default build command depends on the project language. *Ruby* projects use `rake`, the common denominator for most *Ruby* projects.
89
89
90
-
You can overwrite the default build step in `.travis.yml`:
90
+
You can overwrite the default build step in `.travis.yml` as follows:
91
91
92
92
```yaml
93
93
script: bundle exec thor build
94
94
```
95
95
{: data-file=".travis.yml"}
96
96
97
-
You can specify multiple script commands as well:
97
+
Or, specify multiple script commands as shown below:
98
98
99
99
```yaml
100
100
script:
@@ -103,36 +103,35 @@ script:
103
103
```
104
104
{: data-file=".travis.yml"}
105
105
106
-
When one of the build commands returns a non-zero exit code, the Travis CI build runs the subsequent commands as well and accumulates the build result.
106
+
When one of the build commands returns a non-zero exit code, the Travis CI build runs the subsequent commands and accumulates the build result.
107
107
108
-
In the example above, if `bundle exec rake build` returns an exit code of 1, the following command `bundle exec rake builddoc` is still run, but the build will result in a failure.
108
+
In the example above, if `bundle exec rake build` returns an exit code of “1”. Thus, the following command, `bundle exec rake build doc,` still runs, but the build will fail.
109
109
110
110
If your first step is to run unit tests, followed by integration tests, you may still want to see if the integration tests succeed when the unit tests fail.
111
111
112
-
You can change this behavior by using a little bit of shell magic to run all commands subsequently but still have the build fail when the first command returns a non-zero exit code. Here's the snippet for your `.travis.yml`
112
+
Change this behavior by using shell magic to run all commands subsequently, but the build still fails when the first command returns a non-zero exit code. Here's the snippet for your `.travis.yml` file:
This example (note the `&&`) fails immediately when `bundle exec rake build` fails.
119
+
The example above fails immediately when `bundle exec rake build` fails. Note the `&&`
120
+
121
+
### Use the `$?` Command
120
122
121
-
### Note on $?
123
+
Each command in `script` is processed by a special bash function. This function manipulates the `$?` command to produce logs suitable for display. Consequently, you should not rely on the value of `$?` in the `script` section to alter the build behavior.
122
124
123
-
Each command in `script` is processed by a special bash function.
124
-
This function manipulates `$?` to produce logs suitable for display.
125
-
Consequently, you should not rely on the value of `$?` in `script` section to
126
-
alter the build behavior.
127
-
See [this GitHub issue](https://github.com/travis-ci/travis-ci/issues/3771)
125
+
See this [GitHub issue](https://github.com/travis-ci/travis-ci/issues/3771)
128
126
for a more technical discussion.
129
127
130
128
### Complex Build Commands
131
129
132
-
If you have a complex build environment that is hard to configure in the `.travis.yml`, consider moving the steps into a separate shell script.
133
-
The script can be a part of your repository and can easily be called from the `.travis.yml`.
130
+
If you have a complex build environment that is hard to configure in the `.travis.yml` file, consider moving the steps into a separate shell script.
134
131
135
-
Consider a scenario where you want to run more complex test scenarios, but only for builds that aren't coming from pull requests. A shell script might be:
132
+
The script can be a part of your repository and can easily be called from the `.travis.yml` file.
133
+
134
+
Consider a scenario where you want to run more complex test scenarios but only for builds that aren't coming from pull requests. In such case, a shell script might look like the following:
136
135
137
136
```bash
138
137
#!/bin/bash
@@ -142,63 +141,65 @@ if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then
142
141
bundle exec rake test:integration
143
142
fi
144
143
```
144
+
{: data-file=".bash"}
145
145
146
-
> Note the `set -ev` at the top. The `-e` flag causes the script to exit as soon as one command returns a non-zero exit code. This can be handy if you want whatever script you have to exit early. It also helps in complex installation scripts where one failed command wouldn't otherwise cause the installation to fail.
146
+
> Note the `set -ev` at the top. The `-e` flag causes the script to exit once one command returns a non-zero exit code. This is handy if you want a script to exit early. It also helps in complex installation scripts where one failed command wouldn't cause the installation to fail.
147
147
148
148
The `-v` flag makes the shell print all lines in the script before executing them, which helps identify which steps failed.
149
149
150
-
To run that script from your `.travis.yml`:
150
+
Follow these steps to run that script from your `.travis.yml` file:
151
151
152
152
1. Save it in your repository as `scripts/run-tests.sh`.
153
153
2. Make it executable by running `chmod ugo+x scripts/run-tests.sh`.
154
154
3. Commit it to your repository.
155
-
4. Add it to your `.travis.yml`:
155
+
4. Add it to your `.travis.yml` file:
156
156
```yaml
157
157
script: ./scripts/run-tests.sh
158
158
```
159
159
{: data-file=".travis.yml"}
160
160
161
-
#### How does this work? (Or, why you should not use `exit` in build steps)
161
+
#### Use the `exit` Command
162
162
163
-
The steps specified in the job lifecycle are compiled into a single bash script and executed on the worker.
163
+
After specifying the steps in the job lifecycle, these are compiled into a single bash script and executed on the worker.
164
164
165
-
When overriding these steps, do not use `exit` shell built-in command.
166
-
Doing so will run the risk of terminating the build process without giving Travis a chance to
167
-
perform subsequent tasks.
165
+
If you need to override the specified steps, do not use the `exit` shell built-in command. Doing so risks terminating the build process without allowing Travis to perform the subsequent tasks.
168
166
169
-
> Using `exit` inside a custom script is safe. If an error is indicated the task will be mark as failed.
167
+
In contrast, using the `exit` command inside a custom script is safe. If an error is indicated, the task will be marked as failed.
170
168
171
169
## Breaking the Build
172
170
173
-
If any of the commands in the first four phases of the job lifecycle return a non-zero exit code, the build is broken:
171
+
The build is considered broken when one or more of its jobs complete with a state that is not passed.
172
+
173
+
The build is broken if any of the commands in the first four phases of the job lifecycle return a non-zero exit code.
174
174
175
-
- If `before_install`, `install` or `before_script` returns a non-zero exit code,
176
-
the build is **errored** and stops immediately.
177
-
- If `script` returns a non-zero exit code, the build is **failed**, but continues to run before being marked as **failed**.
175
+
The following are common build problem scenarios:
176
+
- If `before_install`, `install`, or `before_script` returns a non-zero exit code,
177
+
Then the build is **errored** and stops immediately.
178
+
- If `script` returns a non-zero exit code, the build results in **failed**, but continues running before being marked as **failed**.
178
179
179
-
The exit code of `after_success`, `after_failure`, `after_script`, `after_deploy` and subsequent stages do not affect the build result.
180
-
However, if one of these stages times out, the build is marked as **failed**.
180
+
The exit code of `after_success`, `after_failure`, `after_script`, `after_deploy`, and subsequent stages do not affect the build result. However, if one of these stages times out, the build is marked as **failed**.
181
+
182
+
To troubleshoot more common issues, see our [Common Builds Problems](/user/common-build-problems/) documentation.
181
183
182
184
## Deploying your Code
183
185
184
-
An optional phase in the job lifecycleis deployment.
185
-
This phase is defined by using one of our continuous deployment providers to deploy code to Heroku, Amazon, or a different supported platform.
186
-
The deploy steps are skipped, if the build is broken.
186
+
Deployment is an optional phase in the job lifecycle. It is defined by using one of our continuous deployment providers to deploy code to Heroku, Amazon, or a different supported platform.
187
+
188
+
If the build is broken, the deploy steps are skipped.
187
189
188
190
When deploying files to a provider, prevent Travis CI from resetting your
189
-
working directory and deleting all changes made during the build ( `git stash
190
-
--all`) by adding `skip_cleanup` to your `.travis.yml`:
191
+
working directory and deleting all changes made during the build ( `git stash --all`) by adding a `skip_cleanup` command to your `.travis.yml` file:
191
192
192
193
```yaml
193
194
deploy:
194
195
skip_cleanup: true
195
196
```
196
197
{: data-file=".travis.yml"}
197
198
198
-
You can run commands before a deploy by using the `before_deploy` phase. A non-zero exit code in this phase will mark the build as **errored**.
199
+
The `before_deploy` phase allows you to run commands before deployment. A non-zero exit code in this phase will mark the build as **errored**.
199
200
200
-
If there are any steps you'd like to run after the deployment, you can use the `after_deploy` phase.
201
+
You can use the `after_deploy` phase to run any steps after the deployment.
201
202
202
-
> Note that `after_deploy` does not affect the status of the build.
203
+
> Note that `after_deploy` does not affect the build's status.
203
204
204
-
> Note that `before_deploy` and `after_deploy` are run before and after every deploy provider, so they will run multiple times, if there are multiple providers.
205
+
> **Note:** The `before_deploy` and `after_deploy` phases run before and after every deploy provider, so they run multiple times if numerous providers exist.
0 commit comments