GitLab CI/CD is a powerful suite of tools built into GitLab that automates the process of building, testing, and deploying code. In this article, we'll walk through how to create a CI/CD pipeline in GitLab to automatically publish changes to a production server whenever a push is made to a repository.
Before you begin, make sure you have the following requirements:
- A GitLab account with access to a GitLab project.
- A production server with SSH access.
- SSH keys configured for password-less access to the production server.
- A
.gitlab-ci.yml
configuration file in your GitLab repository.
To allow GitLab to connect to your production server, you'll need to configure SSH keys. Generate an SSH key on your local machine (if you don't already have one):
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Add the public key to the ~/.ssh/authorized_keys
file on your production server.
Add the private key as a secret variable on GitLab:
- Go to your GitLab project.
- Navigate to Settings > CI / CD > Variables.
- Add a new variable with the name
SSH_PRIVATE_KEY
and paste the contents of your private key.
The .gitlab-ci.yml
file defines the CI/CD pipelines. Here is an example of a basic configuration for deployment to a production server:
stages:
- build
- deploy
variables:
SSH_KEY: $SSH_PRIVATE_KEY
DEPLOY_SERVER: "user@your_production_server"
DEPLOY_PATH: "/path/to/your/project"
before_script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- echo "$SSH_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan -H $DEPLOY_SERVER >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
build:
stage: build
script:
- echo "This is where you would put build commands, e.g., npm install"
deploy:
stage: deploy
script:
- echo "Deploying to production server..."
- rsync -avz --delete-after --exclude '.git' ./ $DEPLOY_SERVER:$DEPLOY_PATH
only:
- master
In detail:
- Stages: Defines the stages of the pipeline. In this example, we have two stages:
build
anddeploy
. - Variables: Defines the environment variables. We use
SSH_PRIVATE_KEY
,DEPLOY_SERVER
andDEPLOY_PATH
. - before_script: Contains commands to run before the jobs of each stage. Here we configure the SSH environment.
- build: An example build job. You can add the commands needed to build your project.
- deploy: The deploy job that uses
rsync
to copy the files to the production server. This job only runs on pushes to themaster
branch.
After configuring the .gitlab-ci.yml
file, do a commit and push to the master
branch. GitLab will automatically start the CI/CD pipeline. You can monitor the progress and see logs for each job in the CI / CD > Pipelines section of your GitLab project.
Conclusion
We have seen how to configure a CI/CD pipeline on GitLab to automatically publish changes to a production server. This basic setup can be extended and customized to fit the specific needs of your project, including additional testing phases, notifications, and automatic rollbacks. The key to a successful CI/CD process is automation and repeatability, ensuring that every code change is tested and deployed consistently.