How to Create a Basic GitLab CI Pipeline: Automating Script Execution on Push
GitLab CI/CD (Continuous Integration/Continuous Delivery) allows you to automate tasks such as running scripts, building projects, or running tests automatically whenever changes are pushed to your GitLab repository. In this blog, we’ll walk through creating a .gitlab-ci.yml
file to define a simple pipeline that runs a script every time you push changes.
What is GitLab CI/CD?
GitLab CI/CD is a built-in feature that automates the execution of scripts and workflows whenever you make changes to your GitLab repository. The pipeline can run tests, check code quality, or even deploy your application. The .gitlab-ci.yml
file is where you define these jobs and pipelines.
Step 1: Set Up a GitLab Repository
If you don’t already have a GitLab repository, follow these steps to create one:
- Log in to GitLab.
- Click New Project on the dashboard.
- Enter the project name and visibility level, then click Create Project.
- Clone the repository locally using the command:
git clone https://gitlab.com/username/your-repository.git
5. Navigate into the repository directory:
cd your-repository
Now that you have a repository set up, let’s create a simple script that we will automate.
Step 2: Create a Basic Script
For this guide, we’ll create a basic script that prints “Hello, GitLab CI!” to the terminal. In your repository, create a new file called script.sh
:
#!/bin/bash
echo "Hello, GitLab CI!"
Make sure the script has execute permissions by running:
chmod +x script.sh
Now we can define the pipeline that will execute this script whenever code is pushed to the repository.
Step 3: Define a .gitlab-ci.yml
File
The heart of the GitLab CI/CD process is the .gitlab-ci.yml
file, which contains all the instructions for running jobs and pipelines.
- In the root of your repository, create a file named
.gitlab-ci.yml
:
touch .gitlab-ci.yml
2. Open the file in a text editor and add the following content:
stages:
- test
run-script-job:
stage: test
script:
- ./script.sh
Explanation:
stages
: Specifies the stages of the pipeline. In this case, we have a single stage calledtest
.run-script-job
: Defines the job that will run in the pipeline.stage: test
: Specifies that this job belongs to thetest
stage.script
: The commands that the job will execute. Here, we are running thescript.sh
file we created earlier.
Step 4: Push Changes and Trigger the Pipeline
Once you’ve defined the .gitlab-ci.yml
file and the script, commit the changes and push them to the GitLab repository:
git add .
git commit -m "Add basic GitLab CI pipeline"
git push origin main
After pushing, GitLab will automatically trigger the pipeline based on the .gitlab-ci.yml
configuration.
View the Pipeline:
- Go to your GitLab repository.
- On the left sidebar, click CI/CD > Pipelines.
- You should see the pipeline running. Once it completes, you’ll see a green checkmark if the job was successful.
Click on the pipeline to view the logs. The output should display “Hello, GitLab CI!” as expected from our script.
Conclusion
In this blog, we’ve demonstrated how to create a simple GitLab CI pipeline that automatically runs a script whenever changes are pushed to the repository. By defining a .gitlab-ci.yml
file, you can automate various tasks like testing, building, or deploying your applications with ease.
GitLab CI/CD is a powerful tool that enables seamless automation, enhancing the efficiency of your development workflow. Stay tuned for more advanced GitLab CI/CD tutorials, where we’ll explore multi-stage pipelines, job dependencies, and deploying applications.