Skip to content

Commit db50a35

Browse files
authored
Merge branch 'master' into nrios14-patch-4
2 parents 55a7e90 + 22d4ec3 commit db50a35

File tree

2 files changed

+79
-78
lines changed

2 files changed

+79
-78
lines changed

user/images/Build-stages-jobs.png

58.4 KB
Loading

user/job-lifecycle.md

Lines changed: 79 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,53 @@ redirect_from:
55
- /user/build-lifecycle/
66
---
77

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.
99

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.
1111

12-
## The Build
12+
In this section, learn the job lifecycle process and how to customize any phase of the build process.
1313

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.
1515
16-
## The Job Lifecycle
16+
## Job Lifecycle Phases
1717

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.
1919

20-
1. `install` - install any dependencies required
21-
2. `script` - run the build script
20+
![Build, Stages, and Jobs Diagram](/user/images/Build-stages-jobs.png)
2221

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.
2923

30-
There are three optional *deployment phases*.
24+
| **Pre-Install Phase (Optional)** | **Install Phase** | **Script Phase** | **Deploy Phase (Optional)** |
25+
|---|---|---|---|
26+
| 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) |
3127

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.
3329
34-
1. OPTIONAL Install [`apt addons`](/user/installing-dependencies/#installing-packages-with-the-apt-addon)
35-
1. OPTIONAL Install [`cache components`](/user/caching/)
36-
1. `before_install`
37-
1. `install`
38-
1. `before_script`
39-
1. `script`
40-
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.
4631

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"}
4847

4948

50-
## Customizing the Installation Phase
49+
## Customize the Installation Phase
5150

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.
5453

55-
You can specify your own script to install your project dependencies:
54+
Install your project dependencies by specifying a script as follows:
5655

5756
```yaml
5857
install: ./install-dependencies.sh
@@ -61,7 +60,7 @@ install: ./install-dependencies.sh
6160
6261
> 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`.
6362

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:
6564

6665
```yaml
6766
install:
@@ -70,31 +69,32 @@ install:
7069
```
7170
{: data-file=".travis.yml"}
7271

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+
7474

7575
You can also use `apt-get` or `snap` to [install dependencies](/user/installing-dependencies/)
7676

77-
### Skipping the Installation Phase
77+
### Skip the Installation Phase
7878

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:
8080

8181
```yaml
8282
install: skip
8383
```
8484
{: data-file=".travis.yml"}
8585

86-
## Customizing the Build Phase
86+
## Customize the Build Phase
8787

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.
8989

90-
You can overwrite the default build step in `.travis.yml`:
90+
You can overwrite the default build step in `.travis.yml` as follows:
9191

9292
```yaml
9393
script: bundle exec thor build
9494
```
9595
{: data-file=".travis.yml"}
9696

97-
You can specify multiple script commands as well:
97+
Or, specify multiple script commands as shown below:
9898

9999
```yaml
100100
script:
@@ -103,36 +103,35 @@ script:
103103
```
104104
{: data-file=".travis.yml"}
105105

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.
107107

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.
109109

110110
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.
111111

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:
113113

114114
```yaml
115115
script: bundle exec rake build && bundle exec rake builddoc
116116
```
117117
{: data-file=".travis.yml"}
118118

119-
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
120122

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.
122124

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)
128126
for a more technical discussion.
129127

130128
### Complex Build Commands
131129

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.
134131

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:
136135

137136
```bash
138137
#!/bin/bash
@@ -142,63 +141,65 @@ if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then
142141
bundle exec rake test:integration
143142
fi
144143
```
144+
{: data-file=".bash"}
145145

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.
147147

148148
The `-v` flag makes the shell print all lines in the script before executing them, which helps identify which steps failed.
149149

150-
To run that script from your `.travis.yml`:
150+
Follow these steps to run that script from your `.travis.yml` file:
151151

152152
1. Save it in your repository as `scripts/run-tests.sh`.
153153
2. Make it executable by running `chmod ugo+x scripts/run-tests.sh`.
154154
3. Commit it to your repository.
155-
4. Add it to your `.travis.yml`:
155+
4. Add it to your `.travis.yml` file:
156156
```yaml
157157
script: ./scripts/run-tests.sh
158158
```
159159
{: data-file=".travis.yml"}
160160

161-
#### How does this work? (Or, why you should not use `exit` in build steps)
161+
#### Use the `exit` Command
162162

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.
164164

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.
168166

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.
170168

171169
## Breaking the Build
172170

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.
174174

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**.
178179

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.
181183

182184
## Deploying your Code
183185

184-
An optional phase in the job lifecycle is 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.
187189

188190
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:
191192

192193
```yaml
193194
deploy:
194195
skip_cleanup: true
195196
```
196197
{: data-file=".travis.yml"}
197198

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**.
199200

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.
201202

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.
203204

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

Comments
 (0)