-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
81 lines (78 loc) · 2.31 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
void sbtAction(String task) {
sh '''
echo "
realm=Sonatype Nexus Repository Manager
host=${NEXUS}
user=${NEXUS_CREDENTIALS_USR}
password=${NEXUS_CREDENTIALS_PSW}" > ~/.sbt/.credentials
'''
sh "sbt -Dsbt.log.noformat=true ${task}"
}
void updateGithubCommit(String status) {
def token = '${GITHUB_PAT_PSW}'
sh """
curl --silent --show-error \
"https://api.github.com/repos/pagopa/${REPO_NAME}/statuses/${GIT_COMMIT}" \
--header "Content-Type: application/json" \
--header "Authorization: token ${token}" \
--request POST \
--data "{\\"state\\": \\"${status}\\",\\"context\\": \\"Jenkins Continuous Integration\\", \\"description\\": \\"Build ${BUILD_DISPLAY_NAME}\\"}" &> /dev/null
"""
}
void ecrLogin() {
withCredentials([usernamePassword(credentialsId: 'ecr-rw', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY')]) {
sh '''
aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin $DOCKER_REPO
'''
}
}
pipeline {
agent { label 'sbt-template' }
environment {
NEXUS = "${env.NEXUS}"
DOCKER_REPO = "${env.DOCKER_REPO}"
MAVEN_REPO = "${env.MAVEN_REPO}"
GITHUB_PAT = credentials('github-pat')
NEXUS_CREDENTIALS = credentials('pdnd-nexus')
ECR_RW = credentials('ecr-rw')
// GIT_URL has the shape [email protected]:pagopa/REPO_NAME.git so we extract from it
REPO_NAME="""${sh(returnStdout:true, script: 'echo ${GIT_URL} | sed "s_https://github.com/pagopa/\\(.*\\)\\.git_\\1_g"')}""".trim()
}
stages {
stage('Test') {
steps {
container('sbt-container') {
updateGithubCommit 'pending'
sbtAction 'test'
}
}
}
stage('Publish Client on Nexus and Docker Image on ECR') {
when {
anyOf {
branch pattern: "[0-9]+\\.[0-9]+\\.x", comparator: "REGEXP"
buildingTag()
}
}
steps {
container('sbt-container') {
script {
ecrLogin()
sbtAction 'docker:publish "project client" publish'
}
}
}
}
}
post {
success {
updateGithubCommit 'success'
}
failure {
updateGithubCommit 'failure'
}
aborted {
updateGithubCommit 'error'
}
}
}