【问题标题】:Jenkins Pipeline with Dockerfile configuration带有 Dockerfile 配置的 Jenkins 流水线
【发布时间】:2022-01-12 12:38:53
【问题描述】:

我正在努力为我的 Jenkins 流水线获得正确的配置。

它可以工作,但我不知道如何分开测试和构建阶段。

要求:

  • 具有独立测试和构建阶段的 Jenkins 流水线
  • 测试阶段需要铬(我目前使用节点高山图像+添加铬)
  • Build 阶段正在构建 Docker 映像,稍后发布(发布阶段)

当前设置:

Jenkins 文件:

pipeline {

environment {
    ...
}
options {
    ...
}
stages {
    stage('Restore') {
       ...
    }
    stage('Lint') {
       ...
    }

    stage('Build & Test DEV') {
        steps {
            script {
                dockerImage = docker.build(...)
            }
        }
    }

    stage('Publish DEV') {
        steps {
            script {
                docker.withRegistry(...) {
                    dockerImage.push()
                }
            }

        }
    }

Dockerfile:

FROM node:12.16.1-alpine AS build

#add chromium for unit tests
RUN apk add chromium

...

ENV CHROME_BIN=/usr/bin/chromium-browser

...

# works but runs both tests & build in the same jenkins stage
RUN npm run test-ci

RUN npm run build

...

这可行,但正如您所见,“构建和测试 DEV”是一个阶段,

我想要 2 个单独的 jenkins 阶段(测试、构建)

我已经尝试使用 Jenkins 代理 docker 并在 jenkins 文件中为测试阶段定义图像,但我不知道如何在其中添加缺少的 chromium 包。

Jenkins 文件:

pipeline {
agent {
    docker {
        image 'node:12.16.1-alpine'
        //add chromium package here?
        //set Chrome_bin env?
    }
}

我也想过使用已经包含铬的docker镜像,但找不到任何官方镜像

非常感谢您对如何完成这项工作的帮助/见解。

【问题讨论】:

    标签: docker jenkins dockerfile jenkins-pipeline pipeline


    【解决方案1】:

    您可以构建您的自定义映像(包括 Chromium 的安装)并将其推送到注册表,然后从该注册表中提取它:

    node {
        docker.withRegistry('https://my-registry') {
    
            docker.image('my-custom-image').inside {
                sh 'make test'
            }
        }
    }
    

    或者使用Dockerfile 直接使用 Jenkins 构建映像:

    node {
        def testImage = docker.build("test-image", "./dockerfiles/test") 
    
        testImage.inside {
            sh 'make test'
        }
    }
    

    从位于./dockerfiles/test/Dockerfile.Dockerfile 构建test-image

    参考:Using Docker with Pipeline

    【讨论】:

      【解决方案2】:

      所以通常我会在 groovy 语法中而不是在 dockerfile 中执行 npm run 命令。所以你的代码看起来像这样:

      pipeline {
        agent {
          docker {
            image 'node:12.16.1-alpine'
            args  '-u root:root' // better would be to use sudo, but this should work
          }
        }
        stages {
          stage('Preparation') {
            steps {
              sh 'apk add chromium'
            }
          }
          stage('build') {
            steps {
              sh 'npm run build'
            }
          }
          stage('test') {
            steps {
              sh 'npm run test'
            }
          }
        }
      }
      

      我还建议您使用 warnings ng jenkins plugin 在 Jenkins 中收集结果

      【讨论】:

      • 我试过了,但我得到了 sh: /bin/bash: not found 错误,不知道为什么
      • 如果我恢复更改,-u root:root 现在会导致权限被拒绝:(
      猜你喜欢
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 2022-09-26
      • 2021-10-07
      • 1970-01-01
      相关资源
      最近更新 更多