Openshift retrieve branch name in jenkinsfile

天大地大妈咪最大 提交于 2020-06-28 14:24:07

问题


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.


回答1:


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 "master") if you know you won't change your branches often.



来源:https://stackoverflow.com/questions/50643501/openshift-retrieve-branch-name-in-jenkinsfile

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