简体   繁体   English

将 Jenkins 管道参数传递给 Dockerfile

[英]Passing Jenkins Pipeline parameter to a Dockerfile

I'm getting a "Bad substitution" error when trying to pass a pipeline parameter to the Dockerfile.尝试将管道参数传递给 Dockerfile 时出现“错误替换”错误。

Jenkins parameter: version Jenkins 参数:版本

Jenkinsfile:詹金斯文件:

pipeline {
    agent any
    stages {

            stage('Build in docker container') {
                agent { dockerfile true }
                    steps {
                        sh 'node -v'
                     }
            }
    }
}

Dockerfile: Dockerfile:

FROM ubuntu:16.04

WORKDIR /root

# install dependencies
RUN apt-get update
RUN apt-get install curl wget vim nano zip git htop ncdu build-essential chrpath libssl-dev libxft-dev apt-transport-https -y

# install node 10
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash
RUN apt-get install --yes nodejs
#RUN node -v
#RUN npm -v

RUN echo ${params.version}

#ARG VERSION
#RUN echo $VERSION

Jenkins error message: Jenkins error message Jenkins 错误消息: Jenkins 错误消息

I'm sure the problem is that im new to pipelines/docker.我确定问题是我对管道/docker 不熟悉。 :) I would be grateful for any help. :) 如果有任何帮助,我将不胜感激。

issue resolved by adding the ARG variable to the Dockerfile.通过将 ARG 变量添加到 Dockerfile 解决了问题。

This is how the Dockerfile looks like:这就是 Dockerfile 的样子:

FROM ubuntu:16.04

WORKDIR /root

# install dependencies
RUN apt-get update
RUN apt-get install curl wget vim nano zip git htop ncdu build-essential chrpath libssl-dev libxft-dev apt-transport-https -y

# install node 10
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash
RUN apt-get install --yes nodejs
#RUN node -v
#RUN npm -v

ARG version=fisticuff
RUN echo $version

and this is how the Jenkinsfile looks like:这就是 Jenkinsfile 的样子:

pipeline {
    agent any
    stages {
        stage('Build in docker container') {
            agent {
                dockerfile {
                    additionalBuildArgs  '--build-arg version="$version"'
                }
            }
            steps {
                sh 'node -v'
            }
        }
    }
}

Console output in Jenkins: Jenkins console output Jenkins 中的控制台 output: Jenkins 控制台 output

Much obliged to all of you for giving me the hints.非常感谢大家给我的提示。 It helped me a lot!这对我帮助很大!

Try running Dockerfile independently first.尝试先独立运行 Dockerfile。 Since you are new to docker try one step at a time.由于您是 docker 的新手,请一次尝试一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jenkins 管道与 Dockerfile 配置 - Jenkins Pipeline with Dockerfile configuration 如何在声明式管道中将 Jenkins 构建参数作为参数传递给 dockerfile - How to pass Jenkins build parameter as arguments to dockerfile in declarative pipeline Jenkins:创建管道以读取 dockerfile - Jenkins: Creating pipeline to read dockerfile 将 dockerfile 与 Jenkins 脚本化流水线语法一起使用 - Using a dockerfile with Jenkins Scripted Pipeline Syntax 如何在基于 dockerfile 的容器中运行 Jenkins 管道? - How to run Jenkins pipeline in a container based on a dockerfile? 将运行时参数传递给DockerFile中未运行的JAR - Passing runtime parameter to JAR in DockerFile not running 将配置文件从 Dockerfile 插入到声明性 Jenkins 管道 Docker 容器中 - Insert a config file into a declarative Jenkins pipeline Docker container from Dockerfile 如何创建 Dockerfile 与 maven jdk 和 docker 安装为 ZAB63A76362C3972AC83DC5CB8 - How to creating Dockerfile with maven jdk and docker installed for jenkins pipeline 声明性 Jenkins 管道:是否可以使用 SSH 凭据构建 Dockerfile? - Declarative Jenkins pipeline: is it possible to build Dockerfile with SSH credentials? 使用 Jenkins 声明式管道为 dockerfile 代理设置构建参数 - Setting build args for dockerfile agent using a Jenkins declarative pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM