-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
193 lines (181 loc) · 7.11 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
181
182
183
184
185
186
187
188
189
190
191
192
193
library identifier: 'jenkins-libs@master', retriever: modernSCM(
[$class: 'GitSCMSource',
remote: 'https://github.com/pagaya/jenkins.git',
credentialsId: 'github-devops-services'])
def parallelStages = [:]
DEV_ACCOUNT_NUMBER = '039139996217'
TESTS_REPORTS_PATH = './junitxml_report.xml'
pipeline {
agent {
kubernetes {
defaultContainer 'builder'
yamlFile '.infra/jenkins-slave.yaml'
}
}
options {
timeout(time: 3, unit: 'HOURS')
ansiColor('xterm')
}
parameters {
booleanParam(name: 'FORCE_DEVELOPMENT_DEPLOY', defaultValue: false, description: 'force deploy to development environment')
booleanParam(name: 'BUILD_DOCKER_IMAGE', defaultValue: false, description: 'Build Docker image')
}
environment {
PYPI_CRED = credentials('pypi-user-pass')
AWS_DEFAULT_REGION = 'us-east-1'
CICD_ACCOUNT_NUMBER = '704102000649'
CICD_ACCOUNT_ROLE = 'JenkinsCrossAccountRole'
ECR_REPO_URL = 'dkr.ecr.us-east-1.amazonaws.com'
REPOSITORY = '704102000649.dkr.ecr.us-east-1.amazonaws.com/via'
REPO_NAME = 'via'
GH_TOKEN = credentials('jenkins-eng-github-token')
GIT_HASH = "${GIT_COMMIT[0..7]}"
SNYK_TOKEN = credentials('snyk-token')
APP_WORKDIR = "services/via-app/"
CODE_ARTIFCAT_REPO_URL = "https://pagaya-artifacts-704102000649.d.codeartifact.${AWS_DEFAULT_REGION}.amazonaws.com/pypi/Pagaya-Artifacts-prod/"
CODE_ARTIFACT_AUTH_TOKEN = ""
}
stages {
stage('Get CodeArtifact Token') {
steps {
script {
withAWS(roleAccount: DEV_ACCOUNT_NUMBER, role: CICD_ACCOUNT_ROLE) {
CODE_ARTIFACT_AUTH_TOKEN = sh(
returnStdout: true,
script: 'aws codeartifact get-authorization-token \
--domain pagaya-artifacts \
--domain-owner 704102000649 \
--region ${AWS_DEFAULT_REGION} \
--query authorizationToken \
--output text'
).trim()
PDM_PYPI_URL = "https://aws:$CODE_ARTIFACT_AUTH_TOKEN@pagaya-artifacts-704102000649.d.codeartifact.${AWS_DEFAULT_REGION}.amazonaws.com/pypi/Pagaya-Artifacts-prod/simple/"
}
}
}
}
stage('publish via-canonical package') {
when {
allOf {
branch "development"
changeset "libs/via-canonical/src/via_canonical_actions/_version.py"
}
}
steps {
publish_package_to_code_artifact("libs/via-canonical")
}
}
stage('Docker Build & Push') {
when {
anyOf {
environment name: 'BUILD_DOCKER_IMAGE', value: 'true'
branch "development"
}
}
steps {
script{
BRANCH_NORMALIZE = env.BRANCH_NAME.replace("/", "-")
if (env.BRANCH_NAME.contains("release")){
docker_tags = "${REPOSITORY}:${GIT_COMMIT[0..7]} -t ${REPOSITORY}:release_candidate -t ${REPOSITORY}:${BRANCH_NORMALIZE}"
}
else{
docker_tags = "${REPOSITORY}:${GIT_COMMIT[0..7]}"
}
}
sh """
echo "DOCKER TGAS ${docker_tags}"
aws ecr get-login-password --region ${AWS_DEFAULT_REGION} | docker login --username AWS --password-stdin ${CICD_ACCOUNT_NUMBER}.${ECR_REPO_URL}
docker build \
-t ${docker_tags} \
--build-arg=PDM_PYPI_URL=${PDM_PYPI_URL} \
--build-arg=COMMIT_HASH=${GIT_COMMIT[0..7]} ${APP_WORKDIR}
docker push ${REPOSITORY} --all-tags
"""
}
}
stage('Tests') {
steps {
tests("services/via-app")
}
}
stage('Develoment Deployment') {
when {
anyOf {
allOf{
environment name: 'BUILD_DOCKER_IMAGE', value: 'true'
environment name: 'FORCE_DEVELOPMENT_DEPLOY', value: 'true'
}
branch "development"
}
}
environment {
ENVIRONMENT = "development"
}
steps {
update_environment_image_tag()
}
}
}
post {
always {
publishHTML(target : [
allowMissing: true,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'unit-test-report',
reportFiles: 'index.html',
reportName: 'Unit Test Report',
reportTitles: 'Via Unit Test Report'])
publishHTML(target : [
allowMissing: true,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'pytest-report',
reportFiles: 'index.html',
reportName: 'Pytest Report',
reportTitles: 'Via Pytest Report'])
}
}
}
def update_environment_image_tag(){
dir ("${REPO_NAME}-infra"){
git credentialsId: 'jenkins-eng-github-token', url: 'https://github.com/pagaya/via-infra.git'
sh """
yq e -i '.via.image.tag = "${GIT_COMMIT[0..7]}"' helm/${REPO_NAME}/values-${ENVIRONMENT}.yaml
cat helm/${REPO_NAME}/values-${ENVIRONMENT}.yaml
git config --global url.'https://${GH_TOKEN}@github.com'.insteadOf https://github.com
git config --global --add safe.directory ${WORKSPACE}/${REPO_NAME}-infra
git config --global user.email "[email protected]"
git config --global user.name "Jenkins"
git commit -am "update ${ENVIRONMENT} image tag to ${GIT_COMMIT[0..7]}"
git push origin master
"""
}
}
def publish_package_to_code_artifact(package_path){
container("python3"){
dir("${package_path}") {
sh """
export PDM_PYPI_URL=$PDM_PYPI_URL
pip3 install --upgrade pip && pip3 install pdm
pdm install -L pdm.standalone.lock
pdm run checks:junit ${WORKSPACE}/publish-package-report
pdm publish --repository ${CODE_ARTIFCAT_REPO_URL} --username aws --password ${CODE_ARTIFACT_AUTH_TOKEN}
"""
}
}
}
def tests(app_path){
dir("${app_path}"){
container("python3"){
sh """
yum groupinstall -y 'Development Tools'
yum install -y libxslt-devel libxml2-devel
export PDM_PYPI_URL=$PDM_PYPI_URL
pip3 install pdm
pdm install -L pdm.standalone.lock
pdm run checks:junit ${WORKSPACE}/unit-test-report
"""
}
}
}