Trouble setting and reading variable

107 Views Asked by At

In my pipeline I have this:

steps {
    script
    {
        def TAG_NAME = env.GIT_BRANCH.split("/")[1]
    }
    sshagent(credentials: ["<snip>"]) {
        sh """
            git tag -a "v.${TAG_NAME}.${env.BUILD_NUMBER}" -m "Add tag"
            git push --tags
        """
    }
}

But when this runs, I see that TAG_NAME is 'null.'

How do I make it so that I can see it in the sshagent.

1

There are 1 best solutions below

0
On BEST ANSWER

A declarative pipeline example to set and get variables across the stages in a different ways .

def x

pipeline {
    agent any;
    stages {
        stage('stage01') {
            steps {
                script {
                    x = 10
                }
            }
        }
        stage('stage02') {
            steps {
                sh "echo $x"
                echo "${x}"
                script {
                    println x
                }
            }
        }
    }
}

on your example, it could be

def TAG_NAME
pipeline {
  agent any;
  stages {
    stage('stageName') {
       steps {
          script {
            TAG_NAME = env.GIT_BRANCH.split("/")[1]
          }
          sshagent(credentials: ["<snip>"]) {
            sh """
              git tag -a "v.${TAG_NAME}.${env.BUILD_NUMBER}" -m "Add tag"
              git push --tags
            """
          }
       }
    }

  }
}