簡體   English   中英

jenkins 管道 - 如何在聲明性管道步驟中使用 powershell 命令構建 docker 鏡像

[英]jenkins pipeline - how to build a docker image in a declarative pipeline steps use powershell command

我正在嘗試通過 jenkins 聲明性管道構建一個 asp.net docker 鏡像,流程在 jenkins slave 機器 windows server2019 上運行,所以它需要使用 powershell 來運行 docker 命令,powershell 版本是 5.1,當我寫 docker 時在命令中構建參數exactlt,運行成功,命令為:

 stage('Build Container') {
            steps {
              echo "Build Container ${IMAGE_NAME}:${TAG} ${DOCKERFILE_PATH}"
              script {
                powershell "docker build -t windowsservercoreiisremote-ltsc2019:test . --force-rm"
              }           
            }
        }

詹金斯控制台輸出是

Build Container windowsservercoreiisremote-ltsc2019:ef16889
 C:\Users\Administrator\Documents\jenkins-test-slave\orkspace\windowsservercoreIISRemote-ltsc2019
[Pipeline] script
[Pipeline] {
[Pipeline] powershell
Sending build context to Docker daemon  64.51kB

Step 1/4 : FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019
 ---> a82e61390cbd
Step 2/4 : LABEL owner="SkIT" version="1.0" description="����垢蝞∠�IS憟辣��indowsservercore�����"
 ---> Using cache
 ---> 83e11b15dac2
Step 3/4 : RUN powershell Install-WindowsFeature Web-Mgmt-Service; New-ItemProperty -Path HKLM:\software\microsoft\WebManagement\Server -Name EnableRemoteManagement -Value 1 -Force; Set-Service -Name wmsvc -StartupType automatic; net user xxxx xxxx /ADD; net localgroup administrators iisadmin /add;
 ---> Using cache
 ---> db03397c278d
Step 4/4 : EXPOSE 80/tcp 443/tcp 8172/tcp
 ---> Using cache
 ---> 32d9d4a005b5
Successfully built 32d9d4a005b5
Successfully tagged windowsservercoreiisremote-ltsc2019:test
[Pipeline] }
[Pipeline] // script
[Pipeline] }

但是,當我在 powershell 命令中使用 jenkins 變量時,例如:

def IMAGE_NAME      = 'windowsservercoreiisremote-ltsc2019'
def TAG             = ''
stage('Set GITHASH & Tag') {
            steps {
              script {  
                echo 'Set GITHASH & Tag'
                GITHASH = powershell(returnStdout: true, script: 'git rev-parse --short HEAD')
                TAG = powershell(returnStdout: true, script: 'git rev-parse --short HEAD')
              }
            }
        }
        stage('Build Container') {
            steps {
              echo "Build Container ${IMAGE_NAME}:${TAG} ${DOCKERFILE_PATH}"
              script {
                powershell "docker build -t ${IMAGE_NAME}:${TAG} . --force-rm"
              }           
            }
        }

powersehll docker build 將失敗,jenkins 控制台輸出為

[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build Container)
[Pipeline] echo
Build Container windowsservercoreiisremote-ltsc2019:ef16889
 C:\Users\Administrator\Documents\jenkins-test-slave\orkspace\windowsservercoreIISRemote-ltsc2019
[Pipeline] script
[Pipeline] {
[Pipeline] powershell
powershell.exe : "docker build" requires exactly 1 argument.
雿 C:\Users\Administrator\Documents\jenkins-test-slave\workspace\windowsservercoreIISRemote-ltsc2019@tmp\durable-e5acf825\powershellWrapper.ps1:3 摮��:1
+ & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: ("docker build" ...tly 1 argument.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

See 'docker build --help'.

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

. : The term '--force-rm' is not recognized as the name of a cmdlet, function, script file, or operable program. Check 

the spelling of the name, or if a path was included, verify that the path is correct and try again.

At C:\Users\Administrator\Documents\jenkins-test-slave\workspace\windowsservercoreIISRemote-ltsc2019@tmp\durable-e5acf8

25\powershellScript.ps1:2 char:4

+  . --force-rm

+    ~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (--force-rm:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException



[Pipeline] }
[Pipeline] // script
[Pipeline] }

我也在詹金斯管道中嘗試 docker build 命令

stage('Build Container') {
            steps {
              echo "Build Container ${IMAGE_NAME}:${TAG} ${DOCKERFILE_PATH}"
              script {
                def buildImage = docker.build("${IMAGE_NAME}:${TAG}","--force-rm")
              }           
            }
        }

但它也不起作用,在powershell上運行時似乎有一些錯誤。

作業參數可作為 PowerShell 的環境變量使用 - 在 PowerShell 腳本中為每個參數添加env:如下所示:

powershell "docker build -t ${env:IMAGE_NAME}:${env:TAG} . --force-rm"

暫無
暫無

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

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