Testing applications with GitLab
GitLab
In this scenario, 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 the VM name:
gitlab
. - 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 VM named
gitlab
and copy its public IP address. -
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 project ID:
root
andmy-project
, respectively. - 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 your file
test.cpp
. Add into it the code of the 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 --location --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 scenario
Create a test scenario to execute the Runner. The scenario is described in a special file named .gitlab-ci.yml
, which should be stored in the project's root directory. According to the scenario, the Runner will compile the project source file, convert it to an executable file, and then run it.
As testing will take place on the VM operating system, you need to install the apps required for testing: git
to clone the project from the repository and g++
to compile the project.
To create a test scenario:
-
Connect to the VM via SSH and install the required apps:
sudo apt update sudo apt install -y git g++
-
Add a test scenario:
-
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 scenario in YAML format. Add the scenario 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 scenario 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 scenario is successfully completed. 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 your 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
, was passed successfully and the second stage, test
, returned 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 assert
was not executed:
How to delete the resources you created
To stop paying for your deployed server, it is enough to 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.