Jenkins Shared Libraries: เป็นไปได้ไหมที่จะส่งผ่านอาร์กิวเมนต์ไปยังเชลล์สคริปต์ที่นำเข้าเป็น 'libraryResource'

ฉันมีการตั้งค่าต่อไปนี้:

(ถอดออก) Jenkinsfile:

@Library('my-custom-library') _

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                printHello name: 'Jenkins'
            }
        }
    }
}

my-custom-library/resources/com/org/scripts/print-hello.sh:

#!/bin/bash

echo "Hello, $1"

my-custom-library/vars/printHello.groovy:

def call(Map parameters = [:]) {
    def printHelloScript = libraryResource 'com/org/scripts/print-hello.sh'
    def name = parameters.name
    //the following line gives me headaches
    sh(printHelloScript(name))
}

ฉันคาดหวัง Hello, Jenkins แต่มันส่งข้อยกเว้นต่อไปนี้:

groovy.lang.MissingMethodException: ไม่มีลายเซ็นของวิธีการ: java.lang.String.call() ใช้ได้กับประเภทอาร์กิวเมนต์: (java.lang.String) ค่า: [Jenkins]

วิธีแก้ไขที่เป็นไปได้: wait(), any(), wait(long), split(java.lang.String), take(int), Each(groovy.lang.Closure)

เป็นไปได้ไหมที่จะทำสิ่งที่อธิบายไว้ข้างต้นโดยไม่ต้องผสมโค้ด Groovy และ Bash


person ebu_sho    schedule 14.11.2018    source แหล่งที่มา


คำตอบ (1)


ใช่ ลองดูที่ withEnv

ตัวอย่างที่พวกเขาให้ดูเหมือน;

node {
  withEnv(['MYTOOL_HOME=/usr/local/mytool']) {
    sh '$MYTOOL_HOME/bin/start'
  }
}

ใช้ได้กับคุณมากขึ้น:

// resources/test.sh
echo "HI here we are - $PUPPY_DOH --"

// vars/test.groovy
def call() {
   withEnv(['PUPPY_DOH=bobby']) {
    sh(libraryResource('test.sh'))
  }
}

พิมพ์:

[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] libraryResource
[Pipeline] sh
+ echo HI here we are - bobby --
HI here we are - bobby --
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }

เมื่อใช้สิ่งนั้น คุณสามารถส่งผ่านโดยใช้ตัวแปรที่มีชื่อที่กำหนดขอบเขตได้ เช่น

def call(Map parameters = [:]) {
    def printHelloScript = libraryResource 'com/org/scripts/print-hello.sh'
    def name = parameters.name
    withEnv(['NAME=' + name]) { // This may not be 100% syntax here ;)
    sh(printHelloScript)
}

// print-hello.sh
echo "Hello, $name"
person chrisb    schedule 28.01.2019
comment
คุณสามารถใช้ withEnv(["NAME=${name}"]) (หมายเหตุ qoutes คู่) คุณอาจต้องการหลีกเลี่ยงเนื้อหาตัวแปรก่อน - person iliis; 04.11.2020