environments.md 10.7 KB
Newer Older
1 2 3 4 5
# Introduction to environments and deployments

>**Note:**
Introduced in GitLab 8.9.

6
During the development of software, there can be many stages until it's ready
7 8 9 10 11
for public consumption. You sure want to first test your code and then deploy it
in a testing or staging environment before you release it to the public. That
way you can prevent bugs not only in your software, but in the deployment
process as well.

12 13 14 15
GitLab CI is capable of not only testing or building your projects, but also
deploying them in your infrastructure, with the added benefit of giving you a
way to track your deployments. In other words, you can always know what is
currently being deployed or has been deployed on your servers.
16 17 18

## Overview

19 20
With environments, you can control the Continuous Deployment of your software
all within GitLab. All you need to do is define them in your project's
A
Achilleas Pipinellis 已提交
21 22 23 24
[`.gitlab-ci.yml`][yaml] as we will explore below. GitLab provides a full
history of your deployments per every environment.

Environments are like tags for your CI jobs, describing where code gets deployed.
25 26 27 28 29
Deployments are created when [jobs] deploy versions of code to environments,
so every environment can have one or more deployments. GitLab keeps track of
your deployments, so you always know what is currently being deployed on your
servers.

30 31 32
To better understand how environments and deployments work, let's consider an
example. We assume that you have already created a project in GitLab and set up
a Runner. The example will cover the following:
A
Achilleas Pipinellis 已提交
33

34 35 36 37
- We are developing an application
- We want to run tests and build our app on all branches
- Our default branch is `master`
- We deploy the app only when a pipeline on `master` branch is run
A
Achilleas Pipinellis 已提交
38

39
Let's see how it all ties together.
A
Achilleas Pipinellis 已提交
40

41
## Defining environments
A
Achilleas Pipinellis 已提交
42

43
Let's consider the following `.gitlab-ci.yml` example:
A
Achilleas Pipinellis 已提交
44

45 46 47 48 49
```yaml
stages:
  - test
  - build
  - deploy
A
Achilleas Pipinellis 已提交
50

51 52 53
test:
  stage: test
  script: echo "Running tests"
A
Achilleas Pipinellis 已提交
54

55 56 57
build:
  stage: build
  script: echo "Building the app"
A
Achilleas Pipinellis 已提交
58

59 60 61
deploy_staging:
  stage: deploy
  script:
A
Achilleas Pipinellis 已提交
62
    - echo "Deploy to staging server"
63
  environment:
A
Achilleas Pipinellis 已提交
64 65
    name: staging
    url: https://staging.example.com
66 67 68
  only:
  - master
```
69

