运行 Jenkins Pipeline Stage 中 docker 图像中的命令
Running a command inside a docker image in Jenkins Pipeline Stage
我有一个简单的 Jenkins 管道,它创建了一个包含 3 个容器的 pod - jnlp、dind、example-test
看起来如下 -
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
metadata:
name: example-pb
annotations:
container.apparmor.security.beta.kubernetes.io/dind: unconfined
container.seccomp.security.alpha.kubernetes.io/dind: unconfined
labels:
some-label: label1
spec:
serviceAccountName: example
securityContext:
runAsUser: 10000
runAsGroup: 10000
containers:
- name: jnlp
image: 'jenkins/jnlp-slave:4.3-4-alpine'
args: ['$(JENKINS_SECRET)', '$(JENKINS_NAME)']
- name: dind
image: docker:dind
securityContext:
runAsUser: 0
runAsGroup: 0
fsGroup: 0
privileged: true
tty: true
volumeMounts:
- name: var-run
mountPath: /var/run
- name: example-test
image: pranavbhatia/example-test:0.1
securityContext:
runAsUser: 0
runAsGroup: 0
fsGroup: 0
volumeMounts:
- name: var-run
mountPath: /var/run
volumes:
- emptyDir: {}
name: var-run
"""
}
}
还定义了几个阶段-
stages {
stage ('DIND') {
steps {
container('dind') {
sh 'pwd && echo "Pulling image" && docker pull ubuntu:18.04'
}
}
}
stage ('EXAMPLE') {
steps {
container('example-test') {
sh './example'
}
}
}
所以现在我的根文件夹中有这个脚本“example”,我想 运行 但不知何故找不到它。
Dockerfile 看起来像这样 -
FROM ubuntu:18.04
COPY ./example ./example
#make it executable
RUN chmod +x ./example
#command to keep container running in detached mode
CMD tail -f /dev/null
pwd returns 使用“/home/jenkins/agent/workspace/test-pipeline”而不是 docker 容器路径。
输出结果如下-
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Created Pod: test-pipeline-14-s7167-4zcg5-s68gw in namespace dc-pipeline
Still waiting to schedule task
‘test-pipeline-14-s7167-4zcg5-s68gw’ is offline
Agent test-pipeline-14-s7167-4zcg5-s68gw is provisioned from template test-pipeline_14-s7167-4zcg5
---
apiVersion: "v1"
kind: "Pod"
metadata:
annotations:
container.apparmor.security.beta.kubernetes.io/dind: "unconfined"
container.seccomp.security.alpha.kubernetes.io/dind: "unconfined"
buildUrl: "http://jenkins-164-229:8080/job/test-pipeline/14/"
runUrl: "job/test-pipeline/14/"
labels:
some-label: "label1"
jenkins: "slave"
jenkins/label: "test-pipeline_14-s7167"
name: "test-pipeline-14-s7167-4zcg5-s68gw"
spec:
containers:
- args:
- "$(JENKINS_SECRET)"
- "$(JENKINS_NAME)"
env:
- name: "JENKINS_SECRET"
value: "********"
- name: "JENKINS_TUNNEL"
value: "jenkins-164-229-agent:50000"
- name: "JENKINS_AGENT_NAME"
value: "test-pipeline-14-s7167-4zcg5-s68gw"
- name: "JENKINS_NAME"
value: "test-pipeline-14-s7167-4zcg5-s68gw"
- name: "JENKINS_AGENT_WORKDIR"
value: "/home/jenkins/agent"
- name: "JENKINS_URL"
value: "http://jenkins-164-229:8080/"
- name: "HOME"
value: "/home/jenkins"
image: "jenkins/jnlp-slave:4.3-4-alpine"
name: "jnlp"
volumeMounts:
- mountPath: "/home/jenkins/agent"
name: "workspace-volume"
readOnly: false
- image: "pranavbhatia/example-test:0.1"
name: "example-test"
securityContext:
runAsGroup: 0
runAsUser: 0
volumeMounts:
- mountPath: "/var/run"
name: "var-run"
- mountPath: "/home/jenkins/agent"
name: "workspace-volume"
readOnly: false
- image: "docker:dind"
name: "dind"
securityContext:
privileged: true
runAsGroup: 0
runAsUser: 0
tty: true
volumeMounts:
- mountPath: "/var/run"
name: "var-run"
- mountPath: "/home/jenkins/agent"
name: "workspace-volume"
readOnly: false
nodeSelector:
beta.kubernetes.io/os: "linux"
restartPolicy: "Never"
securityContext:
runAsGroup: 10000
runAsUser: 10000
serviceAccountName: "example"
volumes:
- emptyDir: {}
name: "var-run"
- emptyDir:
medium: ""
name: "workspace-volume"
Running on test-pipeline-14-s7167-4zcg5-s68gw in /home/jenkins/agent/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (DIND)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ pwd
/home/jenkins/agent/workspace/test-pipeline
+ echo 'Pulling image'
Pulling image
+ docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
7595c8c21622: Pulling fs layer
d13af8ca898f: Pulling fs layer
70799171ddba: Pulling fs layer
b6c12202c5ef: Pulling fs layer
b6c12202c5ef: Waiting
d13af8ca898f: Verifying Checksum
d13af8ca898f: Download complete
70799171ddba: Verifying Checksum
70799171ddba: Download complete
b6c12202c5ef: Verifying Checksum
b6c12202c5ef: Download complete
7595c8c21622: Verifying Checksum
7595c8c21622: Download complete
7595c8c21622: Pull complete
d13af8ca898f: Pull complete
70799171ddba: Pull complete
b6c12202c5ef: Pull complete
Digest: sha256:a61728f6128fb4a7a20efaa7597607ed6e69973ee9b9123e3b4fd28b7bba100b
Status: Downloaded newer image for ubuntu:18.04
docker.io/library/ubuntu:18.04
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (EXAMPLE)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ pwd
/home/jenkins/agent/workspace/test-pipeline
+ ./example
/home/jenkins/agent/workspace/test-pipeline@tmp/durable-26584660/script.sh: 1: /home/jenkins/agent/workspace/test-pipeline@tmp/durable-26584660/script.sh: ./example: not found
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE
知道如何解决这个问题吗?
使用 sh '/example'(不带点)执行它可能有效。您将它安装在根文件系统中,但是当容器中的命令来自 Jenkins 运行 时,PWD 将成为工作空间。
我有一个简单的 Jenkins 管道,它创建了一个包含 3 个容器的 pod - jnlp、dind、example-test
看起来如下 -
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
metadata:
name: example-pb
annotations:
container.apparmor.security.beta.kubernetes.io/dind: unconfined
container.seccomp.security.alpha.kubernetes.io/dind: unconfined
labels:
some-label: label1
spec:
serviceAccountName: example
securityContext:
runAsUser: 10000
runAsGroup: 10000
containers:
- name: jnlp
image: 'jenkins/jnlp-slave:4.3-4-alpine'
args: ['$(JENKINS_SECRET)', '$(JENKINS_NAME)']
- name: dind
image: docker:dind
securityContext:
runAsUser: 0
runAsGroup: 0
fsGroup: 0
privileged: true
tty: true
volumeMounts:
- name: var-run
mountPath: /var/run
- name: example-test
image: pranavbhatia/example-test:0.1
securityContext:
runAsUser: 0
runAsGroup: 0
fsGroup: 0
volumeMounts:
- name: var-run
mountPath: /var/run
volumes:
- emptyDir: {}
name: var-run
"""
}
}
还定义了几个阶段-
stages {
stage ('DIND') {
steps {
container('dind') {
sh 'pwd && echo "Pulling image" && docker pull ubuntu:18.04'
}
}
}
stage ('EXAMPLE') {
steps {
container('example-test') {
sh './example'
}
}
}
所以现在我的根文件夹中有这个脚本“example”,我想 运行 但不知何故找不到它。
Dockerfile 看起来像这样 -
FROM ubuntu:18.04
COPY ./example ./example
#make it executable
RUN chmod +x ./example
#command to keep container running in detached mode
CMD tail -f /dev/null
pwd returns 使用“/home/jenkins/agent/workspace/test-pipeline”而不是 docker 容器路径。
输出结果如下-
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Created Pod: test-pipeline-14-s7167-4zcg5-s68gw in namespace dc-pipeline
Still waiting to schedule task
‘test-pipeline-14-s7167-4zcg5-s68gw’ is offline
Agent test-pipeline-14-s7167-4zcg5-s68gw is provisioned from template test-pipeline_14-s7167-4zcg5
---
apiVersion: "v1"
kind: "Pod"
metadata:
annotations:
container.apparmor.security.beta.kubernetes.io/dind: "unconfined"
container.seccomp.security.alpha.kubernetes.io/dind: "unconfined"
buildUrl: "http://jenkins-164-229:8080/job/test-pipeline/14/"
runUrl: "job/test-pipeline/14/"
labels:
some-label: "label1"
jenkins: "slave"
jenkins/label: "test-pipeline_14-s7167"
name: "test-pipeline-14-s7167-4zcg5-s68gw"
spec:
containers:
- args:
- "$(JENKINS_SECRET)"
- "$(JENKINS_NAME)"
env:
- name: "JENKINS_SECRET"
value: "********"
- name: "JENKINS_TUNNEL"
value: "jenkins-164-229-agent:50000"
- name: "JENKINS_AGENT_NAME"
value: "test-pipeline-14-s7167-4zcg5-s68gw"
- name: "JENKINS_NAME"
value: "test-pipeline-14-s7167-4zcg5-s68gw"
- name: "JENKINS_AGENT_WORKDIR"
value: "/home/jenkins/agent"
- name: "JENKINS_URL"
value: "http://jenkins-164-229:8080/"
- name: "HOME"
value: "/home/jenkins"
image: "jenkins/jnlp-slave:4.3-4-alpine"
name: "jnlp"
volumeMounts:
- mountPath: "/home/jenkins/agent"
name: "workspace-volume"
readOnly: false
- image: "pranavbhatia/example-test:0.1"
name: "example-test"
securityContext:
runAsGroup: 0
runAsUser: 0
volumeMounts:
- mountPath: "/var/run"
name: "var-run"
- mountPath: "/home/jenkins/agent"
name: "workspace-volume"
readOnly: false
- image: "docker:dind"
name: "dind"
securityContext:
privileged: true
runAsGroup: 0
runAsUser: 0
tty: true
volumeMounts:
- mountPath: "/var/run"
name: "var-run"
- mountPath: "/home/jenkins/agent"
name: "workspace-volume"
readOnly: false
nodeSelector:
beta.kubernetes.io/os: "linux"
restartPolicy: "Never"
securityContext:
runAsGroup: 10000
runAsUser: 10000
serviceAccountName: "example"
volumes:
- emptyDir: {}
name: "var-run"
- emptyDir:
medium: ""
name: "workspace-volume"
Running on test-pipeline-14-s7167-4zcg5-s68gw in /home/jenkins/agent/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (DIND)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ pwd
/home/jenkins/agent/workspace/test-pipeline
+ echo 'Pulling image'
Pulling image
+ docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
7595c8c21622: Pulling fs layer
d13af8ca898f: Pulling fs layer
70799171ddba: Pulling fs layer
b6c12202c5ef: Pulling fs layer
b6c12202c5ef: Waiting
d13af8ca898f: Verifying Checksum
d13af8ca898f: Download complete
70799171ddba: Verifying Checksum
70799171ddba: Download complete
b6c12202c5ef: Verifying Checksum
b6c12202c5ef: Download complete
7595c8c21622: Verifying Checksum
7595c8c21622: Download complete
7595c8c21622: Pull complete
d13af8ca898f: Pull complete
70799171ddba: Pull complete
b6c12202c5ef: Pull complete
Digest: sha256:a61728f6128fb4a7a20efaa7597607ed6e69973ee9b9123e3b4fd28b7bba100b
Status: Downloaded newer image for ubuntu:18.04
docker.io/library/ubuntu:18.04
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (EXAMPLE)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ pwd
/home/jenkins/agent/workspace/test-pipeline
+ ./example
/home/jenkins/agent/workspace/test-pipeline@tmp/durable-26584660/script.sh: 1: /home/jenkins/agent/workspace/test-pipeline@tmp/durable-26584660/script.sh: ./example: not found
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE
知道如何解决这个问题吗?
使用 sh '/example'(不带点)执行它可能有效。您将它安装在根文件系统中,但是当容器中的命令来自 Jenkins 运行 时,PWD 将成为工作空间。