OpenShift在jenkinsfile中检索分支名称 [英] Openshift retrieve branch name in jenkinsfile

查看:260
本文介绍了OpenShift在jenkinsfile中检索分支名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在指向Openshift的Bitbucket服务器上配置了Webhook. 我想从内联jenkinsfile中的webhook有效负载中获取GIT repo url,git branch等,但我不知道如何检索它们. (不过Webhook触发器会生成).

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).

有可能吗?

这是我的BuildConfig

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

-谢谢.

推荐答案

根据 git插件在您的Jenkins实例上.然后,您将通过${env.GIT_BRANCH}${env.GIT_URL}使用变量GIT_BRANCHGIT_URL. 请确保您的分支名称中没有斜杠(例如1.2.3版),因为这会混淆詹金斯中的许多关键工具.

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.

或者作为最后的选择,在 Jenkins脚本化管道如果您知道自己不会经常更改分支,则可以通过参数或默认值(例如"master")来设置自己的环境变量.

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.

这篇关于OpenShift在jenkinsfile中检索分支名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