# Struggling to Understand Environment Variables

**URL:** <https://community.jenkins.io/t/struggling-to-understand-environment-variables/5483>\
**Category:** Ask a question\
**Tags:** question\
**Created:** [January 26, 2023, 8:29pm UTC](https://community.jenkins.io/t/struggling-to-understand-environment-variables/5483 "2023-01-26T20:29:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elliott](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/elliott/32/2892_2.png) [@Elliott](https://community.jenkins.io/u/Elliott)\
**Post date:** [January 26, 2023, 8:29pm UTC](https://community.jenkins.io/t/struggling-to-understand-environment-variables/5483/1 "2023-01-26T20:29:35Z")

</div>

I’m trying to write a pipeline that pulls code from a Bitbucket repo, build it, package it into an RPM, and then pushes the RPM to a Nexus repo. It all works if I hardcode the filename into the Nexus step, but when I try to use a variable I keep getting errors. I’ve looked through the docs and found the environmental variables built into Jenkins and also Bitbucket, but when I try to test them with an echo or `sh` I run into errors or it tells me there’s no variable with that name. Here’s the beginning of my pipeline up to the test:

```auto
pipeline {

    agent {
        label 'nodejs'
    }

    parameters {
        choice choices: ['dev', 'alpha', 'reseller', 'integrate', 'productioncp', 'productionc1', 'productionc2'], description: 'Describe the environment we want to build for.', name: 'ENVIRONMENT'
        booleanParam name: 'RUN_VERACODE', defaultValue: false, description: 'Run Veracode Scan' 
    }

    triggers {
        bitbucketPush buildOnCreatedBranch: true, overrideUrl: '<repo URL>'
    }

    options {
        buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '12', daysToKeepStr: '', numToKeepStr: '12')
    }

    stages {
        stage ('Checking out from BitBucket') {
            steps {
                bbs_checkout( 
                branches: [
                    [name: '*/develop']
                    ],
                credentialsId: '<redacted>',
                id: '<redacted>',
                projectName: '<project>',
                repositoryName: '<repo name>',
                serverId: '<redacted>',
                sshCredentialsId: 'bitbucket-ssh'
                )
            }
        }

        stage ('test') {
            steps {
                script {
                  echo 'Running tests'
                  echo ${BRANCH_NAME} ${TAG_NAME} ${JOB_NAME} ${GIT_BRANCH} ${BITBUCKET_REPO_SLUG}
                }
            }
        }

```

I’ve also tried the following:

```auto
echo "${BRANCH_NAME}"
echo ${BRANCH_NAME}
echo BRANCH_NAME
echo env.BRANCH_NAME
sh "echo BRANCH_NAME ${BRANCH_NAME}"

```

I either get an error along the lines of:

```auto
git rev-list --no-walk 7a7a238ce08201d420b1b2511bd5af387a235ad9 # timeout=10
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 50: expecting '}', found 'Running tests' @ line 50, column 26.
                     - echo 'Running tests'
                            ^

```

or

```auto
> git rev-list --no-walk 61282f8f91c53cc5e759670c6d914d55d62812fd # timeout=10
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 44: Expected a symbol @ line 44, column 17.
                   echo BRANCH_NAME TAG_NAME JOB_NAME
                   ^

```

and finally

```auto
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: BRANCH_NAME for class: groovy.lang.Binding
	at groovy.lang.Binding.getVariable(Binding.java:63)
	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:266)

```

---

<div class="post-metadata">

**Author:** ![halkeye](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/halkeye/32/8_2.png) [@halkeye](https://community.jenkins.io/u/halkeye)\
**Post date:** [January 26, 2023, 8:38pm UTC](https://community.jenkins.io/t/struggling-to-understand-environment-variables/5483/2 "2023-01-26T20:38:45Z")

</div>

> [@Elliott](#):
>
> `echo ${BRANCH_NAME} `

You need to quote this.

In this case you need double . Double quotes get interpreted by groovy/Jenkins file.  
Single quotes gets passed through directly. Great for sh statements were you waNNbt shell to handle the variables

---

<div class="post-metadata">

**Author:** ![Elliott](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/elliott/32/2892_2.png) [@Elliott](https://community.jenkins.io/u/Elliott)\
**Post date:** [January 26, 2023, 8:42pm UTC](https://community.jenkins.io/t/struggling-to-understand-environment-variables/5483/3 "2023-01-26T20:42:09Z")

</div>

When I use double quotes I get:

```auto
groovy.lang.MissingPropertyException: No such property: BRANCH_NAME for class: groovy.lang.Binding

```

---

<div class="post-metadata">

**Author:** ![halkeye](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/halkeye/32/8_2.png) [@halkeye](https://community.jenkins.io/u/halkeye)\
**Post date:** [January 26, 2023, 9:05pm UTC](https://community.jenkins.io/t/struggling-to-understand-environment-variables/5483/4 "2023-01-26T21:05:13Z")

</div>

I think branch name is only auto set for multibranch pipelines. Maybe single pipelines that use SCM for the pipeline. If you fill in the text area manually it won’t be set.

I don’t know anything about the Bitbucket implementation specifically but the checkout step returns a bunch of variables.

I’m going to ping @MarkEWaite who might know more. I need to go back to bed.

---

<div class="post-metadata">

**Author:** ![Elliott](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/elliott/32/2892_2.png) [@Elliott](https://community.jenkins.io/u/Elliott)\
**Post date:** [January 26, 2023, 9:14pm UTC](https://community.jenkins.io/t/struggling-to-understand-environment-variables/5483/5 "2023-01-26T21:14:53Z")

</div>

Well my biggest issue is how to I stop hard coding:

```auto
My_Package_Name-4.9.18-00195.el7.x86_64.rpm

```

and instead can use:

```auto
${JOB_NAME}-${RELEASE}.x86_64.rpm

```

---

<div class="post-metadata">

**Author:** ![slide\_o\_mix](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/slide_o_mix/32/20977_2.png) [@slide\_o\_mix](https://community.jenkins.io/u/slide_o_mix)\
**Post date:** [January 26, 2023, 9:23pm UTC](https://community.jenkins.io/t/struggling-to-understand-environment-variables/5483/6 "2023-01-26T21:23:29Z")

</div>

“${JOB\_NAME}-${RELEASE}.x86\_64.rpm” is how you would need to use it. I don’t see where this would be used in your pipeline above, can you share your entire pipeline (redacted)?

---

<div class="post-metadata">

**Author:** ![Elliott](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/elliott/32/2892_2.png) [@Elliott](https://community.jenkins.io/u/Elliott)\
**Post date:** [January 26, 2023, 11:03pm UTC](https://community.jenkins.io/t/struggling-to-understand-environment-variables/5483/7 "2023-01-26T23:03:58Z")

</div>

Thanks for the response. I kinda inherited this when I joined my team so I went ahead and stripped down the pipeline to the bare minimum and it’s looking something like the following. I “redacted” fields since this is for my company:

```auto
pipeline {
    agent {
        label 'nodejs' 
    }

    triggers {
        bitbucketPush buildOnCreatedBranch: true, overrideUrl: 'https://<repo url>'
    }

    options {
        buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '12', daysToKeepStr: '', numToKeepStr: '12')
    }

    stages {
        stage ('Checking out from BitBucket') {
            steps {
                bbs_checkout( 
                branches: [
                    [name: '*/develop']
                    ],
                credentialsId: '8678e029-b51f-4e56-8f78-47642ef1e856',
                id: '70753d0e-e717-4085-9789-fc41cad97ecf',
                projectName: '<Project>',
                repositoryName: '<repo name>',
                serverId: 'd4051227-3587-4fb7-bb32-a05bdc3531ec',
                sshCredentialsId: 'bitbucket-ssh'
                )
            }
        }

        stage ('Build') {
            steps {
                echo 'Building Artifacts'
                sh 'make rpm'
            }
        }

        stage ('Publish to Nexus') {
            steps {
                nexusArtifactUploader(
                credentialsId: '',
                groupId: 'sms',
                nexusUrl: '<my work domain>:8081/', 
                nexusVersion: 'nexus3',
                protocol: 'http',
                repository: 'sms-staging-rpm',
                version: '*',
                artifacts: [
                    [artifactId: '',
                     classifier: '',
                     file: "${JOB_NAME}-${RELEASE}-${BUILD_NUMBER}.el7.x86_64.rpm",
                     type: 'rpm']
                ])
             }       
        }
    }
}

```

It goes through everything and in the last stage it gets the variables like it should, except the ${BUILD\_NUMER} variable has 2 leading zeros so instead of being “318” it’ll return “00318” and fails to find the correct file.

---

<div class="post-metadata">

**Author:** ![slide\_o\_mix](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/slide_o_mix/32/20977_2.png) [@slide\_o\_mix](https://community.jenkins.io/u/slide_o_mix)\
**Post date:** [January 26, 2023, 11:23pm UTC](https://community.jenkins.io/t/struggling-to-understand-environment-variables/5483/8 "2023-01-26T23:23:45Z")

</div>

What you have looks like it should definitely work. How did you determine that the filename is different and how is the file name generated in the makefile?

---

<div class="post-metadata">

**Author:** ![Elliott](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/elliott/32/2892_2.png) [@Elliott](https://community.jenkins.io/u/Elliott)\
**Post date:** [January 27, 2023, 2:53pm UTC](https://community.jenkins.io/t/struggling-to-understand-environment-variables/5483/9 "2023-01-27T14:53:17Z")

</div>

I found the filename was different because I was going through the console output. As for the filename, I’m reviewing my makefile and spec file now. Thank you all for your help.
