-
Notifications
You must be signed in to change notification settings - Fork 4
/
Jenkinsfile
140 lines (110 loc) · 5.95 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
//////////////////////////////////////////////////////////////////////////////////////////
def withSecretEnv(List<Map> varAndPasswordList, Closure closure) {
wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: varAndPasswordList]) {
withEnv(varAndPasswordList.collect { "${it.var}=${it.password}" }) {
closure()
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////
node {
properties([[$class: 'BuildDiscarderProperty',
strategy: [$class: 'LogRotator',
numToKeepStr: '7',
daysToKeepStr: '7',
artifactNumToKeepStr: '7',
artifactDaysToKeepStr: '7']]])
dir("${JENKINS_HOME}/workspace/${JOB_NAME}/${BUILD_NUMBER}") {
env.PROJECT_NAME = 'polymesh-portal'
env.GIT_BRANCH = env.BRANCH_NAME
env.VAULT_ADDR = 'https://127.0.0.1:8200/'
env.VAULT_CACERT = '/etc/ssl/certs/vault.tls.chain.pem'
env.GIT_REPO = "ssh://[email protected]:4444/Deployment/${PROJECT_NAME}.git"
if (env.CHANGE_BRANCH) {
env.GIT_BRANCH = env.CHANGE_BRANCH // Job was started from a pull request
}
withCredentials([
usernamePassword(credentialsId: 'vault_approle',
usernameVariable: 'VAULT_ROLE_ID',
passwordVariable: 'VAULT_SECRET_ID'),
]){
withSecretEnv([[var: 'VAULT_TOKEN',
password: sh (returnStdout: true,
label: 'Login To Vault',
script: '''\
vault write -field=token \
-format=table \
auth/approle/login \
role_id="$VAULT_ROLE_ID" \
secret_id="$VAULT_SECRET_ID"
'''.stripIndent()).trim()]]) {
stage('Clone Repo') {
sh (label: 'Clone Repo',
script: '''\
#!/bin/bash
set -eu -o pipefail
umask 0077 # mode: 0600
cleanup() {
{ shred ssh.json; sync; rm -f ssh.json; } || true
{ shred id_ed25519; sync; rm -f id_ed25519; } || true
{ shred id_ed25519-cert.pub; sync; rm -f id_ed25519-cert.pub; } || true
}
trap 'excode=$?; cleanup; trap - EXIT; exit $excode' EXIT HUP INT QUIT PIPE TERM
printf '{
"key_type": "ed25519",
"key_bits": 0,
"ttl": "1h",
"valid_principals": "jenkins",
"cert_type": "user",
"key_id": "",
"critical_options": {
},
"extensions": {
}
}' | vault write -format=json ssh/issue/jenkins - > ssh.json
jq -r .data.private_key ssh.json > id_ed25519
jq -r .data.signed_key ssh.json > id_ed25519-cert.pub
export GIT_SSH_COMMAND=ssh
export GIT_SSH_COMMAND+=' -o IdentitiesOnly=yes'
export GIT_SSH_COMMAND+=' -o StrictHostKeyChecking=yes'
export GIT_SSH_COMMAND+=' -o CertificateFile=id_ed25519-cert.pub'
export GIT_SSH_COMMAND+=' -o IdentityFile=id_ed25519'
umask 0022 # mode: 0644
git clone \
--depth=1 \
--single-branch \
--branch "$GIT_BRANCH" \
"$GIT_REPO"
'''.stripIndent())
}
}
}
dir("${PROJECT_NAME}") {
env.AWS_ACCOUNT_NUMBER = env.AWS_ACCOUNT_NUMBER_DATACENTER_PRIMARY
env.AWS_REGION = env.AWS_REGION_DATACENTER_PRIMARY
env.AWS_DEFAULT_REGION = env.AWS_REGION
env.CONTAINER_REGISTRY = "${env.AWS_ACCOUNT_NUMBER}.dkr.ecr.${AWS_REGION}.amazonaws.com"
env.GIT_COMMIT = sh (returnStdout: true,
label: 'Read Git Commit',
script: 'git rev-parse HEAD').trim()
echo "GIT_COMMIT: ${env.GIT_COMMIT}"
stage('Build') {
sh (label: 'Build`',
script: '''\
#!/bin/bash
docker build -f Dockerfile -t "${CONTAINER_REGISTRY}/polymesh/portal:${GIT_COMMIT}" .
''')
}
stage('Push') {
sh (label: 'Push',
script: '''\
#!/bin/bash
aws ecr get-login-password | \
docker login "$CONTAINER_REGISTRY" --username AWS --password-stdin
docker push "${CONTAINER_REGISTRY}/polymesh/portal:${GIT_COMMIT}" || true
''')
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////