forked from ever-co/ever-gauzy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
180 lines (177 loc) · 7.76 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
pipeline {
agent {
label 'docker'
}
environment {
DOCKER_BUILDKIT = 1 // Experimental faster build system
REPO_NAME = "ever-gauzy"
IMAGE_API = "gauzy-api"
IMAGE_WEBAPP = "gauzy-webapp"
GITHUB_DOCKER_USERNAME = credentials('github-docker-username')
GITHUB_DOCKER_PASSWORD = credentials('github-docker-password')
GITHUB_DOCKER_REPO = "docker.pkg.github.com/ever-co/ever-gauzy"
GITHUB_DISPATCH_TOKEN = credentials('github-dispatch-token')
GITHUB_TOKEN = credentials('github-token')
CI_URL = "ci.ever.co"
AWS_ACCESS_KEY_ID = credentials('aws-access-key')
AWS_SECRET_ACCESS_KEY = credentials('aws-secret-key')
AWS_DEFAULT_REGION = "us-east-1"
AWS_ECR_REPO = """${sh(
script: "aws ecr get-login --no-include-email --region ${env.AWS_DEFAULT_REGION} | grep -o 'https://.*' | sed -e 's|https://||g' | tr -d '\n'",
returnStdout: true
)}"""
AWS_ECR_PASSWORD = """${sh(
script: "aws ecr get-login-password --region ${env.AWS_DEFAULT_REGION}",
returnStdout: true
)}"""
WORKFLOW_ID = """${sh(
script: "curl --silent -X GET -H 'Accept: application/vnd.github.v3+json' https://api.github.com/repos/ever-co/$REPO_NAME-pulumi/actions/workflows/pulumi.yml | tr -s ' ' | tr -d '\r' | tr -d '\n' | grep -Eo '[0-9]{5,9}' | uniq",
returnStdout: true
)}"""
}
stages {
stage("Clone") {
steps{
git branch: 'develop',
url: 'https://github.com/ever-co/ever-gauzy.git'
sh """
curl 'https://api.github.com/repos/ever-co/${REPO_NAME}/statuses/$GIT_COMMIT' -H 'Authorization: token ${GITHUB_TOKEN}' -H 'Content-Type: application/json' -X POST -d '{"state": "pending", "context": "Jenkins", "description": "Jenkins pipeline is running", "target_url": "https://$CI_URL/job/${JOB_NAME}/$BUILD_NUMBER/console"}'
"""
}
post {
success {
echo "Cloning successful..."
}
failure {
echo "Cloning failed! See log for details. Terminating..."
}
}
}
stage("Docker Image Build") {
parallel {
stage("API Image") {
steps {
sh "docker build -t ${env.IMAGE_API} -f .deploy/api/Dockerfile ."
}
post{
success {
echo "Image for API built!"
}
failure {
echo "API Image build failed..."
}
}
}
stage("Gauzy WebApp Image") {
steps {
sh "docker build -t ${env.IMAGE_WEBAPP} -f .deploy/webapp/Dockerfile ."
}
post {
success {
echo "Image for Webapp built!"
}
failure {
echo "Webapp image build failed..."
}
}
}
}
}
stage("Login to repositories") {
steps {
sh "docker login docker.pkg.github.com -u ${GITHUB_DOCKER_USERNAME} -p ${GITHUB_DOCKER_PASSWORD}"
sh "docker login ${AWS_ECR_REPO} -u AWS -p ${AWS_ECR_PASSWORD}" // Login to AWS ECR
}
}
stage ("Push To AWS ECR") {
parallel {
stage ("API Image") {
steps {
// Tag and push to ECR
sh "docker tag ${IMAGE_API} ${AWS_ECR_REPO}/${IMAGE_API}"
sh "docker push ${AWS_ECR_REPO}/${IMAGE_API}"
sh "docker rmi ${AWS_ECR_REPO}/${IMAGE_API}" // Cleans tag
}
post {
success {
echo "Successfuly pushed to ECR on build ${env.BUILD_ID}!"
}
failure {
echo "Push to ECR failed! See log for details..."
}
}
}
stage ("WebApp Image") {
steps {
sh "docker tag ${IMAGE_WEBAPP} ${AWS_ECR_REPO}/${IMAGE_WEBAPP}"
sh "docker push ${AWS_ECR_REPO}/${IMAGE_WEBAPP}"
sh "docker rmi ${AWS_ECR_REPO}/${IMAGE_WEBAPP}" // Cleans tag
}
post {
success {
echo "Successfully pushed to ECR on build ${env.BUILD_ID}!"
}
failure {
echo "Push to ECR failed! See log for details..."
}
}
}
}
}
stage ("Push to GitHub") {
parallel {
stage ("API Image") {
steps {
sh "docker tag ${IMAGE_API} ${GITHUB_DOCKER_REPO}/${IMAGE_API}"
sh "docker push ${GITHUB_DOCKER_REPO}/${IMAGE_API}"
sh "docker rmi ${GITHUB_DOCKER_REPO}/${IMAGE_API}"
}
post {
success {
echo "Successfully pushed to GitHub on build ${env.BUILD_ID}!"
}
failure {
echo "Push to GitHub failed! See log for details..."
}
}
}
stage ("WebApp Image") {
steps {
sh "docker tag ${IMAGE_WEBAPP} ${GITHUB_DOCKER_REPO}/${IMAGE_WEBAPP}"
sh "docker push ${GITHUB_DOCKER_REPO}/${IMAGE_WEBAPP}"
sh "docker rmi ${GITHUB_DOCKER_REPO}/${IMAGE_WEBAPP}"
}
post {
success {
echo "Successfully pushed to GitHub on build ${env.BUILD_ID}!"
}
failure {
echo "Push to GitHub failed! See log for details..."
}
}
}
}
}
stage ("Pulumi Update") {
steps {
sh """
curl -sX POST 'https://api.github.com/repos/ever-co/${REPO_NAME}-pulumi/actions/workflows/2319005/dispatches' -H 'Accept: application/vnd.github.v3+json' -H 'Authorization: token ${GITHUB_DISPATCH_TOKEN}' -d '{"ref": "master"}'
"""
}
}
}
post {
success {
echo "Gauzy CI/CD pipeline executed successfully!"
sh """
curl 'https://api.github.com/repos/ever-co/${REPO_NAME}/statuses/$GIT_COMMIT' -H 'Authorization: token ${GITHUB_TOKEN}' -H 'Content-Type: application/json' -X POST -d '{"state": "success", "context": "Jenkins", "description": "Jenkins pipeline succeeded", "target_url": "https://$CI_URL/job/${JOB_NAME}/$BUILD_NUMBER/console"}'
"""
}
failure {
echo "Gauzy CI/CD pipeline failed..."
sh """
curl 'https://api.github.com/repos/ever-co/${REPO_NAME}/statuses/$GIT_COMMIT' -H 'Authorization: token ${GITHUB_TOKEN}' -H 'Content-Type: application/json' -X POST -d '{"state": "failure", "context": "Jenkins", "description": "Jenkins pipeline failed", "target_url": "https://$CI_URL/job/${JOB_NAME}/$BUILD_NUMBER/console"}'
"""
}
}
}