pipeline { agent { label 'ruby && freebsd' } environment { DB_NAME = 'url_shortener.db' } stages { stage('Init') { steps { sh ''' #!/usr/local/bin/bash rbenv local 3.2.2 echo "ENV[\\\"APP_SESSION_SECRET\\\"] ||= $(ruby -rsecurerandom -e 'puts SecureRandom.base64(64).inspect()')" > .env.rb echo "ENV[\\\"DB_NAME\\\"] ||= \\\"${DB_NAME}\\\"" >> .env.rb ''' } } stage('Build dependencies') { steps { sh 'bundle config set --local path "vendor"' sh 'bundle install' sh 'sequel -m db/migrations sqlite://db/${DB_NAME}' } } stage('Code Linting') { steps { catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') { sh 'bundle exec rubocop --format html --out rubocop.html' } } } stage('Run tests') { steps { catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') { sh 'bundle exec cucumber features --format html --out cucumber.html' } catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') { sh 'bundle exec rspec spec --format html --out spec.html' } } } stage('Report results') { steps { publishHTML (target: [ allowMissing: false, alwaysLinkToLastBuild: false, keepAll: true, reportDir: '.', reportFiles: 'rubocop.html, cucumber.html, spec.html, coverage/index.html', reportName: 'Test Results', reportTitles: 'Rubocop Results, Cucumber Results, RSpec Results, Test Coverage']) } } stage('Build production deliverable') { 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" CUR_DIR=$(pwd) mkdir -p /tmp/url-shortener cp -R * /tmp/url-shortener cp .env.rb /tmp/url-shortener/ cp .ruby-version /tmp/url-shortener/ cd /tmp/url-shortener rm -rf features spec coverage db/*.db .git* Jenkinsfile *.html .rubocop.yml cd /tmp tar -czvf $ZIP_FILE url-shortener/ mv /tmp/$ZIP_FILE $CUR_DIR/ ''' } archiveArtifacts artifacts: '*.tgz' } } stage('Clean up deliverable') { steps { sh 'rm -rf /tmp/url-shortener' sh 'rm -rf *.tgz' } } } post { success { mattermostSend channel: 'git-messages', color: 'good', message: "[${JOB_NAME}](${JOB_URL}) [#${BUILD_NUMBER}](${BUILD_URL}) ([Gitea](${GIT_URL}))", text: 'Build Finished Successfully' } unstable { mattermostSend channel: 'git-messages', color: 'warning', message: "[${JOB_NAME}](${JOB_URL}) [#${BUILD_NUMBER}](${BUILD_URL}) ([Gitea](${GIT_URL}))", text: "Build Unstable" } failure { mattermostSend channel: 'git-messages', color: 'danger', message: "[${JOB_NAME}](${JOB_URL}) [#${BUILD_NUMBER}](${BUILD_URL}) ([Gitea](${GIT_URL}))", text: "Build Failed" } always { cleanWs deleteDirs: true, patterns: [[pattern: '*', type: 'INCLUDE']] } } }