-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathJenkinsfile
83 lines (70 loc) · 2.54 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Requirement:
* 1. Kubernetes cluster
* 2. Credentials:
* - docker-hub
* - aws-eb-key
* 3. AWS Elastic Benstalk in us-east-1 region
* - Application: jrcms
* - Environment: jrcms-test, jrcms-staging and jrcms-production
**/
podTemplate(
containers: [containerTemplate(name: 'docker', image: 'docker', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'eb', image: 'mini/eb-cli', command: 'cat', ttyEnabled: true)],
volumes: [hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock')]
) {
node(POD_LABEL) {
def image
withCredentials([usernamePassword(credentialsId: 'docker-hub', usernameVariable: 'USER', passwordVariable: 'PASSWD')]) {
image = "${USER}/jrcms:${currentBuild.number}"
}
stage('Build and Test') {
checkout scm
container('docker') {
sh "docker build -t ${image} ."
}
}
if (env.BRANCH_NAME == 'master') {
stage('Push Docker image') {
withCredentials([usernamePassword(credentialsId: 'docker-hub', usernameVariable: 'USER', passwordVariable: 'PASSWD')]) {
container('docker') {
sh "docker login --username ${USER} --password ${PASSWD}"
sh "docker push ${image}"
}
}
}
stage("Deploy to test environment") {
deployToEB('test')
}
stage("Integration test to test environment") {
smokeTest('test')
}
stage("Deploy to staging environment") {
deployToEB('staging')
}
stage("Integration test to staging environment") {
smokeTest('staging')
}
stage("Deploy to production environment") {
deployToEB('production')
}
}
}
}
def smokeTest(environment) {
container('eb') {
}
}
def deployToEB(environment) {
checkout scm
withCredentials([usernamePassword(credentialsId: 'aws-eb-key', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY')]) {
container('eb') {
withEnv(["AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}", "AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}", "AWS_REGION=us-east-1"]) {
dir("deployment") {
sh "sh generate-dockerrun.sh ${currentBuild.number}"
sh "eb deploy jrcms-${environment} -l ${currentBuild.number}"
}
}
}
}
}