Testing applications with GitLab
GitLab
In this tutorial, you will set up GitLab on a virtual machine, create a single project in the C++ programming language, configure a project test script, and test its execution.
To create and test a project in the GitLab environment:
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Required paid resources
The cost for maintaining a GitLab server includes:
- Fee for a disk and a continuously running VM (see Yandex Compute Cloud pricing).
- Fee for using a dynamic or static public IP address (see Yandex Virtual Private Cloud pricing).
Create a VM with GitLab
- On the folder page in the management console
, click Create resource and select Virtual machine instance. - In the Name field, enter
gitlab
for the VM name. - Select an availability zone for your VM.
- Under Boot disk image, go to the Marketplace tab and select a public GitLab image.
- Under Disks, select a 20 GB SSD.
- Under Computing resources:
-
Choose a VM platform.
-
Specify the required number of vCPUs and the amount of RAM.
For GitLab to run properly, specify the following configuration:
- Platform:
Intel Ice Lake
- Guaranteed vCPU performance:
100%
- vCPU:
4
- RAM:
8 GB
- Platform:
-
- Under Network settings:
- Select the Network and Subnet to connect your VM to. If the required network or subnet is not listed, create it.
- Under Public IP, keep Auto to assign your VM a random external IP address from the Yandex Cloud pool, or select a static address from the list if you reserved one in advance.
- Under Access, specify the information required to access the VM:
- In the Login field, enter your preferred login for the user to create on the VM.
- In the SSH key field, paste your public SSH key. You need to create a key pair for the SSH connection yourself. To learn how, see Connecting to a VM via SSH.
- Click Create VM.
- Wait about five minutes for the VM to be created and for all its services to start. After all of the services fully launch, GitLab will become available through its web interface in the browser.
Configure GitLab
-
On the Compute Cloud page, select the created
gitlab
VM and copy its public IP. -
Connect to the VM via SSH.
-
Get the GitLab administrator password using the following VM command:
sudo cat /etc/gitlab/initial_root_password
-
Copy the password (without spaces) from the
Password
row to the clipboard or a separate file. -
Open
http://<VM_public_IP_address>
in your browser. This will take you to the GitLab web interface. -
Log in using the administrator account:
- Username or email:
root
. - Password: Password you copied earlier.
If you are unable to log in, reset the administrator account password
. - Username or email:
-
Log in to the system again using the administrator account and the new password.
Set privacy settings
Disable sign-up for other users on the login page:
- Go to Admin area.
- In the left-hand panel, go to Settings and select General.
- Under Sign-up restrictions, click Expand.
- Disable Sign-up enabled.
- Click Save changes.
Now, only the administrator can register a new user from the Users tab in Overview.
Create a project
To create a project:
-
On the GitLab home page, select Create a project.
-
On the page that opens, specify:
- Project name:
My Project
- Project group and ID:
root
andmy-project
- Set a description and the scope of the project if required.
- Project name:
-
Click Create project.
-
Once you create the project, in the left-hand panel, go to Settings and select the CI/CD tab.
-
Under Auto DevOps, click Expand, disable Default to Auto DevOps pipeline, and click Save changes.
-
Add a project file.
-
In the left-hand panel, go to the GitLab project.
-
Click
in the repository navigation bar and select New file from the drop-down menu. -
Name the file as
test.cpp
. To this file, add the code of a program that checks the product of 2 × 2 and displaysHello World
if the result is 4:#include <iostream> #include <cassert> int main() { assert(2 * 2 == 4); std::cout << "Hello world!" << std::endl; return 0; }
-
Enter a commit name in the Commit message field.
-
Click Commit changes.
-
Set up and run testing for the project
A Runner is a program that tests and builds projects in the GitLab environment by following provided instructions.
Configure and register a Runner
-
Use SSH to connect to the VM and change to administrator mode in the console:
sudo -i
-
Download the Runner:
curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
-
Make the Runner executable:
chmod +x /usr/local/bin/gitlab-runner
-
Create a separate user to start the Runner:
useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
-
Install and start the Runner:
gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner gitlab-runner start
-
Register the Runner in GitLab:
-
Launch interactive registration with the
gitlab-runner register
command. -
Enter your GitLab server address. When you see the prompt:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com)
Enter
http://<public_IP_of_your_VM>
. -
Enter the registration token for the Runner. To retrieve it, go to the project page in GitLab, select Settings on the left-hand panel, and click the CI/CD tab. Then click Expand under Runners. Under Set up a specific Runner manually, copy the token from step 3 and enter it in the request response:
Please enter the gitlab-ci token for this runner <token>
-
When you see the prompt:
Please enter the gitlab-ci description for this runner
Enter a description for the Runner:
My runner
. -
Do not specify anything in the tag field, just press Enter. Otherwise, by default, the Runner will not run without specifying the appropriate tags for the project.
-
Specify the runtime environment. In our case, when prompted:
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
Enter:
shell
.
-
Runner installation and setup is complete. If everything is done correctly, the Runners activated for this project section with the registered Runner should appear on the page where you copied the registration token.
Create a test script
Create a test script to execute the Runner. The script is described in a special file named .gitlab-ci.yml
. It should be located in the project root directory. According to the script, the Runner will compile the project source file, convert it to an executable file, and then run it.
Since testing is performed on the VM operating system, you need to install the apps required for testing: git
to clone a project from the repository and g++
to compile the project.
To create a test script:
-
Connect to the VM via SSH and install the required apps:
sudo apt update sudo apt install -y git g++
-
Add a test script:
-
Open the GitLab web interface.
-
Open the GitLab project.
-
On the page that opens, click Set up CI/CD.
-
A page will open asking you to add a new file named
.gitlab-ci.yml
, in which you need to describe the script in YAML format. Add the script text:stages: - build - test - pack cache: paths: - hello build: stage: build script: g++ test.cpp -o hello test: stage: test script: ./hello pack: stage: pack script: gzip -c hello > hello.gz artifacts: paths: - hello.gz
The script indicates that the process is divided into three stages that are performed sequentially:
build
: At the first stage, the project is compiled and converted to an executable file namedhello
.test
: At the second stage, the executable file is run.pack
: At the third stage, an archive with the executable file is created, which you can download via the GitLab web interface after the script successfully finishes. Underartifacts
, there is a list of files available for download.
Under
cache
, specify which files and directories are to be transferred between stages. If you omit it, thehello
file will not be available at thetest
stage and an error will occur. -
Click Commit changes
-
After committing, the system will automatically start testing the last commit. To check its results, select Build on the left-hand panel in the GitLab project and then Pipelines from the drop-down menu. The line you will get should contain the first test with the passed
status. By clicking the cloud icon, you can download the build artifacts.
Create an error in the project
Now, make the project run with an error that the Runner should help you find during testing. To do this:
-
Go to the project repository and open the
test.cpp
file. -
Click Edit.
-
In the check, assert that the product of multiplying 2 x 2 should be 5. In this case, an error occurs when the program is run and it fails.
... assert(2 * 2 == 5); ...
-
Name the commit
Wrong assert in test.cpp
. -
Click Commit Changes.
Open Build → Pipelines. In the Stages column, you can see that, as a result of the test, the first stage, build
, completed successfully and the second stage, test
, ended with an error. The third stage, pack
, was skipped and the build artifacts were not generated.
If you click the failed
progress status and go to Failed Jobs, you can see the error text saying that assertion
failed:
How to delete the resources you created
To stop paying for your deployed server, delete the gitlab
VM you created.
If you reserved a static public IP address specifically for this VM:
- Select Virtual Private Cloud in your folder.
- Go to the IP addresses tab.
- Find the required address, click
, and select Delete.