Skip to content

Commit d69b138

Browse files
authored
Merge pull request #85 from marcosschroh/docs/gitlab-ci-tutorial
docs(Tutorial): Tutorial section added. GitLab tutorial added
2 parents dd8b974 + 82ae6fd commit d69b138

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed
63.6 KB
Loading
32.1 KB
Loading
85.8 KB
Loading

docs/tutorials/gitlab_ci.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
### Create a new release usign GitLab CI
2+
3+
For this example, we have a `python/django` application and `Docker` as containerization tool.
4+
5+
*Goal*: Bump a new version every time that a change occurs on `master` branch. The bump should be executed automatically by the `CI` process.
6+
7+
#### Development Workflow:
8+
9+
1. A developer creates a new commit on any branch (except `master`)
10+
2. A developer creates a merge request (MR) against `master` branch
11+
3. When the `MR` is merged into master, the 2 stages of the CI are executed
12+
4. For simplification, we store the software version in a file called `VERSION`. You can use any file that you want as `commitizen` supports it.
13+
5. The commit message executed automatically by the `CI` must include `[skip-ci]` in the message, otherwise the process will generate a loop. You can define the message structure in [commitizen](https://woile.github.io/commitizen/bump/) as well.
14+
15+
#### Gitlab Configuration:
16+
17+
In order to be able to change files and push new changes with `Gitlab CI` runners, we need to have a `ssh` key and configure a git user.
18+
19+
First, let's create a `ssh key`. The only requirement is to create it without a passphrase:
20+
21+
```
22+
ssh-keygen -f deploy_key -N ""
23+
```
24+
25+
The previous command will create a private and public key under the files `deploy_key` and `deploy_key.pub`. We will use them later.
26+
27+
For the git user, we need an email and username. You can choose whatever you want, in this example, we choose `ci-runner@myproject.com` and `admin` respectively.
28+
29+
Now, we need to create three environment variables that will be visible for the runners. They should be created in the `variables` section under `settings/ci_cd`:
30+
31+
![gitlab variables](../images/gitlab_ci/gitlab_variables.png)
32+
33+
Create `SSH_PRIVATE_KEY`, `CI_EMAIL`, `CI_USERNAME` variables and fill them with the `private_key`, `email` and `username` that we have created previously.
34+
35+
The latest step is to create a `deploy key.` To do this, we should create it under the section `settings/repository` and fill it with the `public key` generated before. Check `Write access allowed`, otherwise, the runner won't be able to write the changes to the repository.
36+
37+
![gitlab deploy key](../images/gitlab_ci/gitlab_deploy_key.png)
38+
39+
If you have more projects under the same organization, you can reuse the deploy key created before, but you will have to repeat the step where we have created the environment variables (ssh key, email, and username).
40+
41+
tip: If the CI raise some errors, try to unprotect the private key.
42+
43+
#### Defining GitLab CI Pipeline
44+
45+
1. Create a `.gitlab-ci.yaml` file that contains `stages` and `jobs` configurations. You can find more info [here](https://docs.gitlab.com/ee/ci/quick_start/).
46+
47+
2. Define `stages` and `jobs`. For this example, we define two `stages` with one `job` each one.
48+
* Test the application.
49+
* Auto bump the version. Means changing the file/s that reflects the version, creating a new commit and git tag.
50+
51+
52+
#### Stages and Jobs
53+
54+
```yaml
55+
image: docker:latest
56+
57+
services:
58+
- docker:dind
59+
60+
variables:
61+
API_IMAGE_NAME: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
62+
63+
before_script:
64+
- apk add --no-cache py-pip
65+
- pip install docker-compose
66+
67+
stages:
68+
- test
69+
- auto-bump
70+
71+
test:
72+
stage: test
73+
script:
74+
- docker-compose run -e DJANGO_ENVIRONMENT=dev your_project python manage.py test # run tests
75+
76+
auto-bump:
77+
stage: auto-bump
78+
image: python:3.6
79+
before_script:
80+
- 'which ssh-agent || ( apt-get update -qy && apt-get install openssh-client -qqy )'
81+
- eval `ssh-agent -s`
82+
- echo "${SSH_PRIVATE_KEY}" | tr -d '\r' | ssh-add - > /dev/null # add ssh key
83+
- pip3 install -U Commitizen # install commitizen
84+
- mkdir -p ~/.ssh
85+
- chmod 700 ~/.ssh
86+
- echo "$SSH_PUBLIC_KEY" >> ~/.ssh/id_rsa.pub
87+
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
88+
dependencies:
89+
- test
90+
script:
91+
- git remote set-url origin git@gitlab.com:discover/rentee-core.git # git configuration
92+
- git config --global user.email "${CI_EMAIL}" && git config --global user.name "${CI_USERNAME}"
93+
- 'exists=`git show-ref refs/heads/master` && if [ -n "$exists" ]; then git branch -D master; fi'
94+
- git checkout -b master
95+
- cz bump # execute auto bump and push to master
96+
- git push origin master:$CI_COMMIT_REF_NAME
97+
- TAG=$(head -n 1 VERSION) # get the new software version and save into artifacts
98+
- echo "#!/bin/sh" >> variables
99+
- echo "export TAG='$TAG'" >> variables
100+
- git push origin $TAG
101+
only:
102+
refs:
103+
- master
104+
artifacts:
105+
paths:
106+
- variables
107+
```
108+
109+
So, every time that a developer push to any branch the `test` job is executed. If the branch is `master` and the test jobs success, the `auto-bump` takes place.
110+
To be able to push using the Gitlab runner we have to set the ssh key, configure git and finally execute the auto bump.
111+
112+
After merging the new changed into master we have the final result:
113+
114+
![gitlab final ci result](../images/gitlab_ci/gitlab_final_ci_result.png)

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ nav:
1313
- Configuration: 'config.md'
1414
- Bump: 'bump.md'
1515
- Customization: 'customization.md'
16+
- Tutorials:
17+
- GitLab CI: 'tutorials/gitlab_ci.md'
1618

1719
markdown_extensions:
1820
- markdown.extensions.codehilite:

0 commit comments

Comments
 (0)