-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathJenkinsfile
103 lines (89 loc) · 3.72 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
pipeline {
agent any
environment{
AWS_CRED = 'AWS_sortlog' //Change to yours AWS credentials
AWS_REGION = 'ap-southeast-2'
}
//Install denpendencies
stages{
stage('Install dependency')
{
steps{
echo "Installing packages"
sh 'yarn install'
}
}
stage('yarn build')
{
steps{
sh " yarn export"
sh 'ls -la ./out'
}
}
stage('TF Launch for Production'){
when {branch 'main'}
steps {
withAWS(credentials: AWS_CRED, region: AWS_REGION) {
sh '''
export APP_ENV="frontend"
terraform init -input=false
terraform workspace select ${APP_ENV} || terraform workspace new ${APP_ENV}
terraform apply \
-var="app_env=${APP_ENV}"\
--auto-approve
'''
script {
S3_BUCKET_NAME = sh(returnStdout: true, script: " terraform output name").trim()
CLOUDFRONT_DISTRIBUTION_ID = sh(returnStdout: true, script: "terraform output arn").trim()
CLOUDFRONT_DOMAIN_NAME = sh(returnStdout: true, script: "terraform output domain").trim()
}
}
}
}
stage('upload to S3 bucket production') {
when {branch 'main'}
steps {
withAWS(credentials: AWS_CRED, region: AWS_REGION)
{ dir('./out')
{
echo "deploy to S3 "
sh "aws s3 sync . s3://${S3_BUCKET_NAME}"
// sh "aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_DISTRIBUTION_ID --paths '/*'"
}
}
}
}
stage('TF Launch for UAT'){
when {branch 'uat'}
steps {
withAWS(credentials: AWS_CRED, region: AWS_REGION) {
sh '''
export APP_ENV="uat-p"
terraform init -input=false
terraform workspace select ${APP_ENV} || terraform workspace new ${APP_ENV}
terraform destroy \
-var="app_env=${APP_ENV}"\
--auto-approve
'''
script {
S3_BUCKET_NAME = sh(returnStdout: true, script: " terraform output name").trim()
CLOUDFRONT_DISTRIBUTION_ID = sh(returnStdout: true, script: "terraform output arn").trim()
CLOUDFRONT_DOMAIN_NAME = sh(returnStdout: true, script: "terraform output domain").trim()
}
}
}
}
stage('upload to S3 bucket UAT') {
when {branch 'uat'}
steps {
withAWS(credentials: AWS_CRED, region: AWS_REGION)
{ dir('./out')
{
echo "deploy to S3 "
sh "aws s3 sync . s3://${S3_BUCKET_NAME}"
// sh "aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_DISTRIBUTION_ID --paths '/*'"
}
}
}
}
}}