PowerStat
PowerStat

Reputation: 3821

Is it possible to check if checked out from a specific repository in a Jenkins declarative pipeline?

I would like to have a release stage in my Jenkinsfile that only runs when it's checked out from the original repository. This is to avoid error messages on cloned repositories, because of missing keys etc. there.

stage('Release')
 {
  when
   {
    allOf 
     {
      // TODO Check for repository url https://github.com/PowerStat/TemplateEngine.git
      branch 'master'
     }
   }
  steps
   {
    script
     {
      if (isUnix()) 
       {           
        sh 'mvn --batch-mode release:clean'
        sh 'mvn --batch-mode release:prepare'
        sh 'mvn --batch-mode release:perform'
       }
      else
       {
        bat 'mvn --batch-mode release:clean'
        bat 'mvn --batch-mode release:prepare'
        bat 'mvn --batch-mode release:perform'
       }
     }
   }
 }

I have studied Pipeline Syntax: when but have no idea how to do the test I would like to have.

Also I thought about using an environment variable Global Variable Reference, but found non with the repository URL in it.

So my question is: how to implement this check in a decalarative pipeline?

Upvotes: 3

Views: 1310

Answers (1)

Szymon Stepniak
Szymon Stepniak

Reputation: 42174

You can get remote repository URL from git config remote.origin.url command. You can execute this command using expression directive inside the when block - it defines a closure that returns a boolean value.

Consider the following example:

def expectedRemoteUrl = "https://github.com/PowerStat/TemplateEngine.git"

pipeline {
    agent any

    stages {
        stage("Release") {
            when {
                allOf {
                    branch 'tmp'
                    expression {
                        def remoteUrl = isUnix() ?
                            sh(script: "git config remote.origin.url", returnStdout: true)?.trim() :
                            bat(script: "git config remote.origin.url", returnStdout: true)?.trim()

                        return expectedRemoteUrl == remoteUrl
                    }
                }
            }

            steps {
                echo "Do your release steps here..."
            }
        }
    }
}

Alternatively, if git command is not available in the node that runs the pipeline, you can get the remote repository URL with scm.userRemoteConfigs?.first()?.url. Consider the following example:

def expectedRemoteUrl = "https://github.com/PowerStat/TemplateEngine.git"

pipeline {
    agent any

    stages {
        stage("Release") {
            when {
                allOf {
                    branch 'tmp'
                    expression {
                        def remoteUrl = scm.userRemoteConfigs?.first()?.url

                        return expectedRemoteUrl == remoteUrl
                    }
                }
            }

            steps {
                echo "Do your release steps here..."
            }
        }
    }
}

Upvotes: 1

Related Questions