· 10 years ago · Sep 24, 2016, 01:02 AM
1node{
2 def u = new utility.Commands()
3
4 u.slackStart("jenkins")
5
6 def err = null
7 currentBuild.result = "SUCCESS"
8
9 try {
10 stage('Create new EMT job'){
11 echo "${REPO}"
12 u.cloneEmtMaster("${SLUG}", "${REPO}")
13 }
14 }
15
16 catch (caughtError) {
17
18 err = caughtError
19 currentBuild.result = "FAILURE"
20
21 mail body: "See <${env.BUILD_URL}> \n ${err}" ,
22 from: 'webops@glynndevins.com',
23 replyTo: 'webops@glynndevins.com',
24 subject: "Jenkins Build: ${env.JOB_NAME} ${env.BUILD_DISPLAY_NAME}",
25 to: 'webops@glynndevins.com'
26
27 u.slackFail("jenkins")
28
29 }
30
31 finally {
32
33 /* Must re-throw exception to propagate error */
34 if (err) {
35 throw err
36 }else{
37 u.slackSuccess("jenkins")
38 }
39 }
40}
41
42// ... In shared libs
43def cloneEmtMaster(slug, repo){
44
45 @NonCPS
46 def getSqlInstance() {
47 def db = "jenkins"
48 def user = "jenkins_user"
49 def pwd = "xxxxxxxxxxx"
50
51 def sql = Sql.newInstance(
52 "jdbc:mysql://localhost:3306/${db}",
53 "${user}",
54 "${pwd}",
55 "com.mysql.jdbc.Driver"
56 )
57
58 return sql
59 }
60
61 echo "Connect to database..."
62 def sql = getSqlInstance()
63
64 stage('Ensure emts table exists'){
65 echo "Execute create table if not exists..."
66 sql.execute '''
67 CREATE TABLE IF NOT EXISTS emts (
68 id INTEGER NOT NULL AUTO_INCREMENT,
69 repo VARCHAR(255) NOT NULL,
70 slug VARCHAR(255) NOT NULL,
71 PRIMARY KEY (id)
72 )
73 '''
74 }
75
76 stage('Test for duplicates'){
77 echo "Select emts matching slug..."
78 def rows = sql.rows("SELECT * FROM emts WHERE slug = '${slug}'")
79
80 if( rows.size() == 0 ){
81 stage('Insert new emt row'){
82 sql.execute "INSERT INTO emts (id, repo, slug) VALUES (null, '${repo}', '${slug}')"
83
84 //stage "Rebuild seed jobs"
85 //build job: "/EMT/emt-seed-from-db", quietPeriod: 0
86 }
87 } else {
88 echo "${slug} already exists in emts table."
89 }
90 }
91
92 sql.close()
93}