README.md 7.2 KB
Newer Older
D
Douwe Maan 已提交
1 2
# Quick Start

3 4
Starting from version 8.0, GitLab Continuous Integration (CI) is fully
integrated into GitLab itself and is enabled by default on all projects.
D
Douwe Maan 已提交
5

6
This guide assumes that you:
D
Douwe Maan 已提交
7

8 9 10
- have a working GitLab instance of version 8.0 or higher or are using
  [GitLab.com](https://gitlab.com/users/sign_in)
- have a project in GitLab that you would like to use CI for
D
Douwe Maan 已提交
11

12
In brief, the steps needed to have a working CI can be summed up to:
D
Douwe Maan 已提交
13

14 15 16
1. Create a new project
1. Add `.gitlab-ci.yml` to the git repository and push to GitLab
1. Configure a Runner
D
Douwe Maan 已提交
17

18
From there on, on every push to your git repository the build will be
19
automagically started by the Runner and will appear under the project's
20
`/builds` page.
D
Douwe Maan 已提交
21

22
Now, let's break it down to pieces and work on solving the GitLab CI puzzle.
D
Douwe Maan 已提交
23

24
## Creating a `.gitlab-ci.yml` file
25

26 27
Before you create `.gitlab-ci.yml` let's first explain in brief what this is
all about.
D
Douwe Maan 已提交
28

29
### What is `.gitlab-ci.yml`
D
Douwe Maan 已提交
30

31 32
The `.gitlab-ci.yml` file is where you configure what CI does with your project.
It lives in the root of your repository.
D
Douwe Maan 已提交
33

34 35 36
On any push to your repository, GitLab will look for the `.gitlab-ci.yml`
file and start builds on _Runners_ according to the contents of the file,
for that commit.
D
Douwe Maan 已提交
37

38 39 40 41 42 43
Because `.gitlab-ci.yml` is in the repository, it is version controlled,
old versions still build succesfully, forks can easily make use of CI,
branches can have separate builds and you have a single source of truth for CI.
You can read more about the reasons why we are using `.gitlab-ci.yml`
[in our blog about it][blog-ci].

44 45 46
**Note:** `.gitlab-ci.yml` is a [YAML](https://en.wikipedia.org/wiki/YAML) file
so you have to pay extra attention to the identation. Always use spaces, not
tabs.
47 48 49 50 51

### Creating a simple `.gitlab-ci.yml` file

You need to create a file named `.gitlab-ci.yml` in the root directory of your
repository. Below is an example for a Ruby on Rails project.
D
Douwe Maan 已提交
52 53 54

```yaml
before_script:
55 56 57 58 59
  - apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs
  - ruby -v
  - which ruby
  - gem install bundler --no-ri --no-rdoc
  - bundle install --jobs $(nproc)  "${FLAGS[@]}"
D
Douwe Maan 已提交
60 61 62 63 64 65 66 67 68 69

rspec:
  script:
    - bundle exec rspec

rubocop:
  script:
    - bundle exec rubocop
```

70 71 72
This is the simplest possible build configuration that will work for most Ruby
applications:

73 74
1. Define two jobs `rspec` and `rubocop` (the names are arbitrary) with
   different commands to be executed.
75 76 77
1. Before every job, the commands defined by `before_script` are executed.

The `.gitlab-ci.yml` file defines sets of jobs with constraints of how and when
78 79 80 81
they should be run. The jobs are defined as top-level elements with a name (in
our case `rspec` and `rubocop`) and always have to contain the `script` keyword.
Jobs are used to create builds, which are then picked by
[Runners](../runners/README.md) and executed within the environment of the Runner.
82 83

What is important is that each job is run independently from each other.
D
Douwe Maan 已提交
84

85 86
If you want to check whether your `.gitlab-ci.yml` file is valid, there is a
Lint tool under the page `/ci/lint` of your GitLab instance. You can also find
87
the link under **Settings > CI settings** in your project.
D
Douwe Maan 已提交
88

89 90
For more information and a complete `.gitlab-ci.yml` syntax, please check
[the documentation on .gitlab-ci.yml](../yaml/README.md).
D
Douwe Maan 已提交
91

92
### Push `.gitlab-ci.yml` to GitLab
D
Douwe Maan 已提交
93

94 95
Once you've created `.gitlab-ci.yml`, you should add it to your git repository
and push it to GitLab.
D
Douwe Maan 已提交
96 97 98

```bash
git add .gitlab-ci.yml
99
git commit -m "Add .gitlab-ci.yml"
D
Douwe Maan 已提交
100 101 102
git push origin master
```

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
Now if you go to the **Builds** page you will see that the builds are pending.

You can also go to the **Commits** page and notice the little clock icon next
to the commit SHA.

![New commit pending](img/new_commit.png)

Clicking on the clock icon you will be directed to the builds page for that
specific commit.

![Single commit builds page](img/single_commit_status_pending.png)

Notice that there are two jobs pending which are named after what we wrote in
`.gitlab-ci.yml`. The red triangle indicates that there is no Runner configured
yet for these builds.

119 120
The next step is to configure a Runner so that it picks the pending jobs.

121
## Configuring a Runner
122 123 124 125 126

In GitLab, Runners run the builds that you define in `.gitlab-ci.yml`.
A Runner can be a virtual machine, a VPS, a bare-metal machine, a docker
container or even a cluster of containers. GitLab and the Runners communicate
through an API, so the only needed requirement is that the machine on which the
127
Runner is configured to has Internet access.
128 129 130 131 132 133 134 135

A Runner can be specific to a certain project or serve multiple projects in
GitLab. If it serves all projects it's called a _Shared Runner_.

Find more information about different Runners in the
[Runners](../runners/README.md) documentation.

You can find whether any Runners are assigned to your project by going to
136 137
**Settings > Runners**. Setting up a Runner is easy and straightforward. The
official Runner supported by GitLab is written in Go and can be found at
138 139
<https://gitlab.com/gitlab-org/gitlab-ci-multi-runner>.

140
In order to have a functional Runner you need to follow two steps:
141 142 143 144

1. [Install it][runner-install]
2. [Configure it](../runners/README.md#registering-a-specific-runner)

145 146 147
Follow the links above to set up your own Runner or use a Shared Runner as
described in the next section.

148 149 150 151
For other types of unofficial Runners written in other languages, see the
[instructions for the various GitLab Runners](https://about.gitlab.com/gitlab-ci/#gitlab-runner).

Once the Runner has been set up, you should see it on the Runners page of your
152
project, following **Settings > Runners**.
D
Douwe Maan 已提交
153

154
![Activated runners](img/runners_activated.png)
D
Douwe Maan 已提交
155

156
### Shared Runners
D
Douwe Maan 已提交
157

158 159
If you use [GitLab.com](https://gitlab.com/) you can use **Shared Runners**
provided by GitLab Inc.
D
Douwe Maan 已提交
160

161 162
These are special virtual machines that run on GitLab's infrastructure and can
build any project.
D
Douwe Maan 已提交
163

164
To enable **Shared Runners** you have to go to your project's
165
**Settings > Runners** and click **Enable shared runners**.
D
Douwe Maan 已提交
166

167
[Read more on Shared Runners](../runners/README.md).
D
Douwe Maan 已提交
168

169
## Seeing the status of your build
D
Douwe Maan 已提交
170

171 172
After configuring the Runner succesfully, you should see the status of your
last commit change from _pending_ to either _running_, _success_ or _failed_.
D
Douwe Maan 已提交
173

174
You can view all builds, by going to the **Builds** page in your project.
D
Douwe Maan 已提交
175

176
![Commit status](img/builds_status.png)
D
Douwe Maan 已提交
177

178 179 180
By clicking on a Build ID, you will be able to see the log of that build.
This is important to diagnose why a build failed or acted differently than
you expected.
D
Douwe Maan 已提交
181

182
![Build log](img/build_log.png)
D
Douwe Maan 已提交
183

184 185
You are also able to view the status of any commit in the various pages in
GitLab, such as **Commits** and **Merge Requests**.
D
Douwe Maan 已提交
186

187 188 189 190 191 192 193 194
## Builds badge

You can access a builds badge image using following link:

```
http://example.gitlab.com/namespace/project/builds/status/branch/badge.svg
```

195
## Next steps
D
Douwe Maan 已提交
196

197
Awesome! You started using CI in GitLab!
D
Douwe Maan 已提交
198

199 200
Next you can look into doing more with the CI. Many people are using GitLab
to package, containerize, test and deploy software.
D
Douwe Maan 已提交
201

202
Visit our various languages examples at <https://gitlab.com/groups/gitlab-examples>.
D
Douwe Maan 已提交
203

204 205
[runner-install]: https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/tree/master#installation
[blog-ci]: https://about.gitlab.com/2015/05/06/why-were-replacing-gitlab-ci-jobs-with-gitlab-ci-dot-yml/