Last active
May 20, 2021 22:55
-
-
Save mosesliao/497e86fdb9e0b8bdc90c0461f718361b to your computer and use it in GitHub Desktop.
Example of use of shared libraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library identifier: 'jenkins-shared-libraries@master', retriever: modernSCM( | |
[$class: 'GitSCMSource', | |
remote: 'ssh://[email protected]/smar/jenkins-shared-libraries.git', | |
credentialsId: 'jenkins-bitbucket-ssh-private-key']) | |
pipeline { | |
agent any | |
environment { | |
TEAMS_WEBHOOK_URL = credentials('digital-apac-webhook-url-for-ms-teams') | |
EMAIL_ADDRESS = '$fullpayload_actor_emailAddress' | |
BRANCH_NAME = '$fullpayload_changes_0_ref_displayId' | |
REF_TYPE = '$fullpayload_changes_0_ref_type' //BRANCH / TAG | |
CHANGE_TYPE = '$fullpayload_changes_0_type' // UPDATE / ADD /DELETE | |
WORKSPACE = pwd() | |
} | |
parameters{ | |
string(name: 'fullpayload', defaultValue: '', description: '') | |
string(name: 'REF_TYPE', defaultValue: '', description: '') | |
} | |
triggers { | |
GenericTrigger( | |
genericVariables: [ | |
[ | |
// will be used if changes are pushed to a branch | |
key: 'fullpayload', | |
value: '$' | |
], | |
[ | |
// will be used if changes are pushed to a branch | |
key: 'ref', | |
value: '$.changes[0].ref.type' | |
], | |
[ | |
// will be used if changes are pushed to a branch | |
key: 'action', | |
value: '$.changes[0].type' | |
] | |
], | |
token: 'whatever', | |
printContributedVariables: true, | |
printPostContent: true, | |
regexpFilterText: '$ref $action', | |
regexpFilterExpression: 'BRANCH UPDATE' //This restricts the job only to Commit(Push) of any branch | |
) | |
} | |
stages { | |
stage('Git Clone') { | |
steps { | |
echo 'cloning master server' | |
script { | |
sourceCodeCheckout( | |
ref, | |
fullpayload_changes_0_ref_displayId, | |
fullpayload_repository_links_clone_0_href, | |
'jenkins-bitbucket-ssh-private-key' | |
) | |
} | |
} | |
} | |
stage('docker build') { | |
steps { | |
dir('smart-home-master-server/node-for-smart-home') { | |
withCredentials( | |
[ | |
sshUserPrivateKey( | |
credentialsId: 'jenkins-npm-install-key', | |
keyFileVariable: 'keyfile' | |
) | |
] | |
){ | |
echo 'Build Master Server Image' | |
sh "touch id_rsa" | |
sh "cat $keyfile > id_rsa" | |
script { | |
try { | |
msTeamsMessenger('Building Docker Image','STARTED', 'ADD8E6') | |
masterServer = docker.build("$dockerImageName") | |
msTeamsMessenger('Docker Image Done','SUCCESS!!!', '96ceb4') | |
echo "image hash is ${masterServer.id}" | |
} catch(Exception e) { | |
echo e.toString() | |
env.ERROR_MESSAGE = e.toString() | |
currentBuild.result = 'FAILURE' | |
} | |
} | |
} | |
} | |
} | |
} | |
stage('docker push SG') { | |
when { | |
expression { | |
("${ref}" == "BRANCH" && "${fullpayload_changes_0_ref_displayId}" == 'master') | |
} | |
} | |
steps { | |
script { | |
withAWS(region: 'ap-southeast-1', credentials: 'awsId') { | |
internationalOutput = cfnDescribe(stack:"${env.stackName}") | |
sh "${ecrLogin()}" | |
msTeamsMessenger('Pushing image to Singapore','STARTED', 'ADD8E6') | |
sh "docker tag tas/master-server:${env.BUILD_NUMBER} ${internationalOutput.MasterRepositoryUri.toString()}" | |
sh "docker push ${internationalOutput.MasterRepositoryUri.toString()}" | |
msTeamsMessenger('Singapore image pushed','SUCCESS!!!', '96ceb4') | |
} | |
} | |
} | |
} | |
stage('SG codedeploy') { | |
when { | |
expression { | |
("${ref}" == "BRANCH" && "${fullpayload_changes_0_ref_displayId}" == 'master') | |
} | |
} | |
parallel { | |
stage('web') { | |
steps { | |
ecsCodeDeploy( | |
getTaskDefNoRevision(internationalOutput.WebTaskDef.toString()), | |
"web", | |
8000, | |
env.internationalCodeDeployApplication, | |
'tas-password-rotation', | |
'ap-southeast-1', | |
'awsId', | |
internationalOutput.WebEcsArn.toString() | |
) | |
} | |
} | |
stage('app') { | |
steps { | |
ecsCodeDeploy( | |
getTaskDefNoRevision(internationalOutput.AppTaskDef.toString()), | |
"app", | |
7456, | |
env.internationalCodeDeployApplication, | |
'tas-password-rotation', | |
'ap-southeast-1', | |
'awsId', | |
internationalOutput.AppEcsArn.toString() | |
) | |
} | |
} | |
} | |
} // end of ECS codedeploy | |
stage('docker push CN') { | |
when { | |
expression { | |
("${ref}" == "BRANCH" && "${fullpayload_changes_0_ref_displayId}" == 'master') | |
} | |
} | |
steps { | |
script { | |
withAWS(region: 'cn-north-1', credentials: 'awsChinaId') { | |
chinaOutput = cfnDescribe(stack:"${env.stackName}") | |
sh "${ecrLogin()}" | |
msTeamsMessenger('Pushing image to China','STARTED', 'ADD8E6') | |
sh "docker tag tas/master-server:${env.BUILD_NUMBER} ${chinaOutput.MasterRepositoryUri.toString()}" | |
sh "docker push ${chinaOutput.MasterRepositoryUri.toString()}" | |
msTeamsMessenger('China image pushed','SUCCESS!!!', '96ceb4') | |
} | |
} | |
} | |
} | |
stage('CN codedeploy') { | |
when { | |
expression { | |
("${ref}" == "BRANCH" && "${fullpayload_changes_0_ref_displayId}" == 'master') | |
} | |
} | |
parallel { | |
stage('web') { | |
steps { | |
ecsCodeDeploy( | |
getTaskDefNoRevision(chinaOutput.WebTaskDef.toString()), | |
"web", | |
8000, | |
env.chinaCodeDeployApplication, | |
'tas-code', | |
'cn-north-1', | |
'awsChinaId', | |
chinaOutput.WebEcsArn.toString() | |
) | |
} | |
} | |
stage('app') { | |
steps { | |
ecsCodeDeploy( | |
getTaskDefNoRevision(chinaOutput.AppTaskDef.toString()), | |
"app", | |
7456, | |
env.chinaCodeDeployApplication, | |
'tas-code', | |
'cn-north-1', | |
'awsChinaId', | |
chinaOutput.AppEcsArn.toString() | |
) | |
} | |
} | |
} | |
} // end of ECS codedeploy | |
} | |
post { | |
failure { | |
msTeamsMessenger('Master Server Build failed','FAILED', 'FF6347', [ | |
["name": "error message","template": env.ERROR_MESSAGE] | |
]) | |
} | |
success { | |
msTeamsMessenger('Master Server Build success','SUCCESS!!!', '96ceb4', [ | |
["name": "Version", "template": appVersion], | |
["name": "Commit Hash", "template": fullpayload_changes_0_toHash] | |
]) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment