Add comments to pr for failures

This commit is contained in:
Jamie Curnow 2023-03-08 16:12:57 +10:00
parent 292a209195
commit 35cc486fd7
No known key found for this signature in database
GPG Key ID: FFBB624C43388E9E
4 changed files with 77 additions and 46 deletions

120
Jenkinsfile vendored
View File

@ -1,3 +1,19 @@
import groovy.transform.Field
@Field
def shOutput = ""
def buildxPushTags = ""
def getVersion() {
ver = sh(script: 'cat .version', returnStdout: true)
return ver.trim()
}
def getCommit() {
ver = sh(script: 'git log -n 1 --format=%h', returnStdout: true)
return ver.trim()
}
pipeline { pipeline {
agent { agent {
label 'docker-multiarch' label 'docker-multiarch'
@ -30,7 +46,7 @@ pipeline {
} }
steps { steps {
script { script {
env.BUILDX_PUSH_TAGS = "-t docker.io/${DOCKER_ORG}/${IMAGE}:${BUILD_VERSION} -t docker.io/${DOCKER_ORG}/${IMAGE}:${MAJOR_VERSION} -t docker.io/${DOCKER_ORG}/${IMAGE}:latest" buildxPushTags = "-t docker.io/${DOCKER_ORG}/${IMAGE}:${BUILD_VERSION} -t docker.io/${DOCKER_ORG}/${IMAGE}:${MAJOR_VERSION} -t docker.io/${DOCKER_ORG}/${IMAGE}:latest"
echo 'Building on Master is disabled!' echo 'Building on Master is disabled!'
sh 'exit 1' sh 'exit 1'
} }
@ -45,8 +61,8 @@ pipeline {
steps { steps {
script { script {
// Defaults to the Branch name, which is applies to all branches AND pr's // Defaults to the Branch name, which is applies to all branches AND pr's
// env.BUILDX_PUSH_TAGS = "-t docker.io/jc21/${IMAGE}:github-${BRANCH_LOWER}" // buildxPushTags = "-t docker.io/jc21/${IMAGE}:github-${BRANCH_LOWER}"
env.BUILDX_PUSH_TAGS = "-t docker.io/${DOCKER_ORG}/${IMAGE}:v3" buildxPushTags = "-t docker.io/${DOCKER_ORG}/${IMAGE}:v3"
} }
} }
} }
@ -54,24 +70,58 @@ pipeline {
} }
stage('Frontend') { stage('Frontend') {
steps { steps {
sh './scripts/ci/build-frontend' script {
} def shStatusCode = sh(label: 'build-frontend', returnStatus: true, script: '''
/* set -e
post { ./scripts/ci/build-frontend > ${WORKSPACE}/tmp-sh-build 2>&1
always { ''')
junit 'frontend/eslint.xml' shOutput = readFile "${env.WORKSPACE}/tmp-sh-build"
junit 'frontend/junit.xml' if (shStatusCode != 0) {
error "${shOutput}"
}
}
}
post {
always {
sh 'rm -f ${WORKSPACE}/tmp-sh-build'
// junit 'frontend/eslint.xml'
// junit 'frontend/junit.xml'
}
failure {
npmGithubPrComment("CI Error:\n\n```\n${shOutput}\n```", true)
}
success {
shOutput = ""
} }
} }
*/
} }
stage('Backend') { stage('Backend') {
steps { steps {
withCredentials([string(credentialsId: 'npm-sentry-dsn', variable: 'SENTRY_DSN')]) { withCredentials([string(credentialsId: 'npm-sentry-dsn', variable: 'SENTRY_DSN')]) {
withCredentials([usernamePassword(credentialsId: 'oss-index-token', passwordVariable: 'NANCY_TOKEN', usernameVariable: 'NANCY_USER')]) { withCredentials([usernamePassword(credentialsId: 'oss-index-token', passwordVariable: 'NANCY_TOKEN', usernameVariable: 'NANCY_USER')]) {
sh './scripts/ci/test-backend' script {
def shStatusCode = sh(label: 'test-backend', returnStatus: true, script: '''
set -e
./scripts/ci/test-backend > ${WORKSPACE}/tmp-sh-build 2>&1
''')
shOutput = readFile "${env.WORKSPACE}/tmp-sh-build"
if (shStatusCode != 0) {
error "${shOutput}"
}
}
} }
sh './scripts/ci/build-backend' // Build all the golang binaries
script {
def shStatusCode = sh(label: 'build-backend', returnStatus: true, script: '''
set -e
./scripts/ci/build-backend > ${WORKSPACE}/tmp-sh-build 2>&1
''')
shOutput = readFile "${env.WORKSPACE}/tmp-sh-build"
if (shStatusCode != 0) {
error "${shOutput}"
}
}
// Build the docker image used for testing below
sh '''docker build --pull --no-cache \\ sh '''docker build --pull --no-cache \\
-t "${IMAGE}:${BRANCH_LOWER}-ci-${BUILD_NUMBER}" \\ -t "${IMAGE}:${BRANCH_LOWER}-ci-${BUILD_NUMBER}" \\
-f docker/Dockerfile \\ -f docker/Dockerfile \\
@ -83,8 +133,15 @@ pipeline {
} }
} }
post { post {
always {
sh 'rm -f ${WORKSPACE}/tmp-sh-build'
}
failure {
npmGithubPrComment("CI Error:\n\n```\n${shOutput}\n```", true)
}
success { success {
archiveArtifacts allowEmptyArchive: false, artifacts: 'bin/*' archiveArtifacts allowEmptyArchive: false, artifacts: 'bin/*'
shOutput = ""
} }
} }
} }
@ -152,7 +209,7 @@ pipeline {
withCredentials([string(credentialsId: 'npm-sentry-dsn', variable: 'SENTRY_DSN')]) { withCredentials([string(credentialsId: 'npm-sentry-dsn', variable: 'SENTRY_DSN')]) {
withCredentials([usernamePassword(credentialsId: 'jc21-dockerhub', passwordVariable: 'dpass', usernameVariable: 'duser')]) { withCredentials([usernamePassword(credentialsId: 'jc21-dockerhub', passwordVariable: 'dpass', usernameVariable: 'duser')]) {
sh 'docker login -u "${duser}" -p "${dpass}"' sh 'docker login -u "${duser}" -p "${dpass}"'
sh './scripts/buildx --push ${BUILDX_PUSH_TAGS}' sh "./scripts/buildx --push ${buildxPushTags}"
// sh './scripts/buildx -o type=local,dest=docker-build' // sh './scripts/buildx -o type=local,dest=docker-build'
} }
} }
@ -161,33 +218,14 @@ pipeline {
stage('Docs Deploy') { stage('Docs Deploy') {
when { when {
allOf { allOf {
branch 'v3' // TOODO: change to master when ready branch 'v3' // TODO: change to master when ready
not { not {
equals expected: 'UNSTABLE', actual: currentBuild.result equals expected: 'UNSTABLE', actual: currentBuild.result
} }
} }
} }
steps { steps {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'npm-s3-docs', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) { npmDocsRelease("$DOCS_BUCKET", "$DOCS_CDN")
sh """docker run --rm \\
--name \${COMPOSE_PROJECT_NAME}-docs-upload \\
-e S3_BUCKET=$DOCS_BUCKET \\
-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \\
-e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \\
-v \$(pwd):/app \\
-w /app \\
jc21/ci-tools \\
scripts/docs-upload /app/docs/.vuepress/dist/
"""
sh """docker run --rm \\
--name \${COMPOSE_PROJECT_NAME}-docs-invalidate \\
-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \\
-e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \\
jc21/ci-tools \\
aws cloudfront create-invalidation --distribution-id $DOCS_CDN --paths '/*'
"""
}
} }
} }
stage('PR Comment') { stage('PR Comment') {
@ -201,7 +239,7 @@ pipeline {
} }
steps { steps {
script { script {
def comment = pullRequest.comment("This is an automated message from CI:\n\nDocker Image for build ${BUILD_NUMBER} is available on [DockerHub](https://cloud.docker.com/repository/docker/${DOCKER_ORG}/${IMAGE}) as `${DOCKER_ORG}/${IMAGE}:github-${BRANCH_LOWER}`\n\n**Note:** ensure you backup your NPM instance before testing this PR image! Especially if this PR contains database changes.") npmGithubPrComment("Docker Image for build ${BUILD_NUMBER} is available on [DockerHub](https://cloud.docker.com/repository/docker/${DOCKER_ORG}/${IMAGE}) as `${DOCKER_ORG}/${IMAGE}:github-${BRANCH_LOWER}`\n\n**Note:** ensure you backup your NPM instance before testing this PR image! Especially if this PR contains database changes.", true)
} }
} }
} }
@ -235,13 +273,3 @@ pipeline {
} }
} }
} }
def getVersion() {
ver = sh(script: 'cat .version', returnStdout: true)
return ver.trim()
}
def getCommit() {
ver = sh(script: 'git log -n 1 --format=%h', returnStdout: true)
return ver.trim()
}

View File

@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
set -e set -e
set +x
IMAGE=jc21/gotools:latest IMAGE=jc21/gotools:latest

View File

@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
set -e set -e
set +x
BACKEND_ASSETS=backend/embed/assets BACKEND_ASSETS=backend/embed/assets

View File

@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
set -e set -e
set +x
IMAGE=jc21/gotools:latest IMAGE=jc21/gotools:latest