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.
- A GitLab account
- Git Installed on local
- Bash & Python scripts in GitLab repo
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:
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:
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:
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:
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.