Continuous Integration (CI)

[This is part 6/8 on GitLab Continuous Integration series]

Continuous Integration (CI) functionality in servers like GitLab (programmed in Ruby), GitHub (programmed in Ruby), Bitbucket (programed in Python), Jenkins (programmed in Java), Travis (programmed in Ruby) or CircleCi provide us with the capability automating our code building, testing and even deploying tasks.

In CI/CD Examples at GitLab you can find examples for several use cases that illustrate usage with Webdriver, NPM, PHP, Python, Ruby.

We have so far:

  1. Set up our GitLab Server that work as a Controller.
  2. Set up our local GitLab Container Registry. [Optional]
  3. Installed and registered a GitLab runner or Executor.

We can now set up a CI pipeline that contains our automation script.


I. Setup CI Pipelines

In our first example we will just ‘echo’ commands. You can identify in the script:

  • Four jobs. Jobs “define what to do”. build-job:, test-job1:, test-job2:, deploy-prod:. Job1 and Job2 will run in parallel.
  • Three stages. Stages “define when to run the jobs”. stage:build, stage:test, stage:deploy.

Notice that it uses two predefined variables that in this case the GitLab Server creates and sets on each run: $GITLAB_USER_LOGIN and $CI_COMMIT_BRANCH.

build-job:
  stage: build
  script:
    - echo "Hello, $GITLAB_USER_LOGIN!"

test-job1:
  stage: test
  script:
    - echo "This job tests something"

test-job2:
  stage: test
  script:
    - echo "This job tests something, but takes more time than test-job1."
    - echo "After the echo commands complete, it runs the sleep command for 20 seconds"
    - echo "which simulates a test that runs 20 seconds longer than test-job1"
    - sleep 20

deploy-prod:
  stage: deploy
  script:
    - echo "This job deploys something from the $CI_COMMIT_BRANCH branch."

II. Create the CI file

Now create the Pipeline/CI file .gitlab-ci.yml using the terminal or the Web GitLab Server:

II.1 Using the terminal

To add the file we can use a terminal or as shown in the next section, use the Web app.

  1. Open a terminal, create a directory for your code root directory
$ mkdir ~/Desarrollo/ci_cd/gitlabtest
$ cd ~/Desarrollo/ci_cd/gitlabtest
  1. Initialize the local repository and associate the code with a repository (gitlabtest) that will exist in the GitLab Server.
$ git init
$ git remote add origin http://devguy:Clave123@gitlab.example.com/devguy/gitlabtest.git

Note the use of the developer credentials in the URL

  1. Create a ‘hidden’ file .gitlab-ci.yml
$ vi .gitlab-ci.yml
  1. Add the previous code and save it.
  2. Add the CI file, Commit and Push the code there:
$ git add .gitlab-ci.yml
$ git commit -m "Adding CI pipeline"
$ git push origin master

A repository will be created in the GitLab Server and the pipeline will starts when the changes are committed.

The pipeline starts when the changes are committed.

II.2 Use the GitLab Server

  1. Access our GitLab Server in http://example.gitlab.com log in as devguy/Clave123
  2. Go to Project overview > Details.
  3. Above the file list, select the branch you want to commit to (Master) /project/ Click ‘+’, select New file
  4. Type as name: .gitlab-ci.yml
  5. Add the previous script.
  6. Click Commit changes.

The pipeline starts when the changes are committed.


III. Check our CI job

Get to your project page in the server to see the result. Access the project page in the server with user with a maintainer rol (devguy).

The project would be like. You can see the project name, CI Yaml file:

Go to CI/CD -> Pipelines.

You will see the result on ran pipelines.

And into the first job:

Notes:If the project’s visibility it’s private (default if the project is created from a terminal) Edit the project. Change the switch from private to public. Its’ easier to work with these first projects and tools if we avoid asking for authorization.
If there is a Pipeeline Job Error, first check permissions, add your account to the project even if you are the Administrator or it will give this error.
To validate your .gitlab-ci.yml file, use the CI Lint tool, which is available in every project.
You can also use CI/CD configuration visualization to view a graphical representation of your .gitlab-ci.yml file.

Reference

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s