The Wayback Machine - https://web.archive.org/web/20241001010835/https://www.geeksforgeeks.org/implementation-of-ci-cd-in-c-c-applicationlinux-using-shell-and-docker-executor-on-gitlab/
Open In App

Implementation of CI/CD in C/C++ Application(Linux) Using Shell and Docker Executor on GitLab

Last Updated : 01 Feb, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

There are many executors available to implement CI/CD with GitLab Runner. However, Shell and Docker are more popular among them, and we can easily configure a repository with these runners.  These runners can be chosen based on the requirements and availability of the resources. This article mainly focuses on Shell and Docker executor of C/C++ Linux application and code is written in bash script. The application can build and test using a bash script. 

Shell Executor: Shell Executor is a very simple executor that helps to build the solution locally on the machine, where GitLab Runner is installed. In this case, GitLab Runner is installed on Linux Machine, so need to install the required software in the same system.

Docker Executor: It is a powerful tool that includes much software and it can be accessed by image. The advantage of this executor is that, we do not need to install any software manually, everything will be taken care of through docker and the required image will be downloaded from the docker hub. However, the disadvantageous is that this communication is blocked in some organizations due to security purposes. So, If this is the case Shell Executor is the best alternative. 

Implementation of C/C++ on Shell Executor 

Requirements: These are the basic software which needs to install in Linux machine. However, it can be changed based on the compilation script and need to download other software if needed.

Software  

Description  

Git        This is the first requirement, to commit the changes on GitLab. It is a version control software which tracks the changing set of files
cmake  To build automation, testing, and packaging c/c++application, need to install cmake in Linux machine.
gcc It is a compiler that needs to compile the c/c++ programs 
g++ It is also a compiler that needs to compile the c/c++ programs. It can be chosen based on the written script.
grep Install it if the program is searching plain-text. 

Path Configuration: After successful installation above, need to set the path of this installed software in the machine if it is not set. Run the below command on the machine.

Variable /File                                   

Path 

G++ export GCC=/usr/bin/g++
CC export CC=/usr/bin/gcc
GREP export GREP=/usr/bin/grep
Permission  Give Permission to script before it run: the chmod -R 777 *
.gitlab-ci.yml This file should be inside root directory of the project which contains all the CI/CD configuration including software and script path. Here, you can mention how this repository should run. Before adding this file to the root directory, should check it is a valid yml file or not.

GitLab Runner Set Up: Follow the below steps to download and configure the GitLab Runner.

1. Download GitLab Runner on Linux Machine using the following command

sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

2. Give it permissions for execution using the following command

sudo chmod +x /usr/local/bin/gitlab-runner

3. Create a GitLab CI using the following command

sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

4. Install and run as service using the following command

sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner

5. Start GitLab Runner using the following command

sudo gitlab-runner start

6. Stop the GitLab Runner before registering the repository

sudo gitlab-runner stop

7. Once GitLab Runner is successfully stopped enter the following command in the terminal for repository registration.

sudo gitlab-runner register

8. When you do repository registration with GitLab Runner, the below questions have to answer.

  • Enter your GitLab instance URL: It can be different with each organization and the format will be like http://gitlab.example.com
  • Path: Go to GitLab Account → Select repository which you want to register with runner → Settings → CI/CD → Expand Runner
  • Enter the gitlab-ci token for this runner: It will be a unique token of each project which will need while registration and it can be found Path: Go to GitLab Account → Select repository which you want to register with runner → Settings → CI/CD → Expand Runner
  • Enter the gitlab-ci description for this runner: Put Runner name(any name), which will help you to remember that which runner’ is running
  • Enter the gitlab-ci tags for this runner: It is optional if you want to start GitLab runner when a specific tag is available in yml file.
  • Enter the executor: There will be a list of several executors, and type shell(as GitLab Runner will run our system)

9. After successful registration, start the GitLab Runner using the following command.

sudo gitlab-runner start

10. To verify that GitLab Runner has registered the respective repository and the runner has been started. Go to GitLab Account → Select repository which you want to register with runner → Settings → CI/CD → Expand Runner, There will be green color circle will be available and displaying message will be Runners activated for this project.  Note: If the circle is gray, it means the runner has not started and starts again.

