安装Jenkins
安装Jenkins的同时我们部署一个“节点”(slave),后期我们将这个“节点”信息配置在Jenkins中,让Jenkins在进行CI时就在这个“节点”中进行。
-
场景1:单机docker-compose部署
-
docker-compose.yml
文件version: '3' services: jenkins: restart: always image: jenkinsci/jenkins:2.147-alpine ports: - "8080:8080" volumes: - ./data/jenkins_home:/var/jenkins_home jenkins-slave: restart: always image: setzero/jenkins-slave:0.1.0 depends_on: - jenkins volumes: - /var/run/docker.sock:/var/run/docker.sock
- 启动命令
docker-compose up -d
-
-
场景2:Kubernetes集群部署
-
deploy.yml
apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: app: jenkins name: jenkins spec: replicas: 1 selector: matchLabels: app: jenkins template: metadata: labels: app: jenkins spec: containers: - image: jenkinsci/jenkins:2.147-alpine imagePullPolicy: IfNotPresent name: jenkins ports: - containerPort: 8080 protocol: TCP resources: limits: memory: 3Gi requests: memory: 2Gi volumeMounts: - mountPath: /var/jenkins_home name: jenkins volumes: - emptyDir: {} name: jenkins --- apiVersion: v1 kind: Service metadata: labels: app: jenkins name: jenkins spec: ports: - name: http port: 8080 protocol: TCP targetPort: 8080 selector: app: jenkins type: ClusterIP --- apiVersion: extensions/v1beta1 kind: Ingress metadata: labels: app: jenkins name: jenkins spec: rules: - host: 1.2.3.4.nip.io http: paths: - backend: serviceName: jenkins servicePort: 8080 path: / --- apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: app: jenkins-slave name: jenkins-slave spec: replicas: 1 selector: matchLabels: app: jenkins-slave template: metadata: labels: app: jenkins-slave spec: containers: - image: setzero/jenkins-slave:0.1.0 imagePullPolicy: IfNotPresent name: jenkins-slave ports: - containerPort: 22 protocol: TCP resources: limits: memory: 2Gi requests: memory: 1Gi volumeMounts: - mountPath: /var/run/docker.sock name: docker-sock volumes: - hostPath: path: /var/run/docker.sock name: docker-sock --- apiVersion: v1 kind: Service metadata: labels: app: jenkins-slave name: jenkins-slave spec: ports: - name: http port: 22 protocol: TCP targetPort: 22 selector: app: jenkins-slave type: ClusterIP
- 部署命令
kubectl apply -f deploy.yml -n jenkins
-
配置Jenkins
Jenkins启动后需要到容器中cat
一下/var/jenkins_home/secrets/initialAdminPassword
文件,查看初始密码。
-
安装插件,选择默认的就好了
- 配置“节点”
在安装Jenkins时我们同时部署了一个slave容器,我们将后面CI时会使用到的工具全部打包到此镜像中,这样做的目的就在于后期进行迁移或者重新搭建一套时环境是一致的。
通过菜单系统管理
-->节点管理
-->New Node
这里slave容器的root用户默认密码为
root
,也可使用ROOT_PASSWORD
环境变量在启动slave容器时进行设置,关于Jenkins-slave镜像的更多信息请查看:https://github.com/TimeBye/jenkins-slave
- 查看连接状态
- 安装插件
实战
-
新建任务,选择
流水线
-
填写Gitlab仓库地址
-
生成Secret Token,等会儿Gitlab WebHook配置时会用到它
-
编写CI要执行的语句
-
在Gitlab中配置 WebHook
-
创建成功进行测试
-
Gitlab页面顶部出现测试结果
-
返回Jenkins就可以看到自动执行CI的记录了
-