87 lines
3.0 KiB
Groovy
87 lines
3.0 KiB
Groovy
pipeline {
|
|
agent { label 'static-agent' }
|
|
|
|
environment {
|
|
HARBOR_REGISTRY = 'harbor.myriadcara.com'
|
|
HARBOR_PROJECT = 'library'
|
|
IMAGE_NAME = 'nodejs-app'
|
|
GITEA_HOST = 'gitea.myriadcara.com'
|
|
GITOPS_REPO = 'nodejs-app-gitops'
|
|
GITEA_USER = 'subodh'
|
|
}
|
|
|
|
stages {
|
|
|
|
stage('Checkout') {
|
|
steps {
|
|
checkout scm
|
|
}
|
|
}
|
|
|
|
stage('Build Image') {
|
|
steps {
|
|
// We use Kaniko to build the image safely inside Kubernetes
|
|
sh '''
|
|
docker run --rm \
|
|
-v .:/workspace \
|
|
-v /home/jenkins/.docker/config.json:/kaniko/.docker/config.json \
|
|
gcr.io/kaniko-project/executor:latest \
|
|
--dockerfile /workspace/Dockerfile \
|
|
--context /workspace \
|
|
--destination harbor.myriadcara.com/library/nodejs-app:1 \
|
|
--destination harbor.myriadcara.com/library/nodejs-app:latest
|
|
'''
|
|
}
|
|
}
|
|
|
|
stage('Push to Harbor') {
|
|
steps {
|
|
withCredentials([usernamePassword(
|
|
credentialsId: 'harbor-credentials',
|
|
usernameVariable: 'HARBOR_USER',
|
|
passwordVariable: 'HARBOR_PASS'
|
|
)]) {
|
|
sh """
|
|
docker login ${HARBOR_REGISTRY} \
|
|
-u ${HARBOR_USER} \
|
|
-p ${HARBOR_PASS}
|
|
docker push ${HARBOR_REGISTRY}/${HARBOR_PROJECT}/${IMAGE_NAME}:${BUILD_NUMBER}
|
|
docker push ${HARBOR_REGISTRY}/${HARBOR_PROJECT}/${IMAGE_NAME}:latest
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Update GitOps Repo') {
|
|
steps {
|
|
withCredentials([usernamePassword(
|
|
credentialsId: 'gitea-subodh',
|
|
usernameVariable: 'GITEA_USER_CRED',
|
|
passwordVariable: 'GITEA_PASS'
|
|
)]) {
|
|
sh """
|
|
rm -rf gitops-tmp
|
|
git clone https://${GITEA_USER_CRED}:${GITEA_PASS}@${GITEA_HOST}/${GITEA_USER}/${GITOPS_REPO}.git gitops-tmp
|
|
cd gitops-tmp && \
|
|
sed -i 's|image: ${HARBOR_REGISTRY}/${HARBOR_PROJECT}/${IMAGE_NAME}:.*|image: ${HARBOR_REGISTRY}/${HARBOR_PROJECT}/${IMAGE_NAME}:${BUILD_NUMBER}|' k8s/deployment.yaml && \
|
|
git config user.email "jenkins@ci.local" && \
|
|
git config user.name "Jenkins" && \
|
|
git add k8s/deployment.yaml && \
|
|
git commit -m "Update image tag to build ${BUILD_NUMBER}" && \
|
|
git push
|
|
rm -rf gitops-tmp
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
post {
|
|
success {
|
|
echo "Pipeline succeeded - ArgoCD will sync the new version"
|
|
}
|
|
failure {
|
|
echo "Pipeline failed"
|
|
}
|
|
}
|
|
} |