# jenkins declarative pipeline
declarative pipeline
0. gitlab webhook get wrong commit
1. env/params/local variable
2. variable scope
3. git checkout and git push
4. literal template/ java string
5. groovy vs bash
6. mkdir in pipeline
7. jib command example
8. parallel block
9. each bash as another brand new workspace
---
### 0. webhook trigger by test button only send the lastest commit id

those test only send the default branch lastest commit id
---
### 1. env/params/local variable
${var} => read local var
${params.var} => read from params
${env.var} => read enviroment var
```
environment {
GIT_AUTH = credentials('marketing-cloud-gitlab_ops_cred')
}
parameters {
string(name: 'JAVA_CHECKOUT_COMMIT', defaultValue: "${env.gitlabAfter}", description: 'commitId ${env.gitlabAfter} for webhook carried info, or use refs/heads/main get the main branch newest commit id')
string(name: 'OPS_MAIN_BRANCH', defaultValue: "main", description: 'ops main branch')
string(name: 'OPS_UAT_BRANCH', defaultValue: "deployment/uat", description: 'OPS_BRANCH')
string(name: 'IMAGE_PREFIX', defaultValue: "", description: "test/ for test purpose, left this input empty for uat/prod run replace ")
}
```
### 2. variable scope
2a. each bash is new environment, its env variables are copied from the outer runtime.
2b. local varible declare in script block
w/ def : scope within this script block
w/o def: global var
2c. order of searching variable: local(def)-> global -> params -> env
```
script{
def script_block_scope_var="value"
global_block_scope_var="val"
}
```
### 3. git checkout and git push
clone,pull:
ps. name can replace with git commit id
```
checkout([$class: 'GitSCM',
branches: [[
name:'refs/heads/main']],
userRemoteConfigs: [[
url: 'https://git.systex.com/2021_10B17C/ob17b20110002/code/marketing-cloud-ops.git',
credentialsId: 'gitlab-systex-OB17B20110002-code-systexcloud-uat-eks-jenkins',
]]])
```
push:
```
environment {
GIT_AUTH = credentials('gitlab_ops_cred')
}
steps{
/*
project_{project_id}_bot
project{project_id}_bot@noreply.{Gitlab.config.gitlab.host}
git config --global user.email "project3028_bot@noreply.git.systex.com"
git config --global user.name "project_3028_bot"
*/
sh'''
git config --local credential.helper "!f() { echo username=${env.GIT_AUTH_USR}; echo password=${env.GIT_AUTH_PSW}; }; f"
git config --global user.email "project3028_bot@noreply.git.systex.com"
git config --global user.name "project_3028_bot"
'''
}
```
### 4. literal template(double quote)/ java string(single quote)
double quote script for groovy templatable String(replace var by groovy)
```
VAR="VALUE"
"$VAR" will replace by groovy variable => VALUE
"\$VAR" escape template string replacement=> $VAR for bash
'$VAR' pure java string => $VAR for bash
```
### 5. groovy vs bash
communicate with bash,echo for standard output to params,
trim for unpredictable blank string
```
UPDATED_JAVA_SCHED_TAG=sh(returnStdout:true,script:"""
#!bin/bash
UPDATED_JAVA_SCHED_TAG=\$(cat ./marketing-cloud-sched-api-appsrv/target/jib-image.json | yq -r '.tags[0]')
echo \$UPDATED_JAVA_SCHED_TAG
""").trim()
```
### 6. bash mkdir command cant work,
for create new folder (ex. for multi_repo checkout)
```
dir('folder_name'){
}
```
### 7. jib::maven
```
AWS_PROFILE=ecr mvn -B package jib:build -T 1.5C -DskipTests -Dmaven.test.skip \
-Djib.allowInsecureRegistries=false \
-Djib.to.credHelper=ecr-login \
-Djib.image.repo=$ECR_REPO \
-Djib.image.name=$imagename\
-f ./marketing-cloud-front-web-api-appsrv-lib/pom.xml ;
```
### jib::gradle
```
AWS_PROFILE=ecr gradle jib \
-Djib.to.credHelper=ecr-login \
-PjibImageRepo=866485381963.dkr.ecr.ap-northeast-1.amazonaws.com/jcic/
```
### 8. parallel failfast: 平行處理多工, 設定其中一件fail就中斷所有平行工作
```
pipeline {
environment {
GIT_AUTH = credentials('marketing-cloud-gitlab_ops_cred')
}
options {
parallelsAlwaysFailFast() //all parallel fail fast
}
agent any
stage('Parallel In Sequential') {
parallel {
stage('In Parallel 1') {
steps {
echo "In Parallel 1"
}
}
stage('In Parallel 2') {
steps {
echo "In Parallel 2"
}
}
}
}
}
```
p.s. blank block are forbidden
```
ex. environment{
}
```
###### tags: `jenkins` `declarative pipeline`