为什么“ with”方法在詹金斯管道中不起作用?

时间:2019-08-19 17:15:19

标签: jenkins groovy jenkins-pipeline pipeline

我想使用Groovy中可用的“ with”方法来分配对象的多个值。 当我在本地Groovy代码中运行它时,没有问题,但是在Jenkins管道中运行相同的代码时,突然之间,没有分配任何对象属性。

我经历了Jenkins Pipelines中运行代码带来的差异,但是没有提及多重分配问题

我有基础课

import os

from getmac import get_mac_address

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    YELLOW = "\033[29m"
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

minr = int(input("Starting Ip: "))
maxr = int(input("Ending Ip: "))

ofip = []

while True:

    for num in range(minr, maxr + 1): #plus one is to include the last digit entered
        ip = "192.168.2." + str(num)

        from getmac import getmac

        exit_code = os.system("ping -n 1 -w 1 " + ip + " > nul") # Windows

        getmac.PORT = 44444  # Default: 55555

        if exit_code == 0:
            if ip in ofip:
                print(ip, bcolors.HEADER + "NEW " + bcolors.ENDC + bcolors.OKBLUE + get_mac_address(ip=ip, network_request=True) + bcolors.ENDC)
                ofip.remove(ip)
            print(ip, bcolors.OKGREEN + "ONLINE " + bcolors.ENDC + bcolors.OKBLUE + get_mac_address(ip=ip, network_request=True) + bcolors.ENDC)

        elif exit_code != 0:
            print(ip, bcolors.FAIL + "OFFLINE" + bcolors.ENDC)
            ofip.append(ip)

在本地脚本中的用法:

  Public Property Get sPATH() As String
    sPATH = ThisWorkbook.Path & "\"
  End Property


  Public Property Get sDate() As Date
     sDate = Date
  End Property



  Function GETIMPORTFILE(Index As Long) As String

        Dim fDate As Date

        fDate = Date

        Select Case Index
        Case 1: GETIMPORTFILE = "report.NewLeave." & Format(fDate, "yyyymmdd") & ".xlsx"
        Case 2: GETIMPORTFILE = "report.ReturnToWork." & Format(fDate, "yyyymmdd") & ".xlsx"
        Case 3: GETIMPORTFILE = "report.Denial." & Format(fDate, "yyyymmdd") & ".xlsx"
        Case 4: GETIMPORTFILE = "Open Leave.xlsx"
        Case 5: GETIMPORTFILE = "Employee List.csv"
        End Select

        End Function



            'Private function declaration for downloading the files from the Sharepoint site
            'Function works with Sub download_files()

            Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
            Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
            ByVal szURL As String, ByVal szFileName As String, _
            ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

            Dim Ret As Long

在詹金斯管道步骤中的用法

 Sub import_files()

                    Dim sDestSheet As String
                    Dim strSourceFile As String
                    Dim N As Long
                    Dim strLink As String
                    Dim strURL As String
                    Dim strPath As String

                    'Import the files from the Sharepoint site

                    strSourceFile = ThisWorkbook.Name

                    strLink = "https://sharepoint.teleperformanceusa.com/HumanResources/HRSSReporting/SitePages/Home.aspx"

                    On Error Resume Next

                    For N = 1 To 5 Step 1
strURL = strLink & GETIMPORTFILE(N)      'Access the url to Sharepoint and find the file
strPath = sPATH & GETIMPORTFILE(N)       'Save the file to the path of the workbook and save with the same name.


                        Ret = URLDownloadToFile(0, strURL, strPath, 0, 0) 'Return the file


                    Next


                End Sub

并调用Jenkinsfile

class Car {
    private String colour
    private int tankCapacity

    String getColour() { return this.colour }

    int getTankCapacity() { return this.tankCapacity }

    String toCustomString() {
        return "Car colour: ${this.colour}, tank capacity: ${this.tankCapacity}"
    }
}

本地运行的输出是:

class App {
    static void main(String[] args) {
        Car car = new Car()
        car.with {
            colour = "Black"
            tankCapacity = 50
        }

        println "Printing car details:"
        println car.toCustomString()
        println "Colour: ${car.getColour()}"
        println "Tank capacity: ${car.getTankCapacity()}"
        println car.toString()
        println GroovySystem.version
    }
}

Jenkins Pipeline运行的输出是:

def call(String arg_colour, int arg_tankCapacity) {
    Car car = new Car()
    car.with {
        colour = arg_colour
        tankCapacity = arg_tankCapacity
    }

    echo "Printing car details:"
    echo car.toCustomString()
    echo "Colour: ${car.getColour()}"
    echo "Tank capacity: ${car.getTankCapacity()}"
    echo car.toString()
    echo GroovySystem.version
}

实际上要使其在Jenkins中起作用的唯一方法是在任何字段名称之前使用“ it”,一点都没有意义,并破坏了“ with”方法的思想。

createCar("Black", 50)

有人遇到过吗?

0 个答案:

没有答案