Skip to content

Instantly share code, notes, and snippets.

@apua
Last active May 6, 2023 02:16
Show Gist options
  • Save apua/f66ed142a72009be335e755bca32563d to your computer and use it in GitHub Desktop.
Save apua/f66ed142a72009be335e755bca32563d to your computer and use it in GitHub Desktop.
Jenkinsfile Template
def parallel_stages(string_n, script_path) {
def S = [:]
//for (int i=0; i<(string_n as int); i++) {
for ( i in 1..(string_n as int) ) {
S["$i"] = { sh script_path }
}
return parallel(S)
}
pipeline {
agent any
environment {
// Dynamic variable into env vars
STING_SEED = """${ sh (
returnStdout: true,
script: 'echo 123',
) }"""
}
stages {
stage('Check') {
steps {
echo "params := $params"
sh 'pwd; hostname; ls -latr; env'
//echo "$env" // NOTE: do not invoke env vars from Groovy
}
}
stage('Checkout Build Script') {
steps {
// NOTE: below makes here to be `fpga_regression_tw`:repo: itself
git(url: "[email protected]:sifive/fpga_regression_tw",
branch: "apuatest")
}
}
stage('Build') {
steps {
script {
parallel_stages(params.ITER, './script/apua_build.sh')
}
}
}
}
post {
cleanup { // like `script` clause
deleteDir()
}
}
}
stage('...') {
parallel {
stage('branch A') { steps { ... } }
stage('branch B') { steps { ... } }
}
}
stage('...') { steps { script {
def branches = [:]
for (... in ...) {
branches['branch A'] = { steps { ... } }
}
parallel branches
}}}
stage('...') { steps { script {
def branches = [:]
for (... in ...) {
branches."branch A" = build_closure(...)
}
parallel branches
}}}
def build_closure(...) { return { build() } }
pipeline {
agent any
stages {
stage('Fixed Parallel') { failFast true; parallel {
stage('para1') { steps { echo '' /* sh 'exit 1' //build 14 */ } }
stage('Para2') {
stages {
stage('p-2-1') { steps { echo '' } }
stage('p-2-2') { steps { echo '' } }
}
}
}}
// NOTE: seems like matrix is bad for building with parameters
stage('Matrix Trial') { matrix {
axes {
axis { name 'job'; values 1, 2 }
axis { name 'iter'; values 3, 4 /*params.iter*/ }
}
stages {
//stage("$job-$iteration") { steps { echo '' }}
// Ref: https://issues.jenkins.io/browse/JENKINS-61280?focusedCommentId=416027&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-416027
stage("variable named stage") { steps { script { stage("$job-$iter") { echo "$job-$iter" } }}}
}
}}
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
/* ****************************** */
// Ref: https://stackoverflow.com/a/66438907
// NOTE: should create data for all parallel stages
// Ref: `it` is built-in local variable used in closure
// http://docs.groovy-lang.org/docs/groovy-1.8.9/html/groovy-jdk/java/util/Collection.html#collectEntries(groovy.lang.Closure)
def parallel_tasks = [:] // a mapping type?
def generate_tasks(n,m) {
def T = [:]
for (int i=0; i<n; i++) { for (int j=0; j<m; j++) {
T["$i-$j"] = { echo "$i+$j" /* it's final static value */ }
}}
return T
}
pipeline {
// NOTE: any doesn't work here
//agent any
agent { label "verif-sys-validation-build-agent" }
// NOTE: on blueocean, updating parameters requires rerun with default values first,
// then Jenkins load the parameters definition.
parameters {
string(name: 'job', defaultValue: '2', description: 'Number of jobs') // be careful with typing
string(name: 'iter', defaultValue: '3', description: 'Number of iterations')
}
stages {
stage('Dynamic Parallel2') { steps { script {
parallel generate_tasks((params.job as int),(params.iter as int))
}}}
stage('Dynamic Parallel') { steps { script {
for (int i=0; i<(params.job as int); i++) { for (int j=0; j<(params.iter as int); j++) {
parallel_tasks["$i-$j"] = { echo "$i+$j" /* it's final static value */ }
}}
parallel parallel_tasks
}}}
}
}
/* ****************************** */
def parallel_tasks = [:] // a mapping type?
def generate_tasks(n,m) {
def T = [:]
for (int i=0; i<n; i++) { for (int j=0; j<m; j++) {
T["$i-$j"] = { echo "$i+$j" /* it's final static value */ }
}}
return T
}
def generate_value() { return 999; }
pipeline {
// NOTE: any doesn't work here
//agent any
agent { label "verif-sys-validation-build-agent" }
// NOTE: on blueocean, updating parameters requires rerun with default values first,
// then Jenkins load the parameters definition.
parameters {
string(name: 'job', defaultValue: '2', description: 'Number of jobs') // be careful with typing
string(name: 'iter', defaultValue: '3', description: 'Number of iterations')
}
environment {
DISABLE_AUTH = "$params.job"
MYVALUE = generate_value()
}
stages {
//stage('broken') { steps { r = sh 'echo' } }
stage('return') { steps {
echo "$env.DISABLE_AUTH-$env.MYVALUE"
script {
r = sh script:'echo 123', returnStdout:true
println r // works like `echo`
r = sh ( // if too long to be one line ...
script:'echo 123',
returnStdout:true
)
echo r
}
echo r /* 123 */
}}
stage('return conti.') { steps {
echo r /* 123 */ // I'm not sure the variable scope of `r`
echo env.r /* null */ // but it is not in env
}}
stage('Dynamic Parallel2') { steps { script {
parallel generate_tasks((params.job as int),(params.iter as int))
}}}
stage("sadfs") { steps {
echo params.getProperties().toString() // [class:class java.util.Collections$UnmodifiableMap, empty:false]
echo params.toString() // {iter=3, job=2}
echo "$params" // [iter:3, job:2]
}}
stage('Matrix Trial') { matrix {
axes {
axis { name 'job'; values 1, 2 }
axis { name 'iter'; values 3, 4 /*params.iter*/ }
}
stages {
//stage("$job-$iteration") { steps { echo '' }}
// Ref: https://issues.jenkins.io/browse/JENKINS-61280?focusedCommentId=416027&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-416027
stage("variable named stage (but not itself...)") { steps { script { stage("$job-$iter") { echo "$job-$iter" } }}}
}
}}
}
}
pipeline {
agent any
stages {
stage('Check') {
steps {
sh 'env'
sh 'hostname'
echo "params := $params"
sh 'pwd'
sh 'ls -latr'
}
}
stage('Yet Another') {
steps {
sh "touch somelog-$BUILD_ID"
}
}
}
post {
always {
archiveArtifacts "somelog-$BUILD_ID"
}
cleanup { // like `script` clause
echo 'cleanup'
sh 'pwd; ls -ltra'
deleteDir()
sh 'pwd; ls -ltra'
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment