env variable to be set across jenkinfile for each step

39 views
Skip to first unread message

npwo...@gmail.com

unread,
Oct 19, 2018, 6:12:13 PM10/19/18
to Jenkins Users
Hi, 
I have the following steps in my jenkinsfile that work pretty well with $GIT_VER=some number in the following step only. 
  
stage('Git Checkout') {
      steps {
        git(url: 'ssh://mygit/my.git', branch: 'develop')
        sh '''export GIT_VER=$(git tag -l [0-9].* --points-at origin/develop | tail -n1)

When I add another step in my jenkinsfile environment variable $GIT_VER is blank. Is there a way to set this variable  so it applies to each step in Jenkinfile?

Thank you for looking into this issue for me. 


Slide

unread,
Oct 19, 2018, 7:46:18 PM10/19/18
to jenkins...@googlegroups.com
You want to do something like this:

def GIT_VER = sh(returnStdout: true, script: 'git tag... | tail -n1')

Each invocation of sh will have it's own environment space.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/1482d40a-9e94-4e62-b548-6f91ab469e28%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

npwo...@gmail.com

unread,
Oct 28, 2018, 11:50:16 PM10/28/18
to Jenkins Users
Thank you for your reply.

I did try what you had suggested, but it did not work. But i could be doing something wrong as well. 

Here is my step. 
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        script {
          git(url: 'ssh://my.git', branch: 'testing', changelog: true)
          //git(url: 'ssh://my.git', branch: 'testing')
          //def GIT_VERSION=$(git tag -l [0-9].* --points-at origin/testing | tail -n1)
          //def GIT_VERSION = sh(returnStdout: true, script: 'git tag -l [0-9].* --points-at testing | tail -n1')
          sh '''
export GIT_VERSION=$(git tag -l [0-9].* --points-at origin/testing | tail -n1)
echo $GIT_VERSION'''
        }
      }
    }
  }

    stage('Parallel Deployment') {
      parallel {
        stage('Deployments') {
          steps {
            sh '''echo "Package deployment to all servers"
           echo $GIT_VERSION'''
          }
        }
        stage('server1') {
          steps {
            sh '''scp myapp-${GIT_VER}-${BUILD_NUMBER}.build'''
          }
        }
        stage('server2') {
          steps {
          sh '''scp myapp-${GIT_VER}-${BUILD_NUMBER}.build'''
          }
        }
      }
    }
}

Basically I need to get $GIT_VER into my parallel step. 

Thanks for looking into this. 

Regards,
Reply all
Reply to author
Forward
0 new messages