Linux GitLab Runner Commands

Follow some more GitLab Runner commands to familiarize yourself.  

Command

Description 

sudo gitlab-runner register Register the project with GitLab Runner 
sudo gitlab-runner start Start the runner 
sudo gitlab-runner stop Stop the runner
sudo gitlab-runner status To know the status of gitlab-runner 
sudo gitlab-runner unregister –name  test-runner   Unregister the Runner of a project and replace the test-runner with your runner name and this name can be found inside the config.toml file (where your gitlab-runner ) available.
sudo gitlab-runner unregister –url http://gitlab.example.com/ –token t0k3n Remove Runner by URL and token 
sudo gitlab-runner unregister –all-runners To unregister All Runners
sudo gitlab-runner restart This command stops and then starts the GitLab Runner service
sudo gitlab-runner uninstall This command stops and uninstalls the GitLab Runner from being run as a service
sudo gitlab-runner exec To see a list of available executors, run
sudo gitlab-runner –help Check a recent list of commands by executing
sudo gitlab-runner run –help Can see the name of the environment variable
sudo gitlab-runner –debug To run a command in debug mode
sudo gitlab-runner exec shell To see a list of all available options for the shell executor, run

.gitlab-ci.yml_ shell Executor:

Below is the content of .gitlab-ci.yml on shell executor mode. However, change it if needed.

stages:
 - build
 - test

build_job:
 stage: build
 only:
   - master
 script:  
    - cd sourcecode
    - export G++=/usr/bin/g++  //if not set manually path of g++
    - export GCC=/usr/bin/gcc //if not set manually path of gcc
    - chmod -R 777 *
    - ./BuildPackage.sh
    - pwd  
     
 artifacts:
   expire_in: 365 days   //save the binary which needed while test the application, and it can be downloaded from GitLab
   paths:
      - sourcecode/binaryfolder_name  // save the binary  
       
     
test_job:
 stage: test
 only:
   - master
 script:
    - pwd  
    - cd testdir       //go to test directory for run the test case script  
    - chmod -R 777 *
    - ./tests.sh
   
 dependencies:
   - build_job
   - build_job

Implementation of C/C++ on Docker Executor: There is no need to install any software manually, everything will be taken from the docker container. However, you can install the required software inputting the name in yml file and also can export the path. To run the gitlab runner on docker executor mode, Go to GitLab Runner Set-Up(above), and select docker instead of shell. 

.gitlab-ci.yml_ Docker Executor:

Below is the content of .gitlab-ci.yml on docker executor mode. However, change it if needed. 

image: ubuntu:latest
 
stages:
 - build
 - test
 
before_script:
 - echo "Before script install this on ubuntu image "
 - apt-get update && apt-get -y install cmake && apt-get -y install gcc &&  apt-get -y install g++
 

build_job:
 stage: build
 only:
   - master
 script:  
    - cd sourcecode
    - export G++=/usr/bin/g++  //if not set manually path of g++
    - export GCC=/usr/bin/gcc //if not set manually path of gcc
    - chmod -R 777 *
    - ./BuildPackage.sh
    - pwd  
     
 artifacts:
   expire_in: 365 days   //save the binary which needed while test the application, and it can be downloaded from GitLab
   paths:
      - sourcecode/binaryfolder_name  // save the binary  
       
     
test_job:
 stage: test
 only:
   - master
 script:
    - pwd  
    - cd testdir       //go to test directory for run the test case script  
    - chmod -R 777 *
    - ./tests.sh
   
 dependencies:
   - build_job


Similar Reads

