如何使用Jenkins和openshift定义BuildConfig对象

时间:2018-09-14 19:01:10

标签: jenkins openshift

我正在使用Jenkins和OpenShift创建构建并触发部署。以下是我的YAML文件:

apiVersion: v1
kind: List
metadata: {}
items:
- apiVersion: v1
  kind: BuildConfig
  metadata:
    name: petclinic-pipeline
  spec:
    source:
      git:
       uri: <<git url>>
      type: Git
    strategy:
      type: JenkinsPipeline
      jenkinsPipelineStrategy:
        jenkinsfilePath: Jenkinsfile
    triggers:
    - generic:
        secret: FiArdDBH
      type: Generic

Jenkins配置如下:

node {
  stage('Source Checkout') {
    git url: "git url"
  }

   stage('Build') {
      git url: "git url"
      sh "/var/lib/jenkins/apache-maven-3.5.4/bin/mvn clean package -Dorg.jenkinsci.plugins.durabletask.BourneShellScript.HEARTBEAT_CHECK_INTERVAL=300 -DskipTests=true"
      stash name:"jar", includes:"target/a.jar"
    }

  stage('Build Image') {
    unstash name:"jar"
    sh "oc start-build petclinic-pipeline --from-file=target/a.jar --follow"
  }
 } 

现在,如果应用此方法,则会出现以下错误:

$ oc start-build petclinic-pipeline --from-file=target/a.jar --follow
Uploading file "target/a.jar" as binary input for the build ... The
Build "petclinic-pipeline-20" is invalid: spec.source.git: Invalid
value: "": must be set when using Jenkins Pipeline strategy with
Jenkinsfile from a git repo

我的期望是应该构建映像,并且我无法理解问题出在哪里。是不是像使用Jenkins策略时在YAML文件中不使用spec源git?

1 个答案:

答案 0 :(得分:2)

我个人认为,最好使用OpenShift Pipeline Jebkins Plugin而不是为oc start-build执行命令。

例如,使用OpenShift Pipeline Jenkins插件进行简单的构建和部署描述。有关更多详细信息,请参见here

apiVersion: v1
kind: BuildConfig
metadata:
  labels:
    name: your-pipeline
  name: your-pipeline
spec:
  runPolicy: Serial
  strategy:
    jenkinsPipelineStrategy:
      jenkinsfile: |-
        node(''){
          stage 'Build using your-yourconfig'
              openshiftBuild(namespace: 'your-project', bldCfg: 'your-buildconfig', showBuildLogs: 'true')
          stage 'Deployment using your-deploymentconfig'
              openshiftDeploy(namespace: 'your-project', depCfg: 'your-deploymentconfig')

        }
    type: JenkinsPipeline
  triggers:
  - github:
      secret: gitsecret
    type: GitHub
  - generic:
      secret: genericsecret
    type: Generic

希望对您有帮助。