-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfileDev
More file actions
205 lines (173 loc) · 7.09 KB
/
JenkinsfileDev
File metadata and controls
205 lines (173 loc) · 7.09 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def project = "yApp"
def major = 0
def minor = 0
def revision = 0
def appversion = "$major.$minor.$revision"
def amplify_env = ""
def cognito_pool = ""
def pool_id = ""
def cloudfront_id = ""
def awsProfile = "yApp"
def awsRegion = "us-east-2"
def containerId = ""
//environment name
def MAIN_ENV = "main"
def DEV_ENV = "dev"
def TEST_ENV = "test"
// app id
def AMPLIFY_APP_ID = "d34oy4u1icq89e"
def MAIN_CLOUDFRONT_ID = "E1J2AXX98R4GJP"
def DEV_CLOUDFRONT_ID = "E31QWRSIU8H00V"
//file path we need to update
def LAMBDA_PATH = "amplify/backend/function/yAppLambda/src/appSettings.json"
def GOLBAL_PATH = "amplify/backend/function/yAppLambda/src/global.json"
//cognito
def POOL_URL= "https://cognito-idp.us-east-2.amazonaws.com/"
def TEST_POOL = "us-east-2_DpPbDoSUa"
def DEV_POOL = "us-east-2_AoahD29kD"
def MAIN_POOL = "us-east-2_itXR8ULJ4"
//dotnet tests path
def SOLUTION = "amplify/backend/function/Lambdas.sln"
pipeline {
environment {
def label = null // TODO:Update later
def title = null // filled in later
AWS_SDK_LOAD_CONFIG = "1"
AWS_PROFILE = "default"
IMAGE_NAME = 'my-docker-image' // Replace with your image name
CONTAINER_NAME = 'my-docker-container' // Replace with your container name
//DOCKER_REGISTRY = 'my-registry' // Optional: if you are using a private registry
ACCESS_KEY_ID =""
SECRET_ACCESS_KEY =""
AWS_DEFAULT_REGION = "us-east-2"
APK_OUTPUT_PATH = "/app/yAppMobile/app/build/outputs/apk/debug/app-debug.apk" // Path to the APK inside the container
HOST_OUTPUT_DIR = "output" // Directory on the Jenkins host to store the APK
}
agent any
options {
timeout(time: 1, unit: "HOURS")
parallelsAlwaysFailFast()
disableConcurrentBuilds()
}
stages {
stage('Initialize environment') {
steps {
script {
echo 'Initializing Amplify'
amplify_env = DEV_ENV
pool_id = DEV_POOL
cloudfront_id = DEV_CLOUDFRONT_ID
cognito_pool = "${POOL_URL}${pool_id}"
}
}
}
stage('Update Version & App settings') {
steps {
script {
//To update the version number that display in the app
echo "Update lambda settings"
def jsonData = [
Environment: "dev",
AwsRegion: "us-east-2",
UserPoolId: "${pool_id}",
FriendshipTableName: "Friendship-${amplify_env}",
PostTableName: "Post-${amplify_env}",
CommentTableName: "Comment-${amplify_env}",
]
// Convert JSON data to string
def jsonString = groovy.json.JsonOutput.toJson(jsonData)
// Write JSON string to file
writeFile file: LAMBDA_PATH, text: jsonString
echo "Show the content of the file"
sh ("cat ${LAMBDA_PATH}")
//update awsconfiguration.json for jenkins
def sourceFile = 'yAppMobile/app/src/main/res/raw/amplifyconfigurationdev.json'
def destinationFile = 'yAppMobile/app/src/main/res/raw/amplifyconfiguration.json'
// Ensure the source file exists before attempting to read it
if (fileExists(sourceFile)) {
echo "Replacing content of ${destinationFile} with content from ${sourceFile}."
// Replace the content of the destination file with the source file content
sh(script: "cat ${sourceFile} > ${destinationFile}")
// Ensure the destination file exists after the replacement
if (fileExists(destinationFile)) {
echo "Content of ${destinationFile} replaced successfully."
} else {
error "Content of ${destinationFile} not replaced successfully."
}
sh ("cat ${destinationFile}")
} else {
error "Source file ${sourceFile} does not exist."
}
}
}
}
stage('Build Docker Image') {
steps {
script {
// Pass the AWS Access Key and other build arguments during Docker build
sh("docker build --build-arg AWS_ACCESS_KEY_ID=${ACCESS_KEY_ID} --build-arg AWS_SECRET_ACCESS_KEY=${SECRET_ACCESS_KEY} --build-arg AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} -t ${IMAGE_NAME} .")
}
}
}
stage('Run tests') {
steps {
script {
echo 'Running tests'
sh(script: "docker run ${IMAGE_NAME} dotnet test ${SOLUTION}", returnStatus: true)
// if (testStatus != 0) {
// error("Tests failed. Exiting...")
// }
}
}
}
stage('Initialize Amplify') {
steps {
script {
echo 'Initializing Amplify'
def amplify_project = "{\
\"projectName\":\"yApp\",\
\"envName\":\"${amplify_env}\",\
\"defaultEditor\":\"code\"\
}"
sh(script:"docker run ${IMAGE_NAME} sh -c 'ls'")
sh(script: "docker run ${IMAGE_NAME} sh -c 'chmod +x ./bashDev.sh && ./bashDev.sh'")
}
}
}
stage('Build Apk') {
steps {
script {
echo "Ensuring output directory exists..."
sh(script: "mkdir -p ${HOST_OUTPUT_DIR}")
echo "Creating Docker container to build APK..."
sh(script: "docker create --name ${CONTAINER_NAME} ${IMAGE_NAME} sh -c 'chmod +x ./getApk.sh && ./getApk.sh'")
echo "Starting the container..."
sh(script: "docker start -a ${CONTAINER_NAME}")
}
}
}
stage('Copy Apk') {
steps {
script {
echo "copying apk to host"
sh(script: "docker cp ${CONTAINER_NAME}:${APK_OUTPUT_PATH} ${HOST_OUTPUT_DIR}/app-dev-debug.apk")
sh(script: "docker rm ${CONTAINER_NAME}")
}
}
}
}
post {
always {
echo "Post run"
// to create a webhook for the github repo
}
success {
// Archive the APK file so it can be downloaded from Jenkins
archiveArtifacts artifacts: "${HOST_OUTPUT_DIR}/app-dev-debug.apk", allowEmptyArchive: true
}
cleanup {
echo "Clean up Post"
cleanWs()
}
}
}