jenkins管道-如何使用powershell命令在声明性管道步骤中构建docker映像

时间:2020-03-14 12:14:20

标签: powershell docker jenkins-pipeline

我正在尝试通过jenkins声明性管道构建一个asp.net docker映像,该流正在jenkins从属机器Windows Server2019上运行,因此它需要使用powershell运行docker命令,powershell版本为5.1我在命令中写了docker build parameter precisionlt,运行成功, 命令是:

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

,jenkins控制台输出为

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构建将失败,并且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] }

我也尝试在jenkins管道中使用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上运行时似乎存在一些错误。

1 个答案:

答案 0 :(得分:0)

作业参数可作为 PowerShell 的环境变量使用 - 在 PowerShell 脚本中为每个参数添加 void initTimer (void); void delay (unsigned long milli); unsigned int counter; unsigned int zero = 0b0000000000000000; unsigned int one = 0b0000000001000000; int main (void) { initTimer(); TRISB = 0; LATB = 0; for (counter = 0; counter < 10; counter++) { LATB = zero; delay (SHORT_DELAY); LATB = one; delay (SHORT_DELAY); } while (1); } ,如下所示:

env: