-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
149 lines (137 loc) · 4.34 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def load_conf(branch) {
echo "Branch ${branch}."
def project_name = ""
def stage = ""
def aws_default_region = ""
switch(branch) {
case 'develop':
def file = readJSON(file: './conf/dev_conf.json')
project_name = file["PROJECT_NAME"]
stage = file["STAGE"]
aws_default_region = file["AWS_DEFAULT_REGION"]
case 'master':
def file = readJSON(file: './conf/prod_conf.json')
project_name = file["PROJECT_NAME"]
stage = file["STAGE"]
aws_default_region = file["AWS_DEFAULT_REGION"]
default:
def file = readJSON(file: './conf/test_conf.json')
project_name = file["PROJECT_NAME"]
stage = file["STAGE"]
stage += '-' + env.BRANCH_NAME.split('-')[0].split('/')[1]
aws_default_region = file["AWS_DEFAULT_REGION"]
}
env.BUCKET_NAME = "front-" + project_name + "-" + stage
env.PROJECT_NAME = project_name
env.STAGE = stage
env.AWS_DEFAULT_REGION = aws_default_region
}
def create_env_file() {
def apigw_id = sh(
script: "aws apigateway get-rest-apis --region=\"$env.AWS_DEFAULT_REGION\" --output text --query 'items[?name==`APIGW-$env.PROJECT_NAME-$env.STAGE`].id'",
returnStdout: true
).trim()
sh "echo REACT_APP_APIGW_ID=$apigw_id > .env.$env.STAGE"
sh "echo REACT_APP_AWS_DEFAULT_REGION=$env.AWS_DEFAULT_REGION >> .env.$env.STAGE"
sh "echo REACT_APP_STAGE=$env.STAGE >> .env.$env.STAGE"
sh "cat .env.$env.STAGE"
}
pipeline {
agent any
tools {nodejs "nodejs"}
environment {
// To run npm tests in non interactive mode
CI = true
BRANCH_ENV = "${env.BRANCH_NAME == 'develop' ? 'develop' : env.BRANCH_NAME == 'master' ? 'production' : 'none'}"
}
stages {
stage('Check Deploy') {
steps {
script {
deploy = true
git_commit_message = sh(returnStdout: true, script: 'git show -s --format=%B -1').trim()
echo ("Commit: ${git_commit_message}")
if ((git_commit_message.endsWith("NODEPLOY") && BRANCH_ENV == 'none') || BRANCH_ENV == 'none') {
echo "This build will not be deployed."
deploy = false
}
echo "test"
echo env.BRANCH_ENV
// Basically, you should not need to deploy front on a test env
// But in case you really need, add DEPLOY at the end of the commit message
if (git_commit_message.endsWith("DEPLOY") && BRANCH_ENV == 'none') {
echo "This build is deploying..."
deploy = true
}
}
}
}
stage('Load Conf') {
steps {
load_conf(env.BRANCH_NAME)
}
}
stage('Init') {
steps {
sh "pip3 install --upgrade awscli --user"
sh "npm cache clean --force"
sh "npm install"
}
}
stage('Create Env File') {
steps {
create_env_file()
}
}
stage('Unit Tests') {
steps {
sh "npm run test"
}
}
stage('Before Deploy') {
when {
expression {
deploy == true
}
}
steps {
echo ""
}
}
stage('Build App') {
when {
expression {
deploy == true
}
}
steps {
sh "npm run build"
}
}
stage('Deploy') {
when {
expression {
deploy == true
}
}
steps {
sh "aws s3 cp build s3://$env.BUCKET_NAME/ --recursive"
}
}
stage('After Deploy') {
when {
expression {
deploy == true
}
}
steps {
echo ""
}
}
stage('Clean Workspace') {
steps {
cleanWs()
}
}
}
}