Punter Vicky
Punter Vicky

Reputation: 16992

Access a variable defined in Jenkinsfiles in Shell Script within Jenkinsfile

I am defining a shell script in one of the stages in my Jenkinsfile. How can I access a variable that I define in my Jenkinsfile with the shell script?

In below scenario , I am writing the value of the shell variable to a file and reading into a groovy variable. Is there a way to pass data from shell to groovy without writing it to file system?

unstash 'sources'

                    sh'''
                      source venv/bin/activate
                      export AWS_ROLE_ARN=arn:aws:iam::<accountid>:role/<role name>
                      layer_arn="$(awssume aws lambda list-layer-versions --layer-name dependencies --region us-east-1 --query \"LayerVersions[0].LayerVersionArn\" | tr -d '\"')"
                      echo $layer_arn > layer_arn
                    '''
                    layer_arn = readFile('layer_arn').trim()

Upvotes: 0

Views: 393

Answers (1)

Grunnpi
Grunnpi

Reputation: 117

  • You can can shell command line, providing variable value.
   sh "some stuff $my_var"
  • You can defined environment variable and use it within your shell
withEnv(["MY_VAR=${my_var}") {
   sh 'some stuff'
}

Regards

Upvotes: 1

Related Questions