Created
September 23, 2016 16:19
-
-
Save jjtroberts/432a49986f3daa0878eccdee054cbf79 to your computer and use it in GitHub Desktop.
Jenkins MySQL connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node{ | |
def u = new utility.Commands() | |
u.slackStart("jenkins") | |
def err = null | |
currentBuild.result = "SUCCESS" | |
try { | |
stage('Create new EMT job'){ | |
echo "${REPO}" | |
u.cloneEmtMaster("${SLUG}", "${REPO}") | |
} | |
} | |
catch (caughtError) { | |
err = caughtError | |
currentBuild.result = "FAILURE" | |
mail body: "See <${env.BUILD_URL}> \n ${err}" , | |
from: 'webops@glynndevins.com', | |
replyTo: 'webops@glynndevins.com', | |
subject: "Jenkins Build: ${env.JOB_NAME} ${env.BUILD_DISPLAY_NAME}", | |
to: 'webops@glynndevins.com' | |
u.slackFail("jenkins") | |
} | |
finally { | |
/* Must re-throw exception to propagate error */ | |
if (err) { | |
throw err | |
}else{ | |
u.slackSuccess("jenkins") | |
} | |
} | |
} | |
// ... In shared libs | |
def cloneEmtMaster(slug, repo){ | |
@NonCPS | |
def getSqlInstance() { | |
def db = "jenkins" | |
def user = "jenkins_user" | |
def pwd = "xxxxxxxxxxx" | |
def sql = Sql.newInstance( | |
"jdbc:mysql://localhost:3306/${db}", | |
"${user}", | |
"${pwd}", | |
"com.mysql.jdbc.Driver" | |
) | |
return sql | |
} | |
echo "Connect to database..." | |
def sql = getSqlInstance() | |
stage('Ensure emts table exists'){ | |
echo "Execute create table if not exists..." | |
sql.execute ''' | |
CREATE TABLE IF NOT EXISTS emts ( | |
id INTEGER NOT NULL AUTO_INCREMENT, | |
repo VARCHAR(255) NOT NULL, | |
slug VARCHAR(255) NOT NULL, | |
PRIMARY KEY (id) | |
) | |
''' | |
} | |
stage('Test for duplicates'){ | |
echo "Select emts matching slug..." | |
def rows = sql.rows("SELECT * FROM emts WHERE slug = '${slug}'") | |
if( rows.size() == 0 ){ | |
stage('Insert new emt row'){ | |
sql.execute "INSERT INTO emts (id, repo, slug) VALUES (null, '${repo}', '${slug}')" | |
//stage "Rebuild seed jobs" | |
//build job: "/EMT/emt-seed-from-db", quietPeriod: 0 | |
} | |
} else { | |
echo "${slug} already exists in emts table." | |
} | |
} | |
sql.close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment