-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
127 lines (113 loc) · 3.04 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
def _message
pipeline {
agent {
kubernetes {
defaultContainer 'jnlp'
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
component: ci
spec:
serviceAccountName: jenkins-robot
containers:
- name: docker
image: google/cloud-sdk:368.0.0
command:
- cat
tty: true
volumeMounts:
- mountPath: /var/run/docker.sock
name: docker-sock
- name: kctl
image: alpine/k8s:1.21.2
command:
- cat
tty: true
volumes:
- name: docker-sock
hostPath:
path: /var/run/docker.sock
"""
}
}
triggers {
GenericTrigger(
genericVariables: [
[key: 'ref', value: '$.ref']
],
causeString: 'Triggered on $ref',
token: 'backend-deploy-ababcd',
tokenCredentialId: '',
printContributedVariables: true,
printPostContent: true,
silentResponse: false,
regexpFilterText: '$ref',
regexpFilterExpression: 'refs/heads/main'
)
}
options {
disableConcurrentBuilds()
timeout(time: 30, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '30'))
}
parameters {
string(name: '_git_repo', defaultValue: 'https://github.com/KuzmenkoAlexey/real-world-app-java.git')
string(name: '_git_branch', defaultValue: 'main' )
string(name: '_gcp_repo', defaultValue: 'gcr.io/data-buckeye-288515/onboarding-springboot')
string(name: '_namespace', defaultValue: 'kuzmenko-onboarding')
string(name: '_deployment_name', defaultValue: 'javaapp')
string(name: '_container_name', defaultValue: 'javaapp')
}
stages {
stage('Git checkout') {
steps {
script {
echo "Git checkout 1"
}
checkout([
$class: 'GitSCM',
userRemoteConfigs: [[credentialsId: "git", url: "${_git_repo}"]],
branches: [[name: "${_git_branch}"]]
])
script {
echo "Git checkout 2"
_git_commit = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
echo "Git Commit: ${_git_commit}"
}
}
}
stage('Build') {
steps {
container('docker') {
script {
_build_args = """\
--network=host \
"""
withCredentials([file(credentialsId: 'gcp_sa_key', variable: 'GC_KEY')]) {
sh("gcloud auth activate-service-account --key-file=${GC_KEY}")
sh("gcloud auth configure-docker")
def backendImage = docker.build("${_gcp_repo}:${_git_commit}", "${_build_args} .")
backendImage.push()
}
}
}
}
}
stage('Deploy') {
steps {
container('kctl') {
sh("kubectl -n ${_namespace} set image deployment/${_deployment_name} ${_container_name}=${_gcp_repo}:${_git_commit}")
}
}
}
}
post {
success {
script {
echo "success!!!"
}
}
}
}