Implementation of CI/CD in Java application(Linux) Using Shell and Docker Executor on GitLab
There are many executors available to implement CI/CD with GitLab Runner. However, Shell and Docker are more popular among them, and we can easily configure a repository with these runners. These runners can be chosen based on the requirements and availability of the resources. This article mainly focuses on Shell and Docker executors of Java Linux
6 min read
Implementation of CI/CD in .NET application Using Shell Executor on GitLab
Continuous Integration and Continuous Deployment (CI/CD) have revolutionized software development, allowing teams to automate build, test, and deployment processes for rapid and reliable software delivery. In the .NET ecosystem, implementing CI/CD pipelines is important for maintaining code quality, improving collaboration, and accelerating time-to
6 min read
Shell Script to Show the Difference Between echo “$SHELL” and echo ‘$SHELL’
In shell scripting and Linux, the echo command is used to display text on the terminal or console. When used with the $SHELL variable, which contains the path of the current user's shell program, the output of the echo command can be different depending on whether the variable is enclosed in single or double quotes. echo "$SHELL" will expand the $S
4 min read
Shell Scripting - Difference between Korn Shell and Bash shell
Korn Shell: Korn Shell or KSH was developed by a person named David Korn, which attempts to integrate the features of other shells like C shell, Bourne Shell, etc. Korn Shell allows developers to generate and create new shell commands whenever it is required. Korn shell was developed a long year back as it is older than the BASH shell and it has fe
3 min read
Install GitLab By Using Docker
GitLab is a popular DevOps platform that provides a complete suite of tools for managing your software development lifecycle. It includes features like version control, CI/CD pipelines, and project management, making it an important tool for many development teams. In this article, we will walk you through the steps to install GitLab using Docker.
3 min read
Auto Logout in Linux Shell Using TMOUT Shell Variable
TMOUT is a Linux-Unix shell variable that provides the user security for their login session in the system. It provides the "Auto Logout Functionality" for the login shell in case of no activity for a specified time. Generally when the user leaves the Linux system idle (situation when a user doesn't perform any administrative task by running a comm
2 min read
How to protect Linux shell file using noclobber in bash shell?
Most Linux shells(bash, csh, ksh, tcsh) have a built-in file protection mechanism to prevent files from being overwritten accidentally. In this article, we will see how to prevent it by setting noclobber option. Clobbering Redirecting standard output to a file that already exists would overwrite the existing file content which will result in data l
2 min read
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understanding shell scripting we have to get familiar with th
7 min read
Shell Scripting - Interactive and Non-Interactive Shell
A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell accepts commands from the user and transforms them
3 min read
Difference Between Bind Shell and Reverse Shell
A shell is a program that interprets our commands and gives the written commands to the operating system. It acts as an interface between the user and the operating system. It takes input from the keyboard and gives it to the OS, and the terminal lets you type commands and interact with the shell. When to Use Reverse Shell and Bind Shell?Both rever
6 min read
Shell Scripting - Restricted Shell
Shell is one of the most important and powerful tools available in GNU/Linux-based systems. One can control the entire system if used correctly, this exact feature makes the shell a double-edged sword. It can potentially harm the system if one executes a system command without knowing its underlying details and consequences. To prevent this we use
5 min read
Shell Script to Demonstrate the Use of Shell Function Library
Shell Function Library is basically a collection of functions that can be accessed from anywhere in the development environment. It actually makes shell scripting a bit less tedious and repetitive. By creating a shell script with some functions defined in it, we can then access and call those functions from other files or scripts. It helps in avoid
3 min read
Shell Scripting - Default Shell Variable Value
A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell accepts commands from the user and transforms them
3 min read
Korn Shell vs Bash Shell
Introduction : Korn Shell, also known as ksh, is a Unix shell that was developed by David Korn in the early 1980s. It was designed to be a more powerful and interactive shell than the Bourne Shell (sh), which was the standard Unix shell at the time. The Korn Shell has features such as command-line editing, job control, and shell scripting, making i
6 min read
Shell Scripting - Shell Signals Values
Prerequisites: Processes, Bash Scripting, Shell Function Library Signals help the operating system to communicate with processes and vice-versa. Signals are also generated when processes don't function properly or try to access prohibited memory. Different signals are mapped to different numbers which are referred to as signal values. The Linux OS
6 min read
Shell Scripting - Shell Variables
A shell variable is a character string in a shell that stores some value. It could be an integer, filename, string, or some shell command itself. Basically, it is a pointer to the actual data stored in memory. We have a few rules that have to be followed while writing variables in the script (which will be discussed in the article). Overall knowing
6 min read
Token-Hunter: Collect OSINT for GitLab groups and members
Token-Hunter is a free and open-source tool available on GitHub. This tool is based upon the technology of OSINT. This tool is a free and open-source tool it means you don't have to give any amount to anyone. Download and install this tool free of cost This tool is used as intended to complement different tools such as gitrob, gitleaks, TruffleHog.
2 min read
Difference Between GitLab and GitHub
Version control systems are important tools for managing code changes and collaboration. GitLab and GitHub are two of the most popular platforms in this space. However, they have key differences that can influence which one is the best fit for your project. In this article, we will explore the differences between GitLab and GitHub, highlighting the
3 min read
Ultimate Guide to Advanced Git and GitHub: Overview of GitLab & Bitbucket
In software development, mastering advanced tools and techniques is important for efficiency and collaboration. Git, GitHub, GitLab, and Bitbucket offer features beyond basic version control, like rebasing, cherry-picking, stashing, submodules, and Git hooks in Git, as well as GitHub Actions, GitLab CI/CD pipelines, and Bitbucket Pipelines. This ar
5 min read
Permissions And Roles in GitLab
One of the important settings of GitLab is its permission and role system, which decides what users can and cannot do within a project or group. Understanding GitLab’s permission model is important for managing access, collaboration, and security within your teams and projects. This article will explore the various permissions and roles available i
5 min read
How To Create And Manage GitLab Groups?
GitLab is a powerful platform that enables teams to collaborate on code, manage CI/CD pipelines, and track project progress. For organizations with multiple projects, effective project organization and access control are important. GitLab Groups help you organize related projects under a single entity and manage permissions for multiple users acros
6 min read
How to Delete Branch in Gitlab?
When working with Git, it's common to create branches to isolate changes and work on new features or fixes. However, once a branch has served its purpose, it's often necessary to delete it to keep your repository clean and organized. In this article, we'll see the process of deleting a Git branch using Git commands locally on your machine. Using Gi
2 min read
How to Install GitLab Runner on Ubuntu?
GitLab Runner is a fantastic tool that works like a trusty helper for developers. It takes care of tasks that ensure your software runs smoothly, like testing and deploying. Installing GitLab Runner on your Ubuntu system is like giving yourself a superpower in software development. Automating many repetitive tasks makes your life easier, allowing y
4 min read
How To Clone a Repository From Gitlab?
Cloning a repository in Git involves creating a local copy of a project from a remote server. This allows you to work on the project on your local machine and later push your changes back to the remote repository. GitLab, one of the popular platforms for hosting Git repositories, provides a straightforward way to clone repositories. PrerequisitesGi
3 min read
Managing Git Repositories with GitLab
GitLab is a powerful platform for managing Git repositories, offering a list of features that provide collaboration, code review, continuous integration/continuous deployment (CI/CD), and more. This guide will walk you through Managing Git Repositories with GitLab. Table of Content How to Access GitLab?Uploading a code file in the repositoryCreatin
3 min read
How To Create A Personal Access Token in GitLab?
A personal access token (PAT) in GitLab is an alternative to using a password for authenticating GitLab API requests, Git operations, and other integrations. These tokens provide more control and security, allowing you to define specific scopes and expiration dates. In this article, we'll cover how to create a personal access token in GitLab, inclu
4 min read
GitLab Upgrade Path
Upgrading GitLab is an important task for maintaining the security, stability, and performance of your GitLab instance. It’s important to follow the recommended upgrade paths to ensure a smooth transition without breaking your setup. GitLab regularly releases new versions, including major, minor, and patch updates, each introducing new features, en
4 min read
Introduction To GitLab Flow
While Git itself offers a lot of flexibility, managing workflows can become complex, especially when dealing with different environments like development, staging, and production. This is where GitLab Flow comes into play, providing a structured approach to handling branching and deployment in a way that aligns with your development and deployment
4 min read
How To Remove A GitLab Project?
Removing a GitLab project can be necessary when you no longer need it, or if you want to clean up your workspace. Deleting a project in GitLab is straightforward but requires caution since it permanently erases all data, including repositories, issues, and merge requests associated with the project. In this guide, we will walk you through the steps
3 min read
Gitlab CLI
GitLab, a leading platform for version control, offers a range of tools for managing your repositories, CI/CD pipelines, and more. The GitLab Command Line Interface (CLI) is one of the most powerful tools provided by GitLab and helps to interact with GitLab services directly from the terminal. The GitLab CLI simplifies tasks like managing repositor
5 min read
Practice Tags :