Docker Jenkins Pipeline配置ssh key从gitlab拉取代码

文章目录

      • 1. 环境
      • 2. 进入容器生成ssh key
      • 3. 配置到gitlab
      • 4. Jenkins创建任务测试
      • 5. Jenkins slave使用jenkinsfile部署

1. 环境

Jenkins是k8s使用yaml运行的

[root@node02 ~]# kubectl get pods -n kube-ops
NAME                        READY     STATUS    RESTARTS   AGE
jenkins2-754c69b9c6-pr6lm   1/1       Running   0          47m

[root@node02 ~]# docker ps 
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                      NAMES
35a1313e68e2        jenkins/jenkins         "/sbin/tini -- /usr/…"   About an hour ago   Up About an hour                                               k8s_jenkins_jenkins2-754c69b9c6-pr6lm_kube-ops_28b4a548-0682-11e9-9390-000c29770a60_0

2. 进入容器生成ssh key

[root@node02 ~]# docker exec -it 35a1313e68e2 /bin/bash
#没有.ssh文件夹
jenkins@jenkins2-754c69b9c6-pr6lm:/$ cd ~/.ssh
bash: cd: /var/jenkins_home/.ssh: No such file or directory
jenkins@jenkins2-754c69b9c6-pr6lm:/$ mkdir ~/.ssh
jenkins@jenkins2-754c69b9c6-pr6lm:/$ cd ~/.ssh
jenkins@jenkins2-754c69b9c6-pr6lm:~/.ssh$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/var/jenkins_home/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /var/jenkins_home/.ssh/id_rsa.
Your public key has been saved in /var/jenkins_home/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:4dgUTYXOJnAZqn373B7upua9c2L9cngoi264fa/zUKk jenkins@jenkins2-754c69b9c6-pr6lm
The key's randomart image is:
+---[RSA 2048]----+
|        o=.o.    |
|      ..o.o      |
|      .ooo       |
|     o =..+    . |
|    . o So    o  |
|       . .   o   |
|        ..  E. o |
|        .+o=Oo* o|
|        .*OBX%o=.|
+----[SHA256]-----+
jenkins@jenkins2-754c69b9c6-pr6lm:~/.ssh$ ls
id_rsa	id_rsa.pub

#查看公钥(配置在gitlab的ssh key上才能拉取代码)
[root@node02 ~]# cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1ycxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxnOyR jenkins@jenkins2-754c69b9c6-pr6lm

3. 配置到gitlab

Docker Jenkins Pipeline配置ssh key从gitlab拉取代码_第1张图片

4. Jenkins创建任务测试

测试前要安装Maven插件
Docker Jenkins Pipeline配置ssh key从gitlab拉取代码_第2张图片
配置maven名称
Docker Jenkins Pipeline配置ssh key从gitlab拉取代码_第3张图片
创建测试流水线
Docker Jenkins Pipeline配置ssh key从gitlab拉取代码_第4张图片
流水线脚本

#!groovy
pipeline {
	agent any
	
	environment {
		REPOSITORY="ssh://[email protected]:2222/www19930327/imooc-videos-dev.git"
	}

	tools {
       		 //工具名称必须在Jenkins 管理Jenkins → 全局工具配置中预配置。
        		maven 'M3'
    	}
	
	stages {
		stage('获取代码') {
			steps {
				echo "start fetch code from git:${REPOSITORY}"
				deleteDir()
				git "${REPOSITORY}"
			}
		}
	}
}

Docker Jenkins Pipeline配置ssh key从gitlab拉取代码_第5张图片
立即构建,失败
Docker Jenkins Pipeline配置ssh key从gitlab拉取代码_第6张图片
解决办法

[root@node02 ~]# docker exec -it 35a1313e68e2 /bin/bash
jenkins@jenkins2-754c69b9c6-pr6lm:/$ cd ~/.ssh
jenkins@jenkins2-754c69b9c6-pr6lm:~/.ssh$ ls
id_rsa	id_rsa.pub  known_hosts
jenkins@jenkins2-754c69b9c6-pr6lm:~/.ssh$ git ls-remote -h ssh://[email protected]:2222/www19930327/imooc-videos-dev.git HEAD

构建成功
Docker Jenkins Pipeline配置ssh key从gitlab拉取代码_第7张图片

5. Jenkins slave使用jenkinsfile部署

安装Blue Ocean插件
Docker Jenkins Pipeline配置ssh key从gitlab拉取代码_第8张图片
点击打开Blue Ocean
Docker Jenkins Pipeline配置ssh key从gitlab拉取代码_第9张图片
点击创建流水线
Docker Jenkins Pipeline配置ssh key从gitlab拉取代码_第10张图片
配置gitlab ssh key,创建流水线(项目里面必须要有jenkinsfile文件)
Docker Jenkins Pipeline配置ssh key从gitlab拉取代码_第11张图片
运行

[root@wanfei ~]# kubectl get pods -n kube-ops
NAME                        READY     STATUS    RESTARTS   AGE
jenkins2-754c69b9c6-f8bnd   1/1       Running   0          1d
#在node节点新建了一个jenkins slave pod部署
[root@wanfei ~]# kubectl get pods -n kube-ops
NAME                        READY     STATUS    RESTARTS   AGE
jenkins2-754c69b9c6-f8bnd   1/1       Running   0          1d
jnlp-hczgb                  1/1       Running   0          4s

部署成功
Docker Jenkins Pipeline配置ssh key从gitlab拉取代码_第12张图片
master自动删除了slave的pod

[root@wanfei ~]# kubectl get pods -n kube-ops
NAME                        READY     STATUS    RESTARTS   AGE
jenkins2-754c69b9c6-f8bnd   1/1       Running   0          1d

你可能感兴趣的:(Jenkins)