-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
56 lines (54 loc) · 2.52 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
pipeline {
agent any
environment {
BACKEND_PROJECT_NAME="node-devops_demoapp_backend"
}
stages {
stage('Prebuild') {
steps {
withAWS(credentials: 'devops_jenkins', region: 'ca-central-1') {
sh label: 'TerraformInit', script: 'terraform -chdir=./ci/prebuild init'
sh label: 'CreateECRRepository', script: 'terraform -chdir=./ci/prebuild apply --auto-approve -var backend_project_name=${BACKEND_PROJECT_NAME}'
}
}
}
stage('Build') {
steps {
withAWS(credentials: 'devops_jenkins', region: 'ca-central-1') {
script {
def accountId = sh(script: "aws sts get-caller-identity | grep \"Account\" | sed 's/\"Account\": \"\\(.*\\)\",/\\1/'", returnStdout: true).trim()
sh "docker build ./backend -t ${BACKEND_PROJECT_NAME}"
sh "aws ecr get-login-password | docker login --username AWS --password-stdin ${accountId}.dkr.ecr.ca-central-1.amazonaws.com"
sh "docker tag ${BACKEND_PROJECT_NAME}:latest ${accountId}.dkr.ecr.ca-central-1.amazonaws.com/${BACKEND_PROJECT_NAME}:latest"
sh "docker push ${accountId}.dkr.ecr.ca-central-1.amazonaws.com/${BACKEND_PROJECT_NAME}:latest"
}
}
}
}
stage('DeployStaging') {
steps {
withAWS(credentials: 'devops_jenkins', region: 'ca-central-1') {
script {
sh "terraform -chdir=./ci/deploy/staging init -backend-config=../backend-staging.tf"
def accountId = sh(script: "aws sts get-caller-identity | grep \"Account\" | sed 's/\"Account\": \"\\(.*\\)\",/\\1/'", returnStdout: true).trim()
sh "terraform -chdir=./ci/deploy/staging apply --auto-approve -var aws_ecr_backend_repository_url=${accountId}.dkr.ecr.ca-central-1.amazonaws.com/${BACKEND_PROJECT_NAME}"
}
}
}
}
stage('Destroy') {
input{
message "Do you want to destroy the application?"
}
steps {
withAWS(credentials: 'devops_jenkins', region: 'ca-central-1') {
script {
def accountId = sh(script: "aws sts get-caller-identity | grep \"Account\" | sed 's/\"Account\": \"\\(.*\\)\",/\\1/'", returnStdout: true).trim()
sh "terraform -chdir=./ci/deploy/staging destroy --auto-approve -var aws_ecr_backend_repository_url=${accountId}.dkr.ecr.ca-central-1.amazonaws.com/${BACKEND_PROJECT_NAME}"
sh "terraform -chdir=./ci/prebuild destroy --auto-approve -var backend_project_name=${BACKEND_PROJECT_NAME}"
}
}
}
}
}
}