TECH;Add stageWhen to Jenkins pipeline

This commit is contained in:
Roman Jaroš 2020-08-05 20:57:49 +02:00
parent 68284ce9b8
commit 0d87408c86

170
Jenkinsfile vendored
View file

@ -1,107 +1,85 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
node {
def appVersion
cleanWs();
cleanWs()
stage("checkout") {
stage('checkout') {
def repo = checkout scm
}
def isReleaseCommit = getIsReleaseCommit()
def isVersionCommit = getLastCommitMessage().startsWith('RELEASE;New')
def isReleaseCommit = BRANCH_NAME.startsWith('release/') && !isVersionCommit;
def isDeployBranch = BRANCH_NAME.startsWith('deploy/')
docker.image("romanjaros/ci-nodejs:12.15").inside() {
stage("install") {
sh "cp .npmrc ~/.npmrc"
sh "npm ci"
sh "npm run bootstrap"
}
docker.image('romanjaros/ci-nodejs:12.15').inside() {
stageWhen('install', !isDeployBranch, {
sh 'cp .npmrc ~/.npmrc'
sh 'npm ci'
sh 'npm run bootstrap'
})
stage("code check") {
sh "npm run lint"
sh "npm run test"
}
stageWhen('code check', !isDeployBranch, {
sh 'npm run lint'
sh 'npm run test'
})
stage("build") {
sh "npm run build:app"
}
stageWhen('build', !isDeployBranch, {
sh 'npm run build:app'
})
}
stage("run e2e env") {
sh "docker stop treejs-e2e || true"
sh "docker rm treejs-e2e || true"
sh "docker build -t romanjaros/treejs:latest -f ./docker/Dockerfile ."
stageWhen('run e2e env', !isDeployBranch, {
sh 'docker stop treejs-e2e || true'
sh 'docker rm treejs-e2e || true'
sh 'docker build -t romanjaros/treejs:latest -f ./docker/Dockerfile .'
sh "docker run --name treejs-e2e -d -p 9081:80 --entrypoint='nginx' romanjaros/treejs:latest -g 'daemon off;'"
}
})
docker.image("romanjaros/ci-nodejs:12.15").inside() {
stage("run e2e test") {
sh "npm run test:e2e"
}
docker.image('romanjaros/ci-nodejs:12.15').inside() {
stageWhen('run e2e test', !isDeployBranch, {
sh 'npm run test:e2e'
})
configGit()
stage("release") {
if (BRANCH_NAME.startsWith("release/") && !isReleaseCommit) {
def version = getBranchVersion()
sh "npx lerna version $version --no-git-tag-version --no-push --yes"
appVersion = getAppVersion();
sh "git checkout $BRANCH_NAME"
sh "git commit -am 'RELEASE;New version $appVersion'"
} else {
Utils.markStageSkippedForConditional("release")
}
}
stageWhen('release', isReleaseCommit, {
def version = getBranchVersion()
sh "npx lerna version $version --no-git-tag-version --no-push --yes"
appVersion = getAppVersion()
sh "git checkout $BRANCH_NAME"
sh "git commit -am 'RELEASE;New version $appVersion'"
})
stage("npm publish") {
if (BRANCH_NAME.startsWith("release/") && !isReleaseCommit) {
sh "cp .npmrc ~/.npmrc"
sh "npm run build:module"
sh "npx lerna run ci:publish --ignore documentation"
} else {
Utils.markStageSkippedForConditional("npm publish")
}
stageWhen('npm publish', isReleaseCommit, {
sh 'cp .npmrc ~/.npmrc'
sh 'npm run build:module'
sh 'npx lerna run ci:publish --ignore documentation'
})
}
stageWhen('publish app', isReleaseCommit, {
configDocker()
sh "docker build -t romanjaros/treejs:$appVersion -f ./docker/Dockerfile ."
sh 'docker push romanjaros/treejs:latest'
sh "docker push romanjaros/treejs:$appVersion"
})
if (isReleaseCommit) {
docker.image('romanjaros/ci-nodejs:12.15').inside() {
sh 'git push'
}
}
stage("publish app") {
if (BRANCH_NAME.startsWith("release/") && !isReleaseCommit) {
withCredentials([usernamePassword(
credentialsId: 'Docker Registry',
usernameVariable: 'USERNAME',
passwordVariable: 'PASSWORD'
)]) {
sh "echo $PASSWORD > .password"
sh "docker login -u $USERNAME --password-stdin < .password"
}
sh "docker build -t romanjaros/treejs:$appVersion -f ./docker/Dockerfile ."
sh "docker push romanjaros/treejs:latest"
sh "docker push romanjaros/treejs:$appVersion"
} else {
Utils.markStageSkippedForConditional("publish app")
stageWhen('deploy', isDeployBranch || BRANCH_NAME == 'master' || BRANCH_NAME == 'develop', {
removeDockerImage()
def version = 'latest'
if (BRANCH_NAME != 'master' && BRANCH_NAME != 'develop') {
version = getBranchVersion()
}
}
if (BRANCH_NAME.startsWith("release/") && !isReleaseCommit) {
docker.image("romanjaros/ci-nodejs:12.15").inside() {
sh "git push"
}
}
stage("deploy") {
if (BRANCH_NAME.startsWith("deploy/") || BRANCH_NAME == 'master' || BRANCH_NAME == 'develop') {
removeDockerImage()
def version = 'latest'
if (BRANCH_NAME != 'master' && BRANCH_NAME != 'develop') {
version = getBranchVersion()
}
sh "docker run --name treejs -d -p 81:80 --entrypoint='nginx' romanjaros/treejs:$version -g 'daemon off;'"
} else {
Utils.markStageSkippedForConditional("deploy")
}
}
sh "docker run --name treejs -d -p 81:80 --entrypoint='nginx' romanjaros/treejs:$version -g 'daemon off;'"
})
}
def configGit () {
@ -110,15 +88,24 @@ def configGit () {
usernameVariable: 'USERNAME',
passwordVariable: 'PASSWORD'
)]) {
sh "git config --global user.email 'ci@romanjaros.dev'";
sh "git config --global user.name 'CI'";
sh "git config --global user.email 'ci@romanjaros.dev'"
sh "git config --global user.name 'CI'"
sh "git config credential.username $USERNAME"
sh "git config credential.helper '!echo password=$PASSWORD; echo'"
}
}
}
def getIsReleaseCommit() {
return getLastCommitMessage().startsWith("RELEASE;New")
def configDocker () {
withCredentials(
[usernamePassword(
credentialsId: 'Docker Registry',
usernameVariable: 'USERNAME',
passwordVariable: 'PASSWORD'
)]
) {
sh "echo $PASSWORD > .password"
sh "docker login -u $USERNAME --password-stdin < .password"
}
}
def removeDockerImage() {
@ -127,15 +114,22 @@ def removeDockerImage() {
}
def getLastCommitMessage() {
sh "git log -1 --pretty=format:%s"
sh 'git log -1 --pretty=format:%s'
return sh(returnStdout: true, script: 'git log -1 --pretty=format:%s').trim()
}
def getBranchVersion() {
return BRANCH_NAME.tokenize("/").getAt(1)
return BRANCH_NAME.tokenize('/').getAt(1)
}
def getAppVersion() {
def packageJson = readJSON file: 'lerna.json'
return packageJson.version;
return packageJson.version
}
def stageWhen(name, execute, block) {
return stage(name, execute ? block : {
echo "skipped stage $name"
Utils.markStageSkippedForConditional(STAGE_NAME)
})
}