[DO-1519] add messenger master web host and some refactoring (!79)
Co-authored-by: Rustam Tagaev <rustam.tagaev@avroid.tech> Reviewed-on: https://git.avroid.tech/DevOps/jenkins-pipelines/pulls/79 Reviewed-by: Denis Patrakeev <denis.patrakeev@avroid.team>
This commit is contained in:
@@ -34,13 +34,13 @@ import java.util.regex.Pattern
|
||||
def createGetHttpClient(String url, String jenkinsCreds) {
|
||||
def jenkinsCredentials = CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class)
|
||||
def credentials = jenkinsCredentials.findResult { it.id == jenkinsCreds ? it : null }
|
||||
String auth = credentials.username + ":" + credentials.password;
|
||||
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes("utf-8"));
|
||||
String authHeaderValue = "Basic " + encodedAuth;
|
||||
String auth = 'robot' + '\$' + 'ci' + ":" + credentials.password
|
||||
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes("utf-8"))
|
||||
String authHeaderValue = "Basic " + encodedAuth
|
||||
def httpClient = new URL(url).openConnection() as HttpURLConnection
|
||||
httpClient.setRequestMethod('GET');
|
||||
httpClient.setRequestProperty("Authorization", authHeaderValue);
|
||||
httpClient.setRequestProperty("Accept", "application/json");
|
||||
httpClient.setRequestMethod('GET')
|
||||
httpClient.setRequestProperty("Authorization", authHeaderValue)
|
||||
httpClient.setRequestProperty("Accept", "application/json")
|
||||
return httpClient
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
pipelineJob('Cloud/Deploy/Frontend/web-cloud-messenger-host-deploy') {
|
||||
logRotator {
|
||||
numToKeep(10)
|
||||
artifactNumToKeep(10)
|
||||
}
|
||||
|
||||
parameters {
|
||||
choice {
|
||||
name('ENV')
|
||||
choices(['DEV'])
|
||||
description('Select one of environments')
|
||||
}
|
||||
choiceParameter {
|
||||
name('APP_VERSION')
|
||||
randomName('')
|
||||
filterable(true)
|
||||
filterLength(1)
|
||||
script {
|
||||
groovyScript {
|
||||
fallbackScript {
|
||||
script('')
|
||||
sandbox(false)
|
||||
}
|
||||
script {
|
||||
sandbox(false)
|
||||
script(
|
||||
"""
|
||||
import groovy.json.JsonSlurperClassic
|
||||
import groovy.json.model.*
|
||||
import com.cloudbees.plugins.credentials.CredentialsProvider
|
||||
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials
|
||||
import java.util.regex.Pattern
|
||||
|
||||
def createGetHttpClient(String url, String jenkinsCreds) {
|
||||
def jenkinsCredentials = CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class)
|
||||
def credentials = jenkinsCredentials.findResult { it.id == jenkinsCreds ? it : null }
|
||||
String auth = 'robot' + '\$' + 'ci' + ":" + credentials.password;
|
||||
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes("utf-8"))
|
||||
String authHeaderValue = "Basic " + encodedAuth
|
||||
def httpClient = new URL(url).openConnection() as HttpURLConnection
|
||||
httpClient.setRequestMethod('GET')
|
||||
httpClient.setRequestProperty("Authorization", authHeaderValue)
|
||||
httpClient.setRequestProperty("Accept", "application/json")
|
||||
return httpClient
|
||||
}
|
||||
|
||||
String harborApiUrl = "https://${JENKINS_DOCKER_REGISTRY}/api/v2.0/projects/cloud/repositories/web-cloud-messenger-host/" +
|
||||
"artifacts?page=1&page_size=100&with_tag=true&sort=-push_time"
|
||||
|
||||
def httpClientHarbor = createGetHttpClient(harborApiUrl, "${JENKINS_HARBOR_CREDENTIALS}")
|
||||
httpClientHarbor.connect()
|
||||
|
||||
List imageVersions = []
|
||||
def harborResponse = new JsonSlurperClassic().parseText(httpClientHarbor.inputStream.text)
|
||||
|
||||
harborResponse.each { image ->
|
||||
image.tags.each { tag ->
|
||||
imageVersions.add(tag.name)
|
||||
}
|
||||
}
|
||||
|
||||
return imageVersions
|
||||
"""
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
choiceType('PT_SINGLE_SELECT')
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
definition {
|
||||
cpsScm {
|
||||
scm {
|
||||
git {
|
||||
remote {
|
||||
url("${JENKINS_GIT_REPOSITORY_URL}/DevOps/jenkins-pipelines.git")
|
||||
credentials("${JENKINS_GIT_CREDENTIALS_HTTP}")
|
||||
}
|
||||
branch('master')
|
||||
}
|
||||
}
|
||||
scriptPath('pipelines/Cloud/Deploy/Frontend/web-cloud-messenger-host-deploy.groovy')
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,13 +39,13 @@ import java.util.regex.Pattern
|
||||
def createGetHttpClient(String url, String jenkinsCreds) {
|
||||
def jenkinsCredentials = CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class)
|
||||
def credentials = jenkinsCredentials.findResult { it.id == jenkinsCreds ? it : null }
|
||||
String auth = credentials.username + ":" + credentials.password;
|
||||
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes("utf-8"));
|
||||
String authHeaderValue = "Basic " + encodedAuth;
|
||||
String auth = 'robot' + '\$' + 'ci' + ":" + credentials.password
|
||||
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes("utf-8"))
|
||||
String authHeaderValue = "Basic " + encodedAuth
|
||||
def httpClient = new URL(url).openConnection() as HttpURLConnection
|
||||
httpClient.setRequestMethod('GET');
|
||||
httpClient.setRequestProperty("Authorization", authHeaderValue);
|
||||
httpClient.setRequestProperty("Accept", "application/json");
|
||||
httpClient.setRequestMethod('GET')
|
||||
httpClient.setRequestProperty("Authorization", authHeaderValue)
|
||||
httpClient.setRequestProperty("Accept", "application/json")
|
||||
return httpClient
|
||||
}
|
||||
|
||||
@@ -82,22 +82,6 @@ String helmChart = 'msg-messenger-core-api'
|
||||
String helmRepoPath = "avroid/${helmChart}"
|
||||
String helmRepo = "${env.JENKINS_NEXUS_URL}/repository/avroid-charts"
|
||||
|
||||
|
||||
Map configuration = [
|
||||
vaultUrl: env.JENKINS_VAULT_URL,
|
||||
vaultCredentialId: "${JENKINS_VAULT_TOKEN}",
|
||||
engineVersion: 2
|
||||
]
|
||||
|
||||
List dockerCreds = [
|
||||
[path: 'team-devops/services/ci-cd/Jenkins/jenkins.avroid.tech', engineVersion: 2,
|
||||
secretValues:
|
||||
[
|
||||
[vaultKey: 'k8s_jenkins_deploy_token'],
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
Git git = new Git(this, env.JENKINS_GIT_CREDENTIALS_SSH)
|
||||
PodTemplates slaveTemplates = new PodTemplates(this, env.JENKINS_DOCKER_REGISTRY,
|
||||
["${env.JENKINS_K8S_HARBOR_SECRET}"],
|
||||
@@ -107,7 +91,6 @@ slaveTemplates.jnlp {
|
||||
slaveTemplates.helm {
|
||||
try {
|
||||
node(POD_LABEL){
|
||||
withVault([configuration: configuration, vaultSecrets: dockerCreds]) {
|
||||
stage('get repo with values'){
|
||||
gitVars = git.clone([urlRepo: "${env.JENKINS_GIT_REPOSITORY_SSH_URL}/${repoPath}",
|
||||
branch: branch])
|
||||
@@ -125,7 +108,6 @@ slaveTemplates.jnlp {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(err) {
|
||||
errorMessage = err.getMessage()
|
||||
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
@Library('shared-lib') _
|
||||
|
||||
import tech.avroid.kube.PodTemplates
|
||||
import tech.avroid.scm.Git
|
||||
import tech.avroid.jenkins.Notifications
|
||||
import tech.avroid.jenkins.Jenkins
|
||||
|
||||
String projectName = 'cloud'
|
||||
String repositoryName = 'web-cloud-messenger-host'
|
||||
String k8sAppName = repositoryName
|
||||
String pageSize = 100
|
||||
String apiUrl = "https://${JENKINS_DOCKER_REGISTRY}/api/v2.0/projects/${projectName}" +
|
||||
"/repositories/${repositoryName}/artifacts"
|
||||
properties([
|
||||
buildDiscarder(logRotator(artifactDaysToKeepStr: '',
|
||||
artifactNumToKeepStr: '10',
|
||||
daysToKeepStr: '',
|
||||
numToKeepStr: '10')),
|
||||
disableConcurrentBuilds(),
|
||||
parameters([
|
||||
choice(
|
||||
name: 'ENV',
|
||||
choices: ['DEV'],
|
||||
description: 'Select one of environments'
|
||||
),
|
||||
[$class: 'ChoiceParameter',
|
||||
choiceType: 'PT_SINGLE_SELECT',
|
||||
filterLength: 1,
|
||||
filterable: true,
|
||||
name: 'APP_VERSION',
|
||||
script: [$class: 'GroovyScript',
|
||||
script: [sandbox: false, script: """
|
||||
import groovy.json.JsonSlurperClassic
|
||||
import groovy.json.model.*
|
||||
import com.cloudbees.plugins.credentials.CredentialsProvider
|
||||
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials
|
||||
import java.util.regex.Pattern
|
||||
|
||||
def createGetHttpClient(String url, String jenkinsCreds) {
|
||||
def jenkinsCredentials = CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class)
|
||||
def credentials = jenkinsCredentials.findResult { it.id == jenkinsCreds ? it : null }
|
||||
String auth = 'robot' + '\$' + 'ci' + ":" + credentials.password
|
||||
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes("utf-8"))
|
||||
String authHeaderValue = "Basic " + encodedAuth
|
||||
def httpClient = new URL(url).openConnection() as HttpURLConnection
|
||||
httpClient.setRequestMethod('GET')
|
||||
httpClient.setRequestProperty("Authorization", authHeaderValue)
|
||||
httpClient.setRequestProperty("Accept", "application/json")
|
||||
return httpClient
|
||||
}
|
||||
|
||||
String harborApiUrl = "https://${JENKINS_DOCKER_REGISTRY}/api/v2.0/projects/cloud/repositories/web-cloud-messenger-host/" +
|
||||
"artifacts?page=1&page_size=100&with_tag=true&sort=-push_time"
|
||||
|
||||
def httpClientHarbor = createGetHttpClient(harborApiUrl, "${JENKINS_HARBOR_CREDENTIALS}")
|
||||
httpClientHarbor.connect()
|
||||
|
||||
List imageVersions = []
|
||||
def harborResponse = new JsonSlurperClassic().parseText(httpClientHarbor.inputStream.text)
|
||||
|
||||
harborResponse.each { image ->
|
||||
image.tags.each { tag ->
|
||||
imageVersions.add(tag.name)
|
||||
}
|
||||
}
|
||||
|
||||
return imageVersions
|
||||
"""]],
|
||||
]
|
||||
])
|
||||
|
||||
])
|
||||
|
||||
|
||||
String repoPath = 'Apps-Frontend/web-cloud-messenger-host.git'
|
||||
String branch = 'develop'
|
||||
String valuesPath = ".helm/values.${params.ENV.toLowerCase()}.yaml"
|
||||
String namespace = "tavro-cloud-${params.ENV.toLowerCase()}"
|
||||
|
||||
String helmChart = 'web-cloud-messenger-host'
|
||||
String helmRepoPath = "avroid/${helmChart}"
|
||||
String helmRepo = "${env.JENKINS_NEXUS_URL}/repository/avroid-charts"
|
||||
|
||||
Git git = new Git(this, env.JENKINS_GIT_CREDENTIALS_SSH)
|
||||
PodTemplates slaveTemplates = new PodTemplates(this, env.JENKINS_DOCKER_REGISTRY,
|
||||
["${env.JENKINS_K8S_HARBOR_SECRET}"],
|
||||
'avroid-office')
|
||||
|
||||
slaveTemplates.jnlp {
|
||||
slaveTemplates.helm {
|
||||
try {
|
||||
node(POD_LABEL){
|
||||
stage('get repo with values'){
|
||||
gitVars = git.clone([urlRepo: "${env.JENKINS_GIT_REPOSITORY_SSH_URL}/${repoPath}",
|
||||
branch: branch])
|
||||
}
|
||||
container('helm'){
|
||||
stage('deploy'){
|
||||
echo "Deploying version ${env.APP_VERSION} to ${namespace} namespace"
|
||||
sh """#!/bin/sh
|
||||
helm repo add avroid ${helmRepo}
|
||||
helm -n ${namespace} upgrade -f ${valuesPath} \
|
||||
--set image.tag=${env.APP_VERSION} \
|
||||
--install ${k8sAppName} ${helmRepoPath} \
|
||||
--wait
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(err) {
|
||||
errorMessage = err.getMessage()
|
||||
|
||||
println 'ERROR: ' + errorMessage
|
||||
|
||||
currentBuild.result = 'FAILURE'
|
||||
|
||||
String currentBuildUser = Jenkins.GetCurrentBuildUser(script: this)
|
||||
String emailSubject = "${currentBuild.currentResult}. " +
|
||||
"Pipeline task: ${currentBuild.fullDisplayName}"
|
||||
|
||||
Notifications.email(
|
||||
script: this,
|
||||
subject: emailSubject,
|
||||
errorString: errorMessage,
|
||||
recipientProviders: [],
|
||||
to: "${currentBuildUser}@avroid.team"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user