Skip to content

Instantly share code, notes, and snippets.

@erpe
Last active December 13, 2022 05:44
Show Gist options
  • Save erpe/6a673335537d530fea178385cb71ecc3 to your computer and use it in GitHub Desktop.
Save erpe/6a673335537d530fea178385cb71ecc3 to your computer and use it in GitHub Desktop.
Jenkinsfile for a multibranch-pipeline project that builds and deploys a rails application - using capistrano and rvm on jenkins-server
// jenkinsfile - as declarative pipeline
pipeline {
agent any
environment {
// credentials 'jenkins-db-credential' is globally accessible on jenkins
//
DB_CREDENTIAL = credentials('jenkins-db-credential')
DB_USER = "${env.DB_CREDENTIAL_USR}"
DB_PASS = "${env.DB_CREDENTIAL_PSW}"
RAILS_ENV = 'test'
}
stages {
stage("prepare") {
steps {
// credentials 'rails-master-key' have to be present
// for this specific multibranch-pipeline
withCredentials([string(credentialsId: 'rails-master-key', variable: 'RAILS_MASTER_KEY')]) {
echo 'preparing sch_net build'
sh 'echo "test:\n adapter: postgresql\n database: sch_net_test\n username: $DB_USER\n passsword: $DB_PASS" > ./config/database.yml'
sh '''#!/bin/bash -l
rvm use ruby-2.6.5
gem install bundler
bundle install
./bin/rails db:reset
'''
}
}
}
stage("test") {
steps {
withCredentials([string(credentialsId: 'rails-master-key', variable: 'RAILS_MASTER_KEY')]) {
echo 'testing sch_net'
sh '''#!/bin/bash -l
rvm use ruby-2.6.5
./bin/rails test
'''
}
}
}
stage("deploy staging") {
when {
expression {
env.BRANCH_NAME == 'staging'
}
}
steps {
echo 'deploying staging'
sh '''#!/bin/bash -l
rvm use ruby-2.6.5
bundle exec cap staging deploy
'''
}
}
stage("deploying production") {
when {
expression {
env.BRANCH_NAME == 'master'
}
}
steps {
echo 'deploying master'
sh '''#!/bin/bash -l
rvm use ruby-2.6.5
bundle exec cap production deploy
'''
}
}
}
post {
always {
echo 'always after sch_net'
}
success {
echo 'after success sch_net'
}
failure {
echo 'after failure sch_net'
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment