0%

Jenkins 本地配置

Jenkins 本地配置

背景

编写 Jenkinsfile时,原来每次调试需要修改 Jenkinsfile后推送到 gitlab,然后 Jenkins Job run时,拉取远程 gitlab仓库,读取仓库下的
Jenkinsfile,开发调试效率低.

方案

为了解决拉取远程仓库的问题,那拉取本地仓库即可,这样就需要在本地启一个 Jenkins.在 Jenkins pipeline配置时,配置本地文件路径.

  • 通过 docker-compose.yml 启动 jenkins,将本地目录挂载进jenkins

  • 安装 File System SCM

  • 配置 global libraries

    - 勾选 Load implicitly,不勾选需要在 Jenkinsfile里写上 @Library

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
version: '3.7'
services:
jenkins:
image: 'jenkins/jenkins:lts'
container_name: jenkins
privileged: true
user: root
restart: always
ports:
- 8080:8080
- 50000:50000
volumes:
# - ./data/pipeline-files:/libraries/ltinyho/pipeline-files 挂载本地 Jenkinsfile目录进容器
- ./data/jenkins:/var/jenkins_home
- ./data/jenkins_master:/home/data


jenkins_slave:
build: ./jenkins-slave
image: jenkinsci/jnlp-slave
container_name: jenkins_slave_1
restart: always
depends_on:
- jenkins
environment:
JENKINS_URL: http://jenkins:8080
JENKINS_SECRET: 6b5c0348dbcb94a49389b6190e5656a96e30c77d28f0720036f5f76ba6723a21
JENKINS_AGENT_NAME: node1
JENKINS_AGENT_WORKDIR: /home/node1
user: root
links:
- "jenkins"
volumes:
# - ./data/pipeline-files:/libraries/ltinyho/pipeline-files 挂载本地 Jenkinsfile目录进容器
- ./data/jenkins_slave:/var/jenkins_home
- ./data/jenkins_node:/home
- /var/run/docker.sock:/var/run/docker.sock
- /Users/ltinyho/.ssh:/root/.ssh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
FROM jenkinsci/jnlp-slave
USER root
RUN apt-get update && \
apt-get -y install apt-transport-https \
ca-certificates \
curl \
gnupg2 \
software-properties-common && \
curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \
add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
$(lsb_release -cs) \
stable" && \
apt-get update && \
apt-get -y install docker-ce
# 本地下载好了golangci-lint和 golang copy进image,如不需要可以删除
COPY golangci-lint /usr/local/bin
COPY go1.17.3.linux-amd64.tar.gz /tmp
RUN ls && tar -C /usr/local -xzvf /tmp/go1.17.3.linux-amd64.tar.gz && \
rm -rf /tmp/go1.17.3.linux-amd64.tar.gz

ENV GOROOT=/usr/local/go
ENV CGO_ENABLED=1
ENV GO111MODULE=on
ENV GOPROXY=https://mirrors.aliyun.com/goproxy/,https://goproxy.cn,https://goproxy.io,direct
ENV PATH=/usr/local/go/bin:$PATH
RUN usermod -a -G docker jenkins
USER jenkins