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:
- A fee for a disk and a continuously running VM (see Yandex Compute Cloud pricing).
- A fee for using a dynamic or a static public IP (see Yandex Virtual Private Cloud pricing).
Create a virtual machine with GitLab
- On the management console
folder page, click Create resource and select Virtual machine. - In the Name field, enter the VM name as
gitlab
. - Select an availability zone to place the VM in.
- Under Image/boot disk selection, click the Cloud Marketplace tab and select GitLab as your public image.
- Under Disks, select a 20 GB SSD.
- Under Computing resources:
-
Choose a VM platform.
-
Specify the necessary number of vCPUs and amount of RAM.
For GitLab to run properly, specify the following configuration:
- Platform: Intel Ice Lake.
- Guaranteed vCPU share: 100%.
- vCPU: 4.
- RAM: 8 GB.
-
- Under Network settings:
- Select the Network and Subnet to connect the VM to. If the required network or subnet is not listed, create it.
- Under Public address, 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. See the section about how to connect to VMs 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 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. -
In the browser, open a link in the format
http://<public VM IP address>
. The GitLab web interface will open. -
Log in using the administrator account:
- Username or email:
root
. - Password: The previously copied password.
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:
- Click Configure GitLab.
- In the left 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
. - If necessary, give a description and the scope of the project.
- Project name:
-
Click Create project.
-
Once you create the project, in the left panel, go to Settings and select CI/CD.
-
Under Auto DevOps, click Expand, disable the option Default to Auto DevOps pipeline, and save changes by clicking Save changes.
-
Add a project file.
-
In the left panel, go to Project and select Details.
-
On the project page, click New file.
-
Name the file
test.cpp
. Add the code of a program that checks the product of multiplying 2 x 2 and outputsHello World
if it's 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 your 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-address-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
. -
Don't specify anything in the tag field, just press Enter. Otherwise, by default, the Runner won't 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 necessary apps:
sudo apt update sudo apt install -y git g++
-
Add a test script:
-
In the GitLab web interface, go to the left-hand panel, click Project, and select the Details tab.
-
On the page that opens, click Set up CI&CD.
-
A page opens asking you to add a new file named
.gitlab-ci.yml
, in which you should 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
: In the third step, an archive with the executable is created, which you can download via the GitLab web interface after the script completes successfully. In theartifacts
block, there's a list of files available for download.
The
cache
block shows which files and directories are transferred between stages. If you omit it, thehello
file won't be available at thetest
stage and an error will occur. -
Click Commit changes
-
After committing, the system automatically starts testing the last commit. You can view the testing process and results in the CI/CD section in the left panel. The resulting line should contain the first test and 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 the CI/CD section. 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 final artifacts weren't 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 created resources
To stop paying for your deployed server, just delete the created gitlab
VM.
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.