70
We have defined 3 [stages](yaml/README.md#stages):
71

72 73 74
- test
- build
- deploy
75

76 77 78 79 80 81
The jobs assigned to these stages will run in this order. If a job fails, then
the builds that are assigned to the next stage won't run, rendering the pipeline
as failed. In our case, the `test` job will run first, then the `build` and
lastly the `deploy_staging`. With this, we ensure that first the tests pass,
then our app is able to be built successfully, and lastly we deploy to the
staging server.
82

A
Achilleas Pipinellis 已提交
83 84 85 86 87 88 89
The `environment` keyword is just a hint for GitLab that this job actually
deploys to this environment's `name`. It can also have a `url` which, as we
will later see, is exposed in various places within GitLab. Each time a job that
has an environment specified and succeeds, a deployment is recorded, remembering
the Git SHA and environment name.

To sum up, with the above `.gitlab-ci.yml` we have achieved that:
90

91
- All branches will run the `test` and `build` jobs.
A
Achilleas Pipinellis 已提交
92 93 94
- The `deploy_staging` job will run [only](yaml/README.md#only) on the `master`
  branch which means all merge requests that are created from branches don't
  get to deploy to the staging server
95 96 97
- When a merge request is merged, all jobs will run and the `deploy_staging`
  in particular will deploy our code to a staging server while the deployment
  will be recorded in an environment named `staging`.
98

A
Achilleas Pipinellis 已提交
99 100 101 102 103 104 105 106
Let's now see how that information is exposed within GitLab.

## Viewing the current status of an environment

The environment list under your project's **Pipelines ➔ Environments**, is
where you can find information of the last deployment status of an environment.

Here's how the Environments page looks so far.
107 108 109

![Staging environment view](img/environments_available_staging.png)

A
Achilleas Pipinellis 已提交
110 111 112 113 114 115 116 117 118 119 120 121
There's a bunch of information there, specifically you can see:

- The environment's name with a link to its deployments
- The last deployment ID number and who performed it
- The build ID of the last deployment with its respective job name
- The commit information of the last deployment such as who committed, to what
  branch and the Git SHA of the commit
- The exact time the last deployment was performed
- A button that takes you to the URL that you have defined under the
  `environment` keyword in `.gitlab-ci.yml`
- A button that re-deploys the latest deployment, meaning it runs the job
  defined by the environment name for that specific commit
122 123 124 125 126 127 128

>**Notes:**
- While you can create environments manually in the web interface, we recommend
  that you define your environments in `.gitlab-ci.yml` first. They will
  be automatically created for you after the first deploy.
- The environments page can only be viewed by Reporters and above. For more
  information on the permissions, see the [permissions documentation][permissions].
A
Achilleas Pipinellis 已提交
129 130
- Only deploys that happen after your `.gitlab-ci.yml` is properly configured
  will show up in the "Environment" and "Last deployment" lists.
131

A
Achilleas Pipinellis 已提交
132 133 134
The information shown in the Environments page is limited to the latest
deployments, but as you may have guessed an environment can have multiple
deployments.
135

A
Achilleas Pipinellis 已提交
136
## Viewing the deployment history of an environment
137

A
Achilleas Pipinellis 已提交
138
GitLab keeps track of your deployments, so you always know what is currently
A
Achilleas Pipinellis 已提交
139 140 141 142 143 144
being deployed on your servers. That way you can have the full history of your
deployments per every environment right in your browser. Clicking on an
environment will show the history of its deployments. Assuming you have deployed
multiple times already, here's how a specific environment's page looks like.

![Deployments](img/deployments_view.png)
A
Achilleas Pipinellis 已提交
145

A
Achilleas Pipinellis 已提交
146 147 148 149 150 151 152 153 154 155 156 157
We can see the same information as when in the Environments page, but this time
all deployments are shown. As you may have noticed, apart from the **Re-deploy**
button there are now **Rollback** buttons for each deployment. Let's see how
that works.

## Rolling back changes

You can't control everything, so sometimes things go wrong. When that unfortunate
time comes GitLab has you covered. Simply by clicking the **Rollback** button
that can be found in the deployments page
(**Pipelines ➔ Environments ➔ `environment name`**) you can relaunch the
job with the commit associated with it.
A
Achilleas Pipinellis 已提交
158 159

>**Note:**
A
Achilleas Pipinellis 已提交
160 161 162 163 164 165 166
Bare in mind that your mileage will vary and it's entirely up to how you define
the deployment process in the job's `script` whether the rollback succeeds or not.
GitLab CI is just following orders.

Thankfully that was the staging server that we had to rollback, and since we
learn from our mistakes, we decided to not make the same again when we deploy
to the production server. Enter manual actions for deployments.
A
Achilleas Pipinellis 已提交
167 168 169

## Manually deploying to environments

A
Achilleas Pipinellis 已提交
170 171 172 173 174
Turning a job from running automatically to a manual action is as simple as
adding `when: manual` to it. To expand on our previous example, let's add
another job that this time deploys our app to a production server and is
tracked by a `production` environment. The `.gitlab-ci.yml` looks like this
so far:
175

A
Achilleas Pipinellis 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
```yaml
stages:
  - test
  - build
  - deploy

test:
  stage: test
  script: echo "Running tests"

build:
  stage: build
  script: echo "Building the app"

deploy_staging:
  stage: deploy
  script:
    - echo "Deploy to staging server"
  environment:
    name: staging
    url: https://staging.example.com
  only:
  - master

deploy_prod:
  stage: deploy
  script:
    - echo "Deploy to production server"
  environment:
    name: production
    url: https://example.com
  when: manual
  only:
  - master
```

The `when: manual` action exposes a play button in GitLab's UI and the
`deploy_prod` job will only be triggered if and when we click that play button.
You can find it in the pipeline, build, environment, and deployment views.

| Pipelines | Single pipeline | Environments | Deployments | Builds |
| --------- | ----------------| ------------ | ----------- | -------|
| ![Pipelines manual action](img/environments_manual_action_pipelines.png) | ![Pipelines manual action](img/environments_manual_action_single_pipeline.png) | ![Environments manual action](img/environments_manual_action_environments.png) | ![Deployments manual action](img/environments_manual_action_deployments.png) | ![Builds manual action](img/environments_manual_action_builds.png) |

Clicking on the play button in either of these places will trigger the
`deploy_prod` job, and the deployment will be recorded under a new
environment named `production`.

While this is fine for deploying to some stable environments like staging or
production, what happens for branches? So far we haven't defined anything
regarding deployments for branches other than `master`. Dynamic environments
will help us achieve that.
A
Achilleas Pipinellis 已提交
228 229 230 231 232 233 234 235

## Dynamic environments

As the name suggests, it is possible to create environments on the fly by just
declaring their names dynamically in `.gitlab-ci.yml`.

GitLab Runner exposes various [environment variables][variables] when a job runs,
and as such you can use them
236 237

```
A
Achilleas Pipinellis 已提交
238
review:
239
  stage: deploy
A
Achilleas Pipinellis 已提交
240 241 242 243 244
  script:
    - rsync -av --delete public /srv/nginx/pages/$CI_BUILD_REF_NAME
  environment:
    name: review/$CI_BUILD_REF_NAME
    url: https://$CI_BUILD_REF_NAME.example.com
245 246
```

247
## Closing an environment
248

A
Achilleas Pipinellis 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
```
review:
  stage: deploy
  script:
    - rsync -av --delete public /srv/nginx/pages/$CI_BUILD_REF_NAME
  environment:
    name: review/$CI_BUILD_REF_NAME
    url: http://$CI_BUILD_REF_NAME.$APPS_DOMAIN
    on_stop: stop_review

stop_review:
  script: rm -rf /srv/nginx/pages/$CI_BUILD_REF_NAME
  when: manual
  environment:
    name: review/$CI_BUILD_REF_NAME
    action: stop
```
266

267
## Checkout deployments locally
A
Achilleas Pipinellis 已提交
268 269 270 271 272 273 274 275 276 277 278 279

Since 8.13, a reference in the git repository is saved for each deployment. So
knowing what the state is of your current environments is only a `git fetch`
away.

In your git config, append the `[remote "<your-remote>"]` block with an extra
fetch line:

```
fetch = +refs/environments/*:refs/remotes/origin/environments/*
```

280 281 282 283 284 285 286 287 288
## Further reading

Below are some links you may find interesting:

- [The `.gitlab-ci.yml` definition of environments](yaml/README.md#environment)
- [A blog post on Deployments & Environments](https://about.gitlab.com/2016/08/26/ci-deployment-and-environments/)
- [Review Apps](review_apps.md) Expand dynamic environments to deploy your code for every branch


A
Achilleas Pipinellis 已提交
289
## TODO
290 291 292

Actions

A
Achilleas Pipinellis 已提交
293 294 295 296 297 298 299 300 301 302
- View environments +
- View deployments +
   - Rollback deployments +
   - Run deployments +
- View link to environment URL
- View last commit message of deployment +
- View person who performed the deployment +
- View commit SHA that triggered the deployment +
- View branch the deployment was based on +
- View time ago the deployment was performed +
303

304
[Pipelines]: pipelines.md
305
[jobs]: yaml/README.md#jobs
A
Achilleas Pipinellis 已提交
306
[yaml]: yaml/README.md
307 308
[environments]: #environments
[deployments]: #deployments
A
Achilleas Pipinellis 已提交
309 310
[permissions]: ../user/permissions.md
[variables]: variables/README.md