-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
97 lines (93 loc) · 2.6 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
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
'''
}
}
void githubRegistryLogin() {
sh '''echo $GITHUB_PAT_PSW | docker login $DOCKER_REPO -u $GITHUB_PAT_USR --password-stdin'''
}
pipeline {
agent { label 'sbt-template' }
environment {
GITHUB_PAT = credentials('github-pat')
NEXUS_CREDENTIALS = credentials('pdnd-nexus')
// GIT_URL has the shape git@github.com: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 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'
}
}
}
}
stage('Publish Docker Image on GitHub') {
when {
buildingTag()
}
environment {
DOCKER_REPO = "ghcr.io/pagopa"
}
steps {
container('sbt-container') {
script {
githubRegistryLogin()
sbtAction 'docker:publish'
}
}
}
}
}
post {
success {
updateGithubCommit 'success'
}
failure {
updateGithubCommit 'failure'
}
aborted {
updateGithubCommit 'error'
}
}
}