Set Up Password‑Free GitLab Pulls in Jenkins Using SSH Keys
This guide walks you through creating a dedicated GitLab account for Jenkins, generating and registering SSH keys, installing required GitLab plugins, configuring credentials and tokens, writing a Jenkins pipeline script, and automating deployment with password‑less SSH between servers.
Jenkins password‑free GitLab pull
1. Create a dedicated GitLab user for Jenkins and add an SSH key to that account.
2. On the Jenkins server generate an RSA SSH key pair:
ssh-keygen -t rsa
# Follow prompts to save to /root/.ssh/id_rsa and leave passphrase empty3. Copy the generated public key ( /root/.ssh/id_rsa.pub) and paste it into the GitLab user’s SSH‑key settings.
4. Test the key by cloning a repository from the Jenkins server:
yum install git -y
git clone [email protected]:dev01/sample.gitIf the clone succeeds, the SSH key is working.
Configure Jenkins credentials
In Jenkins go to Manage Credentials → Global credentials → Add credentials and add the private key you generated.
Install GitLab plugins
Install the following plugins from the Jenkins plugin manager:
GitLab
Gitlab Hook
Gitlab Authentication
GitLab Logo
Restart Jenkins after installation.
Generate GitLab token
Log in to GitLab with the Jenkins user, create a personal access token, and copy it.
In Jenkins, under System Configuration , paste the token into the GitLab Authentication plugin settings.
Create Jenkins pipeline
1. Create a new Pipeline project.
2. Choose “Pipeline script” and add the following Jenkinsfile:
node {
stage('拉取代码') {
git credentialsId: 'b907af22-5a74-4eee-aa5f-a822c764279c', url: '[email protected]:dev01/sample.git'
echo "Code Pull"
}
stage('代码扫描') { echo "Code Scanning" }
stage('代码构建') { echo "Code Build" }
stage('是否部署') { input '是否部署' }
stage('开始部署') { sh '/opt/jenkins/sample/sample_release.sh' }
}3. Save the pipeline.
Deployment script
Create /opt/jenkins/sample/sample_release.sh on the Jenkins server:
#!/usr/bin/env bash
scp /usr/local/src/sample.zip [email protected]:/usr/local/Set up password‑less SSH from Jenkins to the target host by adding Jenkins’s public key to /root/.ssh/authorized_keys on 172.19.182.107 and test with:
ssh [email protected]Run the job
Open the pipeline job in Jenkins, click “Build Now”, confirm the deployment prompt, and monitor the console output to ensure the code is pulled, built, and deployed to the target server.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
