DEV Community

Cover image for Gitlab CI/CD Auto-Pull
samnang rosady
samnang rosady

Posted on

Gitlab CI/CD Auto-Pull

GitLab CI/CD Auto-Pull is a technique that allows your remote server to automatically pull the latest code changes whenever updates are pushed to a GitLab repository. This eliminates the need for manual intervention in deployments, making the process seamless and efficient.

Image description

Why Use Auto-Pull in GitLab CI/CD? 🚀

Manually logging into a server and pulling new code updates can be tedious and error-prone. Automating this process offers several benefits:

  • ✅ Efficiency – No need to manually pull changes after every commit.
  • ✅ Consistency – Ensures that the correct version of the code is deployed.
  • ✅ Reduced Human Error – Eliminates the risk of forgetting to pull updates.
  • ✅ Faster Deployments – Code updates are available on the server as soon as they are pushed.

1. Get SSH Access:

How to get openssh-private-key

Test SSH Access

ssh <linux-user>@<PRODUCTION_IP>
Enter fullscreen mode Exit fullscreen mode

Get openssh-private-key

ssh -o StrictHostKeyChecking=no ssh <linux-user>@<PRODUCTION_IP> "cat ~/.ssh/id_rsa"
Enter fullscreen mode Exit fullscreen mode

Value should be:

-----BEGIN OPENSSH PRIVATE KEY-----
....
-----END OPENSSH PRIVATE KEY-----
Enter fullscreen mode Exit fullscreen mode

2. Set variables credential:

Go to GitLab ProjectSettingsCI/CDVariables
You can add credential variable there.
For example: openssh-private-key (PROD_SSH_PRIVATE_KEY).

PROD_SSH_PRIVATE_KEY: Should openssh-private-key of <linux-user> which accessable to project directory, should not be root user.

Key: PROD_SSH_PRIVATE_KEY
Value: <openssh-private-key>
Type: Variable
Environment scope: All (default)
Protect variable: Checked
Mask variable: Checked
Enter fullscreen mode Exit fullscreen mode

Create .gitlab-ci.yml

Go to GitLab ProjectBuildPipeline editor

variables:
  DOCKER_HOST: tcp://docker:2375
  SSH_USER: <linux-user>
  PRODUCTION_IP: <server-ip: xx.xx.xx.xx>

services:
  - docker:dind

stages:
  - deploy_production

deploy-prod:
  stage: deploy_production
  image: alpine:latest
  before_script:
    - apk add openssh-client openssh
    - eval $(ssh-agent -s)
    - echo "$PROD_SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
  script:
    - echo -e "This CI job deploys Stage= [$CI_JOB_STAGE], Branch= [$CI_COMMIT_BRANCH], Server IP= [$PRODUCTION_IP]"
    - ssh -o StrictHostKeyChecking=no ${SSH_USER}@${PRODUCTION_IP} -p 22 "cd <project-path> && git pull origin <branch>"
    - echo -e "\033[0;32mPulled [$CI_COMMIT_BRANCH] \033[0m"
  rules:
    - if: '$CI_COMMIT_BRANCH == "<branch>"'
      when: manual
Enter fullscreen mode Exit fullscreen mode

🌟 Stay tuned 🌟

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

If you found this post useful, consider leaving a ❤️ or a nice comment!

Got it