'Environment variable cannot be used in Jenkins JobDSL plugins

We use Jenkins JobDSL with ConfigurationAsCode for our CI setup. The job is checking if there is a difference in the Git repo and if there is : clone the repo, build the docker image and push it. We want to tag the image built with the same tag we used for the Git repo.

Here the job we use:

freeStyleJob("Name") {
      scm {
        git {
          branch("master")
          remote {
            credentials("account-credential")
            url("https://www.example.com")
          }
        }
      }
      triggers {
        scm("30 14 * * FRI") {
          ignorePostCommitHooks(true)
        }
      }
      steps {
        environmentVariables {
          env("TAG", "\$(git tag)")
        }
        dockerBuildAndPublish {
          dockerfileDirectory("Dockerfile")
          registryCredentials("credential-registry")
          repositoryName("folder/image-name")
          tag("$TAG")
          forcePull(false)
          createFingerprints(false)
          forceTag(false)
        }
      }
    }

The error is about the env variable TAG because it's not set like TAG=v1.0 but like TAG=$(git tag). I tried multiple ways to get the right thing : "$(git tag)", "${git tag}", $(git tag), ... But none could work. I want to be able to tag the image the same version as the git tag we put.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source