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