How to Check for Valid Scripts in GitLab?

Reading Time: 3 minutes

Hello readers, so today we will look that how can we validate scripts in GitLab. Here we are talking about bash scripts and python scripts. Both of these types of scripts have different syntaxes. So we’ll just verify the scripts whenever a push event occurs in GitLab.

Prerequisites

  • A GitLab account
  • Git Installed on local
  • Bash & Python scripts in GitLab repo

Code

So in order to get the GitLab started, we’ve to make a .gitlab-ce.yml file so that we can test the scripts for syntax validation during the pipeline run. So for the same, make a yml file with the same name mentioned above in the root directory of the project.

Now include the stages. Here we have a test stage and that stage is very common in every project it also sounds ethically correct to test the scripts in the testing stage.

Defining the Stage

After the stages are declared, we have to define them too. For defining the stages, firstly you have to give the stage name and after that the stage to which it belongs. Then you have to give the branch name as a reference so that when a change occurs in that branch the pipeline will trigger. Later you have to declare a change block for taking the changes from the directories in which scripts are present.

After defining the stage, the code will look as below snippet:

How to check for valid scripts in GitLab?

Script for Script Checking

Firstly, you have to update the GitLab runner environment to update the packages present in it and after that, we have to install git and shellcheck for bash script validation checking, and for checking the python script check, we’ll just use the python flags as python has an inbuilt tool for that.
Secondly, we’ll check for the commit so that GitLab can pick up the changed files. And after that, we’ll pick up the changed files into a variable and do the validation test on those files. Those changed files will also include the newly added files in the commit. the code snippet will look as below:

How to check for valid scripts in GitLab?

Results

After committing the changes, just go to the GitLab CI section and choose the pipelines option. The triggered pipelines will be shown over there. I did a change in one of my scripts and the changes were correct so the pipeline passed. If the pipeline succeeds then it will be shown as below snippet:

How to check for valid scripts in GitLab?

Or when the pipeline has some syntax issues then it will fail. I did introduce some errors to my script and the pipeline threw errors to me in the logs and the pipeline failed as shown below:

How to check for valid scripts in GitLab?

This tool will surely help you when you have python or bash scripts in your project and you are worried about their syntaxes. For more information, you can check out this link.

Written by 

Shubham Saini is a DevOps Engineer who loves to play with DevOps tools, Security methods and is also interested in Ethical Hacking & Cyber Security. He is a gamer also.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading