Welcome to Day 4 of our exciting journey through GitLab CI/CD! With the basics of Git and GitLab's repository structure under your belt, we're now ready to dive into the heart of this series – setting up your first CI/CD pipeline.
What is a GitLab CI/CD Pipeline?
A CI/CD pipeline is a series of steps that automatically test and deploy your code every time changes are made. These steps, or 'jobs', are defined in a .gitlab-ci.yml
file in the root of your GitLab repository. Each job is an instance of GitLab Runner, which is a service that processes the jobs.
Creating Your First CI/CD Pipeline
Here's how you can create a basic pipeline with a single job that prints "Hello, world!" to the console:
In your project, create a new file named
.gitlab-ci.yml
.In the file, add the following content:
hello_world_job:
script: echo "Hello, world!"
This code defines a single job named hello_world_job
. The script
keyword is followed by the command to be executed by the Runner - in this case, printing "Hello, world!" to the console.
Commit and push this file to your GitLab repository. GitLab will automatically detect the
.gitlab-ci.yml
file and start the CI/CD pipeline.You can view the pipeline by going to CI/CD > Pipelines in your GitLab project.
There, you should see a new pipeline with one job - hello_world_job
. If you click on the job, you can see the job's output. There should be a line that says "Hello, world!"
Understanding Stages and Jobs
In a real-world project, your CI/CD pipeline will likely have multiple jobs. These jobs can be grouped into 'stages'. By default, every pipeline has three stages: build, test, and deploy. Jobs in the same stage run in parallel, while jobs in the next stage only run once the jobs in the previous stage have completed successfully.
Here's an example of a pipeline with two stages:
stages:
- build
- test
build_job:
stage: build
script: echo "Building the project..."
test_job:
stage: test
script: echo "Testing the project..."
In this pipeline, build_job
will run first. Once build_job
has completed, test_job
will run.
That's it for today! You've created your first GitLab CI/CD pipeline and learned about stages and jobs. In the coming days, we'll go deeper into the world of GitLab CI/CD, covering topics such as testing, deploying, and more. Stay tuned, and see you tomorrow!