簡體   English   中英

Jenkins 管道與 Dockerfile 配置

[英]Jenkins Pipeline with Dockerfile configuration

我正在努力為我的 Jenkins 管道獲得正確的配置。

它有效,但我無法弄清楚如何分開測試和構建階段。

要求:

  • Jenkins 具有獨立測試和構建階段的管道
  • 測試階段需要鉻(我目前使用節點高山圖像+添加鉻)
  • 構建階段正在構建一個 docker 鏡像,稍后發布(發布階段)

當前設置:

詹金斯文件:

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 階段(測試,構建)

I already tried using Jenkins agent docker and defining the image for the test stage inside the jenkins file, but I dont know how to add the missing chromium package there.

詹金斯文件:

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

我還考慮過使用已經包含鉻的 docker 圖像,但找不到任何官方圖像

非常感謝您的幫助/見解如何使這項工作。

您可以構建您的自定義映像(包括 Chromium 的安裝)並將其推送到注冊表,然后從該注冊表中提取它:

node {
    docker.withRegistry('https://my-registry') {

        docker.image('my-custom-image').inside {
            sh 'make test'
        }
    }
}

或者使用 Dockerfile 直接使用Dockerfile構建映像:

node {
    def testImage = docker.build("test-image", "./dockerfiles/test") 

    testImage.inside {
        sh 'make test'
    }
}

從位於Dockerfile的 Dockerfile 構建test-image ./dockerfiles/test/Dockerfile.

參考:使用 Docker 與流水線

所以一般來說,我會在 groovy 語法內而不是在 dockerfile 內執行 npm 運行命令。 所以你的代碼看起來像這樣:

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'
      }
    }
  }
}

我還建議您使用警告 ng jenkins 插件在 Jenkins 中收集結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM