简体   繁体   中英

Jenkins Pipeline get variable from git commit

I have a question about a Jenkins pipeline. I would like to have a variable that gets data from the git commit.

For example, if the git commit says "Version 1.0.0" then the variable in the Jenkins file should be "1.0.0". If the commit are 2.0.0 then the variable should be 2.0.0

I've already seen that with the option changelog in Jenkins you can get data from the Git Commit, unfortunately I don't know how to put this data into a variable?

Can anyone help me?

I have already seen and tried the following

pipeline {
    when {
        changelog '1.0.0.0'
    }
    environment {
        nicevariable = " here should be the gitcommit see changelog"
    }



    agent none
    stages {
        stage("first") {
            sh "echo ${nicevariable}"
        }
    }
}

You can define environment variable dynamically , eg:

pipeline {
    agent any
    environment {
        GIT_MESSAGE = """${sh(
            script: 'git log --no-walk --format=format:%s ${GIT_COMMIT}', 
            returnStdout: true
            )}"""
    }
    stages {
        stage('test') {
            steps {
                sh 'echo "Message: --${GIT_MESSAGE}--"'
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM