Ở đây chúng ta có 1 single static method hay func.
package com.acceleratedskillup;
class ValidatorUtils {
static def validateInput(script) {
script.echo "in static validateInput()"
}
}
Chúng ta tạo 1 pipeline scipt
@Library("pipeline-shared-lib")
import static com.acceleratedskillup.ValidatorUtils.*;
pipeline{
agent any
stages{
stage("Build"){
steps{
script{
validateInput(this)
}
}
}
}
}
theo như suy luận thì bạn thấy validateInput(this)
có chứ this đúng không?
nghĩ là block script{}
sẽ được đưa vào func validateInput
như 1 parameter.
ở file groovy thì nó sử dụng script.echo "in static validateInput()"
nên là groovy nó sử dụng được script.echo
của block script{}
và script.echo
thì mới in ra màn hình log được.
save và build để xem kết quả.

Còn 1 cách khác.
import static com.acceleratedskillup.ValidatorUtils.*;
pipeline{
agent any
stages{
stage("Build"){
steps{
script{
library ("pipeline-shared-lib").com.acceleratedskillup.ValidatorUtils.validateInput(this)
}
}
}
}
}

vì github trên ngay con master có credentials github
Nhưng nếu agent là any khi đó thì jenkins master kêu con jenkins-container git code về nhưng con jenkins-container lại ko có credentials để git pull nên là lỗi

Mình nghĩ cách này chỉ với được nếu jenkins là 1 con được cài và tạo thêm credentials trên jenkins agent đó