GIT URL in Jenkins pipeline

故事扮演 提交于 2019-12-11 14:03:54

问题


I am trying to parameterize a Jenkins pipeline. The only input parameter will be GITHUB_URL. I have a Jenkinsfile as a part of the repo. I want to use this variable (defined as parameter) in my pipeline configuration as "Repository URL". How can I access the parameter ?

I have tried $GITHUB_URL, ${GITHUB_URL} and ${params.GITHUB_URL}. No luck

Any other suggestions?


回答1:


Because you are telling you have a jenkinsfile inside your git repo I suppose you do not mean that you want to call a Jenkinsfile using parameters from a shared library.

It's also not sure if you are using a declarative or scripted pipeline. I will explain the "recommended" declarative pipeline:

pipeline {
    agent any


    parameters { 
        string(defaultValue: "https://github.com", description: 'Whats the github URL?', name: 'URL')
    }


    stages {
        stage('Checkout Git repository') {
           steps {
                git branch: 'master', url: "${params.URL}"
            }
        }

        stage('echo') {
           steps {
                echo "${params.URL}"
            }
        }
    }
}

In this pipeline you will add a string parameter to which you can add a URL. When you run the build it will ask for the parameter:

To use this parameter use "${params.URL}": This pipeline will clone the github repo in the first stage and print the URL in the next (echo) stage:

[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (echo)
[Pipeline] echo
https://github.com/lvthillo/docker-ghost-mysql.git
[Pipeline] }


来源:https://stackoverflow.com/questions/48568131/git-url-in-jenkins-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!