首页 > 解决方案 > 使用带有 Openshift/jenkins-client-plugin 的 Pipeline 从 Dockerfile 构建映像

问题描述

我正在尝试将 OpenShift Jenkins 管道设置为:

  1. 从 git 获取源代码。源包括一个Dockerfile
  2. 运行测试
  3. 使用 Dockerfile 构建镜像
  4. 将图像推送到 Imagestream

我创建了一个BuildConfigwithDocker策略,它实际上可以在没有管道的情况下工作。问题是我不知道如何运行测试

如果我尝试从管道启动构建,它不会触发构建。

我在正确的道路上吗?对于这样的项目,最好的方法是什么?我应该继续依赖 OpenShift 特定工具吗?还是迁移到普通的 Kubernetes?

我正在为管道使用https://github.com/openshift/jenkins-client-plugin

这是我的构建配置:

kind: "BuildConfig"
apiVersion: "v1"
metadata:
  name: "front-end-build"
spec:
  runPolicy: "Serial"
  nodeSelector:
    hostname: "vhkdld518"
  source:
    git:
      uri: "https://kraporta@bitbucket/scm/~kraporta/test-kube.git"
  strategy:
    dockerStrategy:
      from:
        kind: "ImageStreamTag"
        name: "nginx:alpine"
      dockerfilePath: Dockerfile
  output:
    to:
      kind: "ImageStreamTag"
      name: "front-end:latest"

这是管道:

node {
    stage('build') {
        openshift.withCluster() {
            openshift.withProject() {
              echo "Using project: ${openshift.project()}"
              def builds = openshift.selector("bc", "front-end-build").related('builds')
              builds.describe()
              timeout(5) { 
                builds.untilEach(1) {
                    it.describe()
                    echo "Inside loop: ${it}"
                    return (it.object().status.phase == "Complete")
                }
              }
            }
        }
    }
}

非常感谢!

标签: openshift

解决方案


我知道这是一个老问题,但我会保留它作为参考。

问题: 缺少步骤,您还没有开始构建过程,所以没有构建!

解决方案: 我们需要在获取构建之前开始构建过程,这里:

def builds = openshift.selector("bc", "front-end-build").related('builds')

成为(有很多方法可以完成):

def buildConfig = openshift.selector("bc", "front-end-build")
openshift.startBuild("front-end-build") # we started the build process
def builds = buildConfig.related('builds')

检查:https ://github.com/openshift/jenkins-client-plugin#watching-and-waiting-of-course


推荐阅读