Skip to content

Commit

Permalink
Merge pull request #1 from wearefine/add_skip_test
Browse files Browse the repository at this point in the history
Add skip test
  • Loading branch information
cpitkin authored Nov 17, 2017
2 parents 8404e87 + e711aa5 commit 0b951d4
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 98 deletions.
24 changes: 22 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# rails-jenkins-shared-libraries Changelog
# Changelog
All notable changes to this project will be documented in this file.

## 1.0.0
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased] - TBD

## [v1.1.0] - 11-16-2017
### Added
- SKIP_TESTS parameter so you can just deploy code
- More debug output in railsRvm function

### Fixed
- ruby_string being undeclared
- DEBUG not setting the env var when passing in true

### Updated
- railsinstallDeps is now its own function
- railsDeploy is now its own function
- SQL commands now clean old DBs, and Users before adding new ones at the start of the build

## [v1.0.0]

- EVERYTHING :tada:
1 change: 1 addition & 0 deletions Jenkinsfile.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ rails {
DEPLOY_VARS = [string(credentialsId: 'secret-text', variable: 'secret-text'), usernameColonPassword(credentialsId: 'git_access', variable: 'git-login-creds')]
SLACK_CHANNEL = '#deploys'
DEBUG = 'false'
SKIP_TESTS = 'false'
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ rails {
DEPLOY_VARS = [string(credentialsId: 'secret-text', variable: 'secret-text'), usernameColonPassword(credentialsId: 'git_access', variable: 'git-login-creds')]
SLACK_CHANNEL = '#deploys'
DEBUG = 'false'
SKIP_TESTS = 'false'
}
```

Expand All @@ -81,6 +82,7 @@ rails {
- **DEPLOY_VARS:** Credential strings you wish to have set during deployment only. See [Deploy Vars Configuration](#Deploy Vars Configuration) for more details. [List]
- **SLACK_CHANNEL:** Specify the Slack channel to use for notifications. [String] Default: #puppet
- **DEBUG:** Turn off Slack notifications and turn on more console output. [String] Default: false
- **SKIP_TESTS:** Don't run tests just checkout and deploy [String] Default: false

## Testing Framework Support

Expand Down
113 changes: 23 additions & 90 deletions vars/rails.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,20 @@ def call(body) {
config.DEBUG = 'false'
env.DEBUG = 'false'
}
else {
env.DEBUG = 'true'
}
if (!config.NODE_INSTALL_NAME) {
error 'NODE_INSTALL_NAME is required'
}
if (!config.SSH_AGENT_ID) {
error 'SSH_AGENT_ID is required'
}
if (!config.SKIP_TESTS){
config.SKIP_TESTS = 'false'
} else {
env.SKIP_TESTS = config.SKIP_TESTS
}

node {
timestamps {
Expand All @@ -79,6 +87,7 @@ def call(body) {
throw e
}

if (config.SKIP_TESTS == 'false') {
getDatabaseConnection(id: 'test_db', type: 'GLOBAL') {
nodejs(nodeJSInstallationName: config.NODE_INSTALL_NAME) {
if (config.DEBUG == 'true') {
Expand All @@ -102,6 +111,10 @@ def call(body) {
}
env.MYSQL_DATABASE = db_name

sql connection: 'test_db', sql: "DROP DATABASE IF EXISTS ${env.MYSQL_DATABASE};"
sql connection: 'test_db', sql: "REVOKE ALL PRIVILEGES, GRANT OPTION FROM ${env.MYSQL_USER}@'%';"
sql connection: 'test_db', sql: "DROP USER ${env.MYSQL_USER}@'%';"

sql connection: 'test_db', sql: "CREATE DATABASE IF NOT EXISTS ${env.MYSQL_DATABASE};"
echo "SQL: CREATE DATABASE IF NOT EXISTS ${env.MYSQL_DATABASE};"
sql connection: 'test_db', sql: "GRANT ALL ON ${env.MYSQL_DATABASE}.* TO \'${env.MYSQL_USER}\'@\'%\' IDENTIFIED BY \'${env.MYSQL_PASSWORD}\';"
Expand All @@ -116,22 +129,7 @@ def call(body) {
throw e
}

try {
stage('Install Dependancies') {
milestone label: 'Install Dependancies'
retry(2) {
railsRvm('bundle install')
}
currentBuild.result = 'SUCCESS'
}
} catch(Exception e) {
currentBuild.result = 'FAILURE'
sql connection: 'test_db', sql: "DROP DATABASE IF EXISTS ${env.MYSQL_DATABASE};"
if (config.DEBUG == 'false') {
railsSlack(config.SLACK_CHANNEL)
}
throw e
}
railsInstallDeps(config)

try {
stage('Load Schema') {
Expand All @@ -143,7 +141,6 @@ def call(body) {
}
} catch(Exception e) {
currentBuild.result = 'FAILURE'
sql connection: 'test_db', sql: "DROP DATABASE IF EXISTS ${env.MYSQL_DATABASE};"
if (config.DEBUG == 'false') {
railsSlack(config.SLACK_CHANNEL)
}
Expand Down Expand Up @@ -176,82 +173,13 @@ def call(body) {
} catch(Exception e) {
junit allowEmptyResults: true, keepLongStdio: true, testResults: 'testresults/*.xml'
currentBuild.result = 'FAILURE'
sql connection: 'test_db', sql: "DROP DATABASE IF EXISTS ${env.MYSQL_DATABASE};"
if (config.DEBUG == 'false') {
railsSlack(config.SLACK_CHANNEL)
}
throw e
}

try {
stage('Deploy') {
milestone label: 'Deploy'
sshagent([config.SSH_AGENT_ID]) {
if (config.DEPLOY_VARS) {
withCredentials(config.DEPLOY_VARS) {
if (config.CAP_VERSION == '3'){
if (env.BRANCH_NAME == 'master') {
railsRvm('cap prod deploy')
}
else if(env.BRANCH_NAME == 'stage') {
railsRvm('cap stage deploy')
}
else if(env.BRANCH_NAME == 'dev') {
railsRvm('cap dev deploy')
}
railsOtherBuildEnvs()
}
if (config.CAP_VERSION == '2'){
if (env.BRANCH_NAME == 'master') {
railsRvm('cap deploy -S loc=prod')
}
else if(env.BRANCH_NAME == 'stage') {
railsRvm('cap deploy -S loc=stage -S branch=stage')
}
else if(env.BRANCH_NAME == 'dev') {
railsRvm('cap deploy -S loc=dev -S branch=dev')
}
railsOtherBuildEnvs()
}
}
}
else {
if (config.CAP_VERSION == '3'){
if (env.BRANCH_NAME == 'master') {
railsRvm('cap prod deploy')
}
else if(env.BRANCH_NAME == 'stage') {
railsRvm('cap stage deploy')
}
else if(env.BRANCH_NAME == 'dev') {
railsRvm('cap dev deploy')
}
railsOtherBuildEnvs()
}
if (config.CAP_VERSION == '2'){
if (env.BRANCH_NAME == 'master') {
railsRvm('cap deploy -S loc=prod')
}
else if(env.BRANCH_NAME == 'stage') {
railsRvm('cap deploy -S loc=stage -S branch=stage')
}
else if(env.BRANCH_NAME == 'dev') {
railsRvm('cap deploy -S loc=dev -S branch=dev')
}
railsOtherBuildEnvs()
}
}
}
currentBuild.result = 'SUCCESS'
}
} catch(Exception e) {
currentBuild.result = 'FAILURE'
sql connection: 'test_db', sql: "DROP DATABASE IF EXISTS ${env.MYSQL_DATABASE};"
if (config.DEBUG == 'false') {
railsSlack(config.SLACK_CHANNEL)
}
throw e
}
railsDeploy(config)

try {
stage('Clean Up') {
Expand All @@ -273,9 +201,14 @@ def call(body) {
}
} // railsNodejs
} // railsDatabase
} // timestamps
if (config.DEBUG == 'false') {
railsSlack(config.SLACK_CHANNEL)
} // SKIP_TESTS
else {
railsInstallDeps(config)
railsDeploy(config)
}
if (config.DEBUG == 'false') {
railsSlack(config.SLACK_CHANNEL)
}
} // timestamps
} // node
}
72 changes: 72 additions & 0 deletions vars/railsDeploy.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env groovy

def call(Map config) {
try {
stage('Deploy') {
milestone label: 'Deploy'
sshagent([config.SSH_AGENT_ID]) {
if (config.DEPLOY_VARS) {
withCredentials(config.DEPLOY_VARS) {
if (config.CAP_VERSION == '3'){
if (env.BRANCH_NAME == 'master') {
railsRvm('cap prod deploy')
}
else if(env.BRANCH_NAME == 'stage') {
railsRvm('cap stage deploy')
}
else if(env.BRANCH_NAME == 'dev') {
railsRvm('cap dev deploy')
}
railsOtherBuildEnvs()
}
if (config.CAP_VERSION == '2'){
if (env.BRANCH_NAME == 'master') {
railsRvm('cap deploy -S loc=prod')
}
else if(env.BRANCH_NAME == 'stage') {
railsRvm('cap deploy -S loc=stage -S branch=stage')
}
else if(env.BRANCH_NAME == 'dev') {
railsRvm('cap deploy -S loc=dev -S branch=dev')
}
railsOtherBuildEnvs()
}
}
}
else {
if (config.CAP_VERSION == '3'){
if (env.BRANCH_NAME == 'master') {
railsRvm('cap prod deploy')
}
else if(env.BRANCH_NAME == 'stage') {
railsRvm('cap stage deploy')
}
else if(env.BRANCH_NAME == 'dev') {
railsRvm('cap dev deploy')
}
railsOtherBuildEnvs()
}
if (config.CAP_VERSION == '2'){
if (env.BRANCH_NAME == 'master') {
railsRvm('cap deploy -S loc=prod')
}
else if(env.BRANCH_NAME == 'stage') {
railsRvm('cap deploy -S loc=stage -S branch=stage')
}
else if(env.BRANCH_NAME == 'dev') {
railsRvm('cap deploy -S loc=dev -S branch=dev')
}
railsOtherBuildEnvs()
}
}
}
currentBuild.result = 'SUCCESS'
}
} catch(Exception e) {
currentBuild.result = 'FAILURE'
if (config.DEBUG == 'false') {
railsSlack(config.SLACK_CHANNEL)
}
throw e
}
}
19 changes: 19 additions & 0 deletions vars/railsInstallDeps.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env groovy

def call(Map config) {
try {
stage('Install Dependancies') {
milestone label: 'Install Dependancies'
retry(2) {
railsRvm('bundle install')
}
currentBuild.result = 'SUCCESS'
}
} catch(Exception e) {
currentBuild.result = 'FAILURE'
if (config.DEBUG == 'false') {
railsSlack(config.SLACK_CHANNEL)
}
throw e
}
}
14 changes: 8 additions & 6 deletions vars/railsRvm.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

def call(String commands) {
if(env.BRANCH_NAME == 'master'){
ruby_string = "${env.RUBY_VERSION}@${env.RUBY_GEMSET}"
sh "bash -c \"source /usr/local/rvm/scripts/rvm && rvm use --install --create ${env.RUBY_VERSION}@${env.RUBY_GEMSET} && ${commands}\""
}
else if(env.BRANCH_NAME == 'stage' | env.BRANCH_NAME == 'dev'){
ruby_string = "${env.RUBY_VERSION}@${env.BRANCH_NAME}-${env.RUBY_GEMSET}"
else if(env.BRANCH_NAME == 'stage' || env.BRANCH_NAME == 'dev'){
sh "bash -c \"source /usr/local/rvm/scripts/rvm && rvm use --install --create ${env.RUBY_VERSION}@${env.BRANCH_NAME}-${env.RUBY_GEMSET} && ${commands}\""
}
else if(env.BRANCH_NAME == /PR-.*/) {
ruby_string = "${env.RUBY_VERSION}@pr-${env.RUBY_GEMSET}"
else if (env.BRANCH_NAME == /PR-.*/) {
sh "bash -c \"source /usr/local/rvm/scripts/rvm && rvm use --install --create ${env.RUBY_VERSION}@pr-${env.RUBY_GEMSET} && ${commands}\""
}
if (env.DEBUG == 'true') {
println "*************************"
println "${env.RUBY_VERSION}"
println "${env.RUBY_GEMSET}"
println "${env.BRANCH_NAME}"
println "RVM Commands: ${commands}"
println "*************************"
}
sh "bash -c \"source /usr/local/rvm/scripts/rvm && rvm use --install --create ${ruby_string} && ${commands}\""
}

0 comments on commit 0b951d4

Please sign in to comment.