skip to Main Content

I have a declarative Jenkins pipeline. I am trying to pass Jenkins build parameters like jira_id and repository name along with credentials to the dockerfile as arguments.

However, on execution, the dockerfile is not able to receive any arguments as passed below.

May I please know the correct way of doing it?

Jenkinsfile Stage

stage('Create Image'){
                    steps{
                          script{
                          withCredentials([usernamePassword(credentialsId: 'admin', passwordVariable: 'admin_pw', usernameVariable: 'admin_user')]){
                          dir("${WORKSPACE}/jenkins/"){
                          def repo=docker.build("latest","--build-arg admin_user=$admin_user --build-arg admin_pw=$admin_pw --build-arg jira_id=$jira_id --build-arg repository_name=$repository_name --no-cache .")
                          }
                     }
                 }
             }
        }

Dockerfile

FROM centos:8
RUN echo "$repository_name, $jira_id, $admin_user"

3

Answers


  1. Chosen as BEST ANSWER

    Found the solution. In the dockerfile, I have to receive all the variables using ARG. ARG jira_id=$jira_id


  2. There is difference between using ARG and ENV.

    ARG can be set during the image build with –build-arg and you can’t access them anymore once the image is built.

    ENV values are accessible during the build, and afterwards once the container runs. You can set ENV values in your Dockerfile.

    enter image description here

    The Jenkinsfile stage is correct.

    Changes in Dockerfile

    FROM centos:8
    ENV jira_id=$jira_id
    ENV repository_name=$repository_name
    ENV admin_user=$admin_user
    RUN echo "$repository_name, $jira_id, $admin_user"
    
    Login or Signup to reply.
  3. While the above answers are correct, it fails if, you need dynamic naming in the "1st line" of Dockerfile i.e. if you have multiple repos to pull for same build it could look like this:

    FROM $corp_repo/centos:8
    

    To make it work, I used sed with ENV in a different way, define ENV outside Dockerfile in your jenkins stage using something like this:

    env.CORP_REPO = "${params.corp_repo}/centos:latest"

    My jenkins stage looks something like this:

        stage('Build docker') {
        daArtifactoryHelper.artifactoryDockerLogin(dockerArtifactoryAddress, artifactoryCredentialId)
        env.CORP_REPO = "${params.corp_repo}/centos:latest"
        sh 'sed -i "1c$CORP_REPO" Dockerfile'
        sh "cat Dockerfile"
        sh "docker build -t ${fullImageName}:${version} ."
        sh "docker push ${fullImageName}:${version}"
    }
    

    Here, sh 'sed -i "1c$CORP_REPO" Dockerfile' is replacing the First line with the dynamic repo name set using ENV.

    You can play around with it.
    Again, this works for nth number lines as you are defining scope outside your Dockerfile.

    Cheers!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search