因为默认的 jnlp image 是不包含 docker 命令的,所以这里需要自己先打一个安装docker 的 jnlp
FROM jenkins/jnlp-slave:latest
USER root
RUN apt update && apt install -y curl
RUN curl https://releases.rancher.com/install-docker/20.10.sh | sh
RUN usermod -aG docker jenkins
USER jenkins
ENTRYPOINT ["/usr/local/bin/jenkins-agent"]
这里为了方便我放开了 docker.sock的全部权限
sudo chmod 777 /var/run/docker.sock
关于jenkins的安装请参考
编写pipeline
pipeline {
agent {
kubernetes {
label 'jenkins-slave'
yaml '''
apiVersion: v1
kind: Pod
metadata:
name: jenkins-slave
spec:
containers:
- name: jnlp
image: libaibai/lz-jnlp-slave:4.6.1
securityContext:
privileged: true
imagePullPolicy: IfNotPresent
env:
- name: JENKINS_URL
value: http://10.43.113.251:8080
- name: JENKINS_AGENT_WORKDIR
value: /tmp
volumeMounts:
- mountPath: /var/run/docker.sock
name: docker-sock
- name: jenkins-docker-cfg
mountPath: /root/.docker
workingDir: /home/jenkins
resources:
limits:
cpu: 100m
memory: 200Mi
restartPolicy: Never
volumes:
- name: docker-sock
hostPath:
path: /var/run/docker.sock
- name: jenkins-docker-cfg
projected:
sources:
- secret:
name: regcred
items:
- key: .dockerconfigjson
path: config.json
'''
}
}
stages{
stage('get code'){
steps{
echo 'getted the code...'
sh "/usr/bin/docker info"
timeout(time: 2, unit: 'HOURS'){
dir('sourcecodes') {
git credentialsId: '0559259b-d672-457f-8d2e-d3925c412d70', url: 'https://github.com/zl86790/kanikotest.git'
}
sh "docker build -f /home/jenkins/agent/workspace/helloworld2/sourcecodes/Dockerfile -t libaibai/useless ."
}
}
}
stage('push image'){
steps{
echo 'push image...'
sh "/usr/bin/docker info"
timeout(time: 2, unit: 'HOURS'){
withDockerRegistry([ credentialsId: "3182bbdd-fd4d-4497-b262-c882b7ac0ee9", url: "" ]) {
sh "docker push libaibai/useless"
}
}
}
}
}
}