Debian Codegraph @

docs/tutorials/gitlab_ci.md

raw
1 ## Create a new release using GitLab CI
2
3 For this example, we have a `python/django` application and `Docker` as a containerization tool.
4
5 _Goal_: Bump a new version every time that a change occurs on the `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](../commands/bump.md) as well.
14
15 ### GitLab Configuration
16
17 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 ```bash
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 `[email protected]` 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. This means changing the file/s that reflects the version, creating a new commit and git tag.
50
51 ### Stages and Jobs
52
53 ```yaml
54 image: docker:latest
55
56 services:
57 - docker:dind
58
59 variables:
60 API_IMAGE_NAME: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
61
62 before_script:
63 - apk add --no-cache py-pip
64 - pip install docker-compose
65
66 stages:
67 - test
68 - auto-bump
69
70 test:
71 stage: test
72 script:
73 - docker-compose run -e DJANGO_ENVIRONMENT=dev your_project python manage.py test # run tests
74
75 auto-bump:
76 stage: auto-bump
77 image: python:3.10
78 before_script:
79 - "which ssh-agent || ( apt-get update -qy && apt-get install openssh-client -qqy )"
80 - eval `ssh-agent -s`
81 - echo "${SSH_PRIVATE_KEY}" | tr -d '\r' | ssh-add - > /dev/null # add ssh key
82 - pip3 install -U commitizen # install commitizen
83 - mkdir -p ~/.ssh
84 - chmod 700 ~/.ssh
85 - echo "$SSH_PUBLIC_KEY" >> ~/.ssh/id_rsa.pub
86 - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
87 dependencies:
88 - test
89 script:
90 - git remote set-url origin [email protected]:discover/rentee-core.git # git configuration
91 - git config --global user.email "${CI_EMAIL}" && git config --global user.name "${CI_USERNAME}"
92 - 'exists=`git show-ref refs/heads/master` && if [ -n "$exists" ]; then git branch -D master; fi'
93 - git checkout -b master
94 - cz bump --yes # execute auto bump and push to master
95 - git push origin master:$CI_COMMIT_REF_NAME
96 - TAG=$(head -n 1 VERSION) # get the new software version and save into artifacts
97 - echo "#!/bin/sh" >> variables
98 - echo "export TAG='$TAG'" >> variables
99 - git push origin $TAG
100 only:
101 refs:
102 - master
103 artifacts:
104 paths:
105 - variables
106 ```
107
108 So, every time that a developer pushes to any branch, the `test` job is executed. If the branch is `master` and the test jobs succeed, the `auto-bump` takes place.
109 To be able to push using the GitLab runner, we have to set the SSH key, configure git, and finally execute the auto bump.
110
111 After merging the new changes into master, we have the final result:
112
113 ![gitlab final ci result](../images/gitlab_ci/gitlab_final_ci_result.png)