Jenkinsfile 代理 "dockerfile" 参数化 'dir' 参数

Jenkinsfile agent "dockerfile" parameterized 'dir' argument

我在 Jenkins 2.280 中使用具有顺序阶段的声明性管道。

dockerfiledir 参数应该从 ${params}:

获得
pipeline {
  agent none
  parameters {
    choice(name: PLATFORM, choices: ['dir1', 'dir2', ...], description: '')
    string(name: FOO, defaultValue: 1001, description: 'Interesting number')
  }
  stages {
    stage('Top') {
      agent {
        dockerfile {
          filename 'Dockerfile'
          dir 'platforms/${params.PLATFORM}'   // <-- HERE
          label 'master'
          additionalBuildArgs '--build-arg FOO=${params.FOO}'
          args '--mount type=bind,source=/opt/tools,target=/tools,readonly'
        }
      }  // agent
      stages {
        stage('Checkout') {
          steps { checkout scm }
        }
        stage('Build') {
          sh """
            // call cmake
            // call ninja
          """
        }
        ...
      } // stages
    } // Top
  } // stages
} // pipeline

这似乎总是导致 Java NoSuchFileException:

java.nio.file.NoSuchFileException: /var/jenkins_home/workspace/<ws_name>/platforms/${params.PLATFORM}/Dockerfile
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)

如果我硬编码 dir 参数到一个有效的目录名,那么我 运行 会遇到 FOO 的问题:

[Pipeline] withEnv
[Pipeline] {
[Pipeline] isUnix
[Pipeline] readFile
[Pipeline] sh
14:18:02  /var/jenkins_home/workspace/<ws_name>@tmp/durable-5884cc09/script.sh: 1: /var/jenkins_home/workspace/<ws_name>@tmp/durable-5884cc09/script.sh: Bad substitution

这两件事怎么办?

有没有更好的方法?

更新:我的问题是管道项目名称中的“space”。

我试图创建一个类似于您的示例。请查看 Docker 文件和 dockerfile 代理需要如何在声明性管道中使用

Docker文件

FROM alpine
ARG PACKAGE
RUN apk add ${PACKAGE}

詹金斯文件

pipeline {
  agent {
     dockerfile {
        filename 'Dockerfile'
        dir "${params.DOCKERFILE_PATH}"
        additionalBuildArgs  "--build-arg PACKAGE=${params.PACKAGE}"
        args '-v /tmp:/tmp'
     }
   }
   parameters {
        string(name: 'DOCKERFILE_PATH', defaultValue: 'src01', description: 'Pick Dockerfile from a folder')
        text(name: 'PACKAGE', defaultValue: 'curl', description: 'Package To Be Installed')
   }
   stages {
     stage('build'){
        steps {
          sh 'git version'
        }
     } 
   }
}

groovy 必须插入您的运行时参数,为此,您必须用 "" 包装它。 '' 不会工作

在你的情况下,它将是

dockerfile {
    filename 'Dockerfile'
    dir "platforms/${params.PLATFORM}"
    label 'master'
    additionalBuildArgs "--build-arg FOO=${params.FOO}"
    args '--mount type=bind,source=/opt/tools,target=/tools,readonly'
}