From 0e9342fab63ea4f4c913471b7ae28e8f52c1c7a9 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Mon, 23 Oct 2023 07:41:52 -0700 Subject: [PATCH 1/9] only build the production environment when the previous steps all executed successfully --- Jenkinsfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Jenkinsfile b/Jenkinsfile index ca09ef4..9553ec7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -62,6 +62,7 @@ pipeline { } stage('Build production deliverable') { steps { + when { equals expected: 'SUCCESS', actual: buildResult } catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { sh ''' #!/usr/local/bin/bash ZIP_FILE="url-shortener_${BRANCH_NAME}_$(date "+%Y-%m-%d_%H-%M-%S").tgz" From 35583e404edce52ab419c16659f376b9c19cc03e Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Mon, 23 Oct 2023 07:43:30 -0700 Subject: [PATCH 2/9] move the local path in the build prod artifact stage to make the tests quicker --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 9553ec7..5d46486 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -18,7 +18,6 @@ pipeline { } stage('Build dependencies') { steps { - sh 'bundle config set --local path "vendor"' sh 'bundle install' sh 'sequel -m db/migrations sqlite://db/${DB_NAME}' } @@ -68,6 +67,7 @@ pipeline { ZIP_FILE="url-shortener_${BRANCH_NAME}_$(date "+%Y-%m-%d_%H-%M-%S").tgz" CUR_DIR=$(pwd) bundle config set --local without 'test' + bundle config set --local path "vendor" bundle clean mkdir -p /tmp/url-shortener cp -R * /tmp/url-shortener From 958091e000eaf70385526a6ed3e8b1c1a93c6085 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Mon, 23 Oct 2023 11:32:02 -0700 Subject: [PATCH 3/9] see if we can access the buildResult status to use it --- Jenkinsfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Jenkinsfile b/Jenkinsfile index 5d46486..e4b8940 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -61,6 +61,7 @@ pipeline { } stage('Build production deliverable') { steps { + echo "BUILD RESULT: ${buildResult}" when { equals expected: 'SUCCESS', actual: buildResult } catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { sh ''' #!/usr/local/bin/bash From de3dcc48ab39298899034d79e7ebaa7944091536 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Mon, 23 Oct 2023 12:31:29 -0700 Subject: [PATCH 4/9] removed the when, and removed unnecessary rm command --- Jenkinsfile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index e4b8940..41598a7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -62,7 +62,6 @@ pipeline { stage('Build production deliverable') { steps { echo "BUILD RESULT: ${buildResult}" - when { equals expected: 'SUCCESS', actual: buildResult } catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { sh ''' #!/usr/local/bin/bash ZIP_FILE="url-shortener_${BRANCH_NAME}_$(date "+%Y-%m-%d_%H-%M-%S").tgz" @@ -87,7 +86,6 @@ pipeline { stage('Clean up deliverable') { steps { sh 'rm -rf /tmp/url-shortener' - sh 'rm -rf *.tgz' } } } From 7c0f6eb60325deebf9d19dab39b26749b5246ae8 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Mon, 23 Oct 2023 14:58:02 -0700 Subject: [PATCH 5/9] limit building deployment artifact + added buildArtifact variable and set it to true + in auditing stage, if there's an error, set buildArtifact to false + in build artifact stage, if buildArtifact is true, build it --- Jenkinsfile | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 41598a7..674125c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,3 +1,5 @@ +def buildArtifact = true + pipeline { agent { label 'ruby && freebsd' } @@ -25,8 +27,16 @@ pipeline { stage('Audit Dependencies') { steps { catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { - sh 'bundle exec ruby-audit check' - sh 'bundle exec bundle-audit check >> audit.html' + script { + try { + sh 'bundle exec ruby-audit check' + sh 'bundle exec bundle-audit check >> audit.html' + } catch (e) { + script { + buildArtifact = false + } + } + } } } } @@ -61,7 +71,11 @@ pipeline { } stage('Build production deliverable') { steps { - echo "BUILD RESULT: ${buildResult}" + when { + expression { + buildArtifact + } + } catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { sh ''' #!/usr/local/bin/bash ZIP_FILE="url-shortener_${BRANCH_NAME}_$(date "+%Y-%m-%d_%H-%M-%S").tgz" From 5cd296cb702a4c8fa6cdb5f6a87919e91f4200a0 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Mon, 23 Oct 2023 15:03:45 -0700 Subject: [PATCH 6/9] when is outside the steps --- Jenkinsfile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 674125c..753fd86 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -70,12 +70,12 @@ pipeline { } } stage('Build production deliverable') { - steps { - when { - expression { - buildArtifact - } + when { + expression { + buildArtifact } + } + steps { catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { sh ''' #!/usr/local/bin/bash ZIP_FILE="url-shortener_${BRANCH_NAME}_$(date "+%Y-%m-%d_%H-%M-%S").tgz" From fbba67a9bdc30b47d0267963ad615568761020fe Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Mon, 23 Oct 2023 15:05:20 -0700 Subject: [PATCH 7/9] install, not clean, since it changed the bundle location --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 753fd86..b30a03d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -82,7 +82,7 @@ pipeline { CUR_DIR=$(pwd) bundle config set --local without 'test' bundle config set --local path "vendor" - bundle clean + bundle install mkdir -p /tmp/url-shortener cp -R * /tmp/url-shortener cp .env.rb /tmp/url-shortener/ From 5f4209dbe8a7e0659a3f6bdfca3ecbcf909bd383 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Mon, 23 Oct 2023 15:10:16 -0700 Subject: [PATCH 8/9] testing skipping building the artifact at the end (and cleanup of it) --- Jenkinsfile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index b30a03d..afb8304 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,4 +1,4 @@ -def buildArtifact = true +def buildArtifact = false pipeline { agent { label 'ruby && freebsd' } @@ -98,6 +98,11 @@ pipeline { } } stage('Clean up deliverable') { + when { + expression { + buildArtifact + } + } steps { sh 'rm -rf /tmp/url-shortener' } From 0dc792b0bfcd11855381dffff75709fba1049ea8 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Mon, 23 Oct 2023 15:48:42 -0700 Subject: [PATCH 9/9] added try catch wrappers around other parts. if they fail, don't build the deliverable --- Jenkinsfile | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index afb8304..78e9968 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,4 +1,4 @@ -def buildArtifact = false +def buildArtifact = true pipeline { agent { label 'ruby && freebsd' } @@ -43,17 +43,41 @@ pipeline { stage('Code Linting') { steps { catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') { - sh 'bundle exec rubocop --format html --out rubocop.html' + script { + try { + sh 'bundle exec rubocop --format html --out rubocop.html' + } catch (e) { + script { + buildArtifact = false + } + } + } } } } stage('Run tests') { steps { catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') { - sh 'bundle exec cucumber features --format html --out cucumber.html' + script { + try { + sh 'bundle exec cucumber features --format html --out cucumber.html' + } catch (e) { + script { + buildArtifact = false + } + } + } } catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') { - sh 'bundle exec rspec spec --format html --out spec.html' + script { + try { + sh 'bundle exec rspec spec --format html --out spec.html' + } catch (e) { + script { + buildArtifact = false + } + } + } } } }