141 lines
4.6 KiB
Groovy
141 lines
4.6 KiB
Groovy
def REGISTRY_URL = 'docker.io'
|
|
def REPO_NAME = 'zhenyus'
|
|
def IMAGE_NAME = 'magicleaps'
|
|
def APP_NAME = 'magicleaps'
|
|
def TAG_PREFIX = 'snapshot'
|
|
def DOCKER_REGISTRY_SECRET = 'kaniko-secret'
|
|
|
|
pipeline {
|
|
agent any
|
|
|
|
stages {
|
|
stage('Checkout') {
|
|
steps {
|
|
git credentialsId: 'freeleaps-azure-dev', url: 'https://freeleaps@dev.azure.com/freeleaps/magicleaps/_git/magicleaps'
|
|
}
|
|
}
|
|
|
|
stage('Set Commit Hash') {
|
|
steps {
|
|
script {
|
|
def commitHash = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
|
|
def shortCommitHash = commitHash.take(7)
|
|
env.COMMIT_HASH = shortCommitHash
|
|
echo "Commit Hash: ${env.COMMIT_HASH}"
|
|
env.TRIGGERED_BRANCH = "${GIT_BRANCH}"
|
|
echo "Triggered Branch: ${env.TRIGGERED_BRANCH}"
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Build Docker Images For Each Components') {
|
|
matrix {
|
|
axes {
|
|
axis {
|
|
name 'COMPONENT'
|
|
values 'backend', 'frontend'
|
|
}
|
|
axis {
|
|
name 'ARCH'
|
|
values 'linux/amd64'
|
|
}
|
|
}
|
|
|
|
agent {
|
|
kubernetes {
|
|
defaultContainer 'kaniko'
|
|
yaml """
|
|
apiVersion: v1
|
|
kind: Pod
|
|
metadata:
|
|
labels:
|
|
freeleaps-devops-job: magicleaps-app-build
|
|
freeleaps-devops-app: magicleaps
|
|
spec:
|
|
containers:
|
|
- name: kaniko
|
|
image: gcr.io/kaniko-project/executor:debug
|
|
command:
|
|
- cat
|
|
tty: true
|
|
volumeMounts:
|
|
- name: kaniko-secret
|
|
mountPath: /kaniko/.docker/config.json
|
|
subPath: .dockerconfigjson
|
|
- name: workspace
|
|
mountPath: /workspace
|
|
volumes:
|
|
- name: kaniko-secret
|
|
secret:
|
|
secretName: kaniko-secret
|
|
- name: workspace
|
|
emptyDir: {}
|
|
"""
|
|
}
|
|
}
|
|
|
|
stages {
|
|
stage('Image Building') {
|
|
steps {
|
|
script {
|
|
def dockerfilePath = "${COMPONENT}/Dockerfile"
|
|
def arch = "${ARCH}"
|
|
def archTag = arch.replace('/', '-')
|
|
def targetImage = "${REGISTRY_URL}/${REPO_NAME}/${IMAGE_NAME}:${COMPONENT}-${TAG_PREFIX}-${COMMIT_HASH}-${archTag}"
|
|
echo "Building Docker image ${targetImage}..."
|
|
|
|
sh """
|
|
/kaniko/executor \
|
|
--dockerfile=${dockerfilePath} \
|
|
--context=${COMPONENT} \
|
|
--destination=${targetImage} \
|
|
--custom-platform=${ARCH} \
|
|
--skip-tls-verify=true \
|
|
--ignore-path=/product_uuid
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Deploy with Argo CD') {
|
|
stages {
|
|
stage('Clone GitOps Manifests Repo') {
|
|
steps {
|
|
git credentialsId: 'freeleaps-azure-dev', url: 'https://freeleaps@dev.azure.com/freeleaps/freeleaps-ops/_git/freeleaps-ops'
|
|
}
|
|
}
|
|
|
|
stage('Automate Update Application Image Tag') {
|
|
steps {
|
|
script {
|
|
def triggeredBranch = "${TRIGGERED_BRANCH}"
|
|
if (triggeredBranch == 'origin/master') {
|
|
echo "Triggered branch is master, deploying to alpha..."
|
|
def valuesFile = APP_NAME + '/helm-pkg/' + APP_NAME + '/values.alpha.yaml'
|
|
def data = readYaml (file: valuesFile)
|
|
data.backend.image.tag = "backend-${TAG_PREFIX}-${env.COMMIT_HASH}-linux-amd64"
|
|
data.frontend.image.tag = "frontend-${TAG_PREFIX}-${env.COMMIT_HASH}-linux-amd64"
|
|
writeYaml file: valuesFile, data: data, overwrite: true
|
|
// git push
|
|
withCredentials([string(credentialsId: 'freeleaps-azure-dev-token-only', variable: 'GIT_CREDENTIALS')]) {
|
|
sh """
|
|
git config user.name "zhenyus"
|
|
git config user.email "zhenyus@mathmast.com"
|
|
git remote add ci_origin https://zhenyus:${GIT_CREDENTIALS}@dev.azure.com/freeleaps/freeleaps-ops/_git/freeleaps-ops
|
|
git add ${valuesFile}
|
|
git commit -m "ci(bot-auto-bump): bump ${APP_NAME} image tags for alpha to ${TAG_PREFIX}-${env.COMMIT_HASH}-linux-amd64"
|
|
git push ci_origin HEAD:master
|
|
"""
|
|
}
|
|
echo "Update ${APP_NAME} image tags for alpha to ${TAG_PREFIX}-${env.COMMIT_HASH}-linux-amd64."
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |