Answers: <p>I would suggest to use Docker secrets with mount command.</p>
  <pre><code>RUN --mount=type=secret,id=secret_name myVariable=$(cat /run/secrets/secret_name) &amp;&amp; $command
  </code></pre>
  <p>After that use withCredentials function and parse secret ID to a variable.</p>
  <pre><code>withCredentials([string(credentialsId: 'jenkinsSecretName', variable: 'mySecretENV')]) {
    docker build -f Dockerfile --secret id=secret_name,env=mySecretENV .
  }
  </code></pre>
  <p>Please be aware that Docker secret is available only on this specific layer where it is defined and you will not be able to access it on another layer later.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-12-14T10:16:26.980Z
Answers: Thank you.  The "RUN --mount" was new to me.  I'll have a look.
Answers: user18619318
Answers: CC BY-SA 4.0
Answers: 2022-12-12T20:48:31.547Z
Answers: 2022-12-12T20:48:31.547Z
Answers: ValeriRangelov
Body: <p>I am looking at getting a newly provisioned Jenkins to run an existing dockerfile which does a gradle build and produces a runtime image which is then to be pushed to our internal docker repository.</p>
  <p>I have the following so far:</p>
  <pre><code>pipeline {
      agent {
          // https://www.jenkins.io/doc/book/pipeline/docker/#dockerfile
          dockerfile {
              // https://www.jenkins.io/doc/book/pipeline/syntax/#agent-parameters
              additionalBuildArgs  &quot;--build-arg 'GIT_REPO=(git remote -v | head -1)' &quot; +
                      &quot;--build-arg 'JENKINS_BRANCH=$BRANCH_NAME' &quot; +
                      &quot;--build-arg 'JENKINS_BUILD_URL=$BUILD_URL' &quot; +
                      &quot;--build-arg 'GIT_SHA=(git rev-parse HEAD)' &quot; +
                      &quot;--build-arg 'ORG_GRADLE_PROJECT_artifactoryUsername=setnow' &quot; +
                      &quot;--build-arg 'ORG_GRADLE_PROJECT_artifactoryPassword=setnow'&quot;
              registryCredentialsId &quot;svc-artifactory&quot;
              label 'set by us'
  
          }
      }
  </code></pre>
  <p>and I would like to provide the proper credential to the gradle build inside by setting the <code>ORG_GRADLE_PROJECT_*</code> variables.  The credential to be used is &quot;svc-artifactory&quot; for this as well.</p>
  <p>How am I to approach this?</p>
  <p>(The GIT_* arguments are disabled shell commands I'm coming back to afterwards.  Hints appreciated)</p>
  
Tags: <docker><jenkins><jenkins-pipeline><gradle>
Title: Passing Gradle credentials into Jenkins `dockerfile`agent?
UserDisplayName: user18619318
ThreadText: # Passing Gradle credentials into Jenkins `dockerfile`agent?
  
  (Asked by: username_0 on 2022-12-12)
  
  I am looking at getting a newly provisioned Jenkins to run an existing dockerfile which does a gradle build and produces a runtime image which is then to be pushed to our internal docker repository.
  
  I have the following so far:
  
  ```
  pipeline {
      agent {
          // https://www.jenkins.io/doc/book/pipeline/docker/#dockerfile
          dockerfile {
              // https://www.jenkins.io/doc/book/pipeline/syntax/#agent-parameters
              additionalBuildArgs  "--build-arg 'GIT_REPO=(git remote -v | head -1)' " +
                      "--build-arg 'JENKINS_BRANCH=$BRANCH_NAME' " +
                      "--build-arg 'JENKINS_BUILD_URL=$BUILD_URL' " +
                      "--build-arg 'GIT_SHA=(git rev-parse HEAD)' " +
                      "--build-arg 'ORG_GRADLE_PROJECT_artifactoryUsername=setnow' " +
                      "--build-arg 'ORG_GRADLE_PROJECT_artifactoryPassword=setnow'"
              registryCredentialsId "svc-artifactory"
              label 'set by us'
  
          }
      }
  
  ```
  
  and I would like to provide the proper credential to the gradle build inside by setting the `ORG_GRADLE_PROJECT_*` variables. The credential to be used is "svc-artifactory" for this as well.
  
  How am I to approach this?
  
  (The GIT\_\* arguments are disabled shell commands I'm coming back to afterwards. Hints appreciated)
  
  **Tags:** docker, jenkins, jenkins-pipeline, gradle
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-12-12. Score: 2
  
  I would suggest to use Docker secrets with mount command.
  
  ```
  RUN --mount=type=secret,id=secret_name myVariable=$(cat /run/secrets/secret_name) && $command
  
  ```
  
  After that use withCredentials function and parse secret ID to a variable.
  
  ```
  withCredentials([string(credentialsId: 'jenkinsSecretName', variable: 'mySecretENV')]) {
    docker build -f Dockerfile --secret id=secret_name,env=mySecretENV .
  }
  
  ```
  
  Please be aware that Docker secret is available only on this specific layer where it is defined and you will not be able to access it on another layer later.
  
  > **Comments:**
  > **username_0**: Thank you.  The "RUN --mount" was new to me.  I'll have a look.  
  
  ---
  
  