-
Notifications
You must be signed in to change notification settings - Fork 27
/
Jenkinsfile
121 lines (105 loc) · 4.99 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
def javaBullshifierVersion = ''
def agentVersion = ''
def fullTag = ''
def tagCheck = ''
def javaBullshifierTags = []
def dockerOptions= '--network=host'
def imageName = 'overops-java-bullshifier'
def localRepoPath = ('docker/' + imageName)
pipeline {
parameters {
booleanParam(name: 'FORCE_PUBLISH', defaultValue: false, description: 'Forces a build and publish')
string(name: 'VERSION', defaultValue: 'latest', description:'Build and publish a specific agent version. Note: Only Full version tag is published if not latest.')
booleanParam(name: 'PUBLISH_TO_AWS', defaultValue: true, description: 'Publish to AWS Registry')
}
environment {
awsRegCred = 'ecr:us-east-1:aws-takipi-dev-service'
registryCred = 'container-registry-build-guy'
gitCred = 'build-guy'
}
agent any
stages {
stage('Cloning Git') {
steps {
git([url: 'https://github.com/takipi/java-bullshifier.git', branch: 'master', credentialsId: gitCred ])
}
}
stage('Determine versions and tags') {
environment {
LOCAL_REGISTRY_CREDS = credentials("${registryCred}")
}
steps {
script{
// Determine the Java Bullshifier Version (Reads local VERSION file)
javaBullshifierVersion = sh(returnStdout: true, script: 'python3 ./scripts/version-support.py --get-version').trim()
// Determine the latest agent version and add latest tags. otherwise only use the agent parameter.
// Note: When setting the Agent Version as param only the full tag is pushed.
if ( params.AGENT_VERSION == 'latest' ) {
agentVersion = sh(returnStdout: true, script: 'python3 ./scripts/version-support.py --get-agent-version').trim()
javaBullshifierTags.add('latest')
javaBullshifierTags.add(javaBullshifierVersion)
} else {
agentVersion = params.AGENT_VERSION
}
// Add full unique tag i.e. 2.13-agent-4.54.0
fullTag = (javaBullshifierVersion + '-agent-' + agentVersion)
javaBullshifierTags.add(fullTag)
// Determine if the tag doesn't exists if not we should build and publish.
registryAPIEndpoint = (env.LOCAL_DOCKER_REGISTRY_URL + '/artifactory/api/docker/docker')
tagCheck = sh(returnStdout: true, script:"python3 ./scripts/version-support.py --check-docker-tag --tag ${fullTag} --repository ${imageName} --registry ${registryAPIEndpoint} --username \$LOCAL_REGISTRY_CREDS_USR --token \$LOCAL_REGISTRY_CREDS_PSW").trim()
}
}
}
stage('Build') {
when {
anyOf {
// Run Build if forced or if the tag does not exists.
expression { return params.FORCE_PUBLISH }
expression { tagCheck == 'false' }
}
}
steps {
script {
options = ''
// Build using the latest agent or one passed in as a parameter.
if ( params.AGENT_VERSION == 'latest' ) {
options = (dockerOptions + ' .')
} else {
options = ( dockerOptions + ' --build-arg VERSION=' + params.AGENT_VERSION + ' .')
}
// Note: "building" two images in order to have correct image name for each registry
// Second call is no-op but is done this way based on the docker plugin api limitations.
dockerImage = docker.build( localRepoPath, options )
awsDockerImage = docker.build( imageName, options )
}
}
}
stage('Publish Image') {
when {
anyOf {
// Run Build if forced or if the tag does not exists.
expression { return params.FORCE_PUBLISH }
expression { tagCheck == 'false' }
}
}
steps {
script {
docker.withRegistry(env.LOCAL_DOCKER_REGISTRY_URL, registryCred ) {
for(String tag in javaBullshifierTags) {
echo(tag)
dockerImage.push(tag)
}
}
// Publish image to private ECR
if (params.PUBLISH_TO_AWS) {
docker.withRegistry(env.AWS_TAKIPI_DEV_REGISTRY_URI, awsRegCred ) {
for(String tag in javaBullshifierTags) {
awsDockerImage.push(tag)
}
}
}
}
}
}
}
}