簡體   English   中英

最小“在此 docker 圖像中運行此命令”jenkinsfile

[英]Minimal "run this command in this docker image" jenkinsfile

有一張名為 IMAGE_NAME 的 docker 圖片。

我想要一個 Jenkins 的工作到docker pull IMAGE_NAME然后docker run IMAGE_NAME /bin/bash -c '/usr/bin/my_startup arg1 arg2 arg3

我根據一些谷歌搜索示例嘗試了 Jenkinsfile 的一些變體,但它們不起作用,而且我顯然不了解階段、代理、 steps {sh '...'}命令運行的環境,或任何Jenkins 行話。

什么是最小的 jenkinsfile,它將提取 IMAGE_NAME 並運行其中包含 arguments 的命令?

最小的詹金斯文件

只要這不是“代碼高爾夫”風格的問題,以下管道將為您解決問題:

pipeline {
  agent any // modify as needed for an agent that supports container runtime

  stages {
    stage('Pull Image and Run Container') {
      // pulls image and then executes as container; note bonus example of arguments to container instantiation if needed
      docker.image('IMAGE_NAME').withRun('-e "env_var=env_value" -p 1234:5678') { container -> // container is lambda scope variable which you can reference as needed
        // execute startup
        sh(label: 'Startup', script: '/usr/bin/my_startup arg1 arg2 arg3')
      }
    }
  }
}

如果您有私有和/或自定義注冊表,則需要在 Jenkins 主服務器上設置withRegistry塊和注冊表憑據插件。

這有效:

docker.image('MY_IMAGE').inside {
  sh '/bin/my-command my-args'
}

顯然這是一個管道腳本,在 Jenkins 2.150.1 的 Groovy 沙箱中運行。

暫無
暫無

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

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