Ramprasad V
Ramprasad V

Reputation: 361

GIT URL in Jenkins pipeline

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?

Upvotes: 1

Views: 13453

Answers (1)

lvthillo
lvthillo

Reputation: 30733

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: enter image description here

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] }

Upvotes: 2

Related Questions