RakeshKShukla
RakeshKShukla

Reputation: 11

Openshift retrieve branch name in jenkinsfile

I have configured webhook on bitbucket server which points to Openshift. I want to get GIT repo url , git branch etc from webhook payload in my inline jenkinsfile but I dont know how to retrieve them. (Webhook triggers build though).

Is it possible ?

Here is my BuildConfig

apiVersion: build.openshift.io/v1
kind: BuildConfig
metadata:

  labels:
    application: spring-demo
    template: openjdk18-web-basic-s2i
  name: spring-demo
  spec:
  output:
    to:
      kind: ImageStreamTag
      name: 'spring--demo:latest'
  runPolicy: Serial
  source:
    type: None
  strategy:
    jenkinsPipelineStrategy:
      jenkinsfile: |-
        pipeline {
        agent {
          label 'maven'
        }
        stages {
          stage('Build') {
            steps{
               sh "echo ${env.BRANCH_NAME}"  <<<<------------- this is null 
            }
          }
        }
        }
    type: JenkinsPipeline
  triggers:
    - bitbucket:
        secretReference:
          name: yoyo
      type: Bitbucket

--Thanks.

Upvotes: 1

Views: 939

Answers (1)

Wimateeka
Wimateeka

Reputation: 2706

According to this stack overflow question and some Jenkins documentation, you need to install the git plugin on your Jenkins instance. Then you will have the variable GIT_BRANCH and GIT_URL available to you via ${env.GIT_BRANCH} and ${env.GIT_URL}. Make sure your branch names do not have slashes in them, (ex: release/1.2.3) as this confuses a lot of key tools in Jenkins.

Alternatively as a last resort, in a Jenkins scripted pipeline you can set your own environment variables via parameter or defaults (like "main") if you know you won't change your branches often.

Upvotes: -1

Related Questions