Выполнение команды внутри изображения docker в Jenkins Pipeline Stage - PullRequest
0 голосов
/ 04 августа 2020

У меня есть простой конвейер Jenkins, который создает модуль с 3 контейнерами - 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» в моей root папке, и я хочу запустить его, но почему-то он не может его найти.

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 возвращается с "/ 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

Есть идеи, как это исправить?

1 Ответ

2 голосов
/ 04 августа 2020

Может сработать, чтобы выполнить его с sh '/ example' (без точки). Вы установили его в файловую систему root, но когда команды в контейнере запускаются из Jenkins, PWD будет рабочим пространством.

...