Files
2026-09-17 17:07:33 +08:00

132 lines
4.5 KiB
Groovy

pipeline {
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: jnlp
image: jenkins/inbound-agent:latest
env: [{name: GIT_SSL_CAINFO, value: /certs/otone-ca.crt}]
volumeMounts: [{name: certs, mountPath: /certs}]
resources: {requests: {memory: 128Mi}, limits: {memory: 512Mi}}
- name: maven
image: harbor.otone.run/devops/agent-maven:latest
resources: {requests: {memory: 256Mi}, limits: {memory: 1Gi}}
- name: gradle
image: harbor.otone.run/devops/agent-gradle:latest
resources: {requests: {memory: 256Mi}, limits: {memory: 1Gi}}
- name: node
image: harbor.otone.run/devops/agent-node:latest
resources: {requests: {memory: 256Mi}, limits: {memory: 1Gi}}
- name: kaniko
image: harbor.otone.run/devops/agent-kaniko:latest
resources: {requests: {memory: 256Mi}, limits: {memory: 1Gi}}
- name: kubectl
image: harbor.otone.run/devops/agent-kubectl:latest
resources: {requests: {memory: 128Mi}, limits: {memory: 256Mi}}
volumes:
- name: certs
configMap: {name: otone-ca}
'''
}
}
options { timestamps(); disableConcurrentBuilds() }
environment { TAG = "b${env.BUILD_NUMBER}" }
stages {
stage('Checkout') {
steps { checkout scm }
}
stage('Maven') {
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId: 'nexus-creds', usernameVariable: 'NU', passwordVariable: 'NP')]) {
sh '''#!/bin/sh -e
cat > settings.xml <<EOF
<settings>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>https://nexus.otone.run/repository/maven-central/</url>
</mirror>
</mirrors>
<servers>
<server>
<id>nexus-releases</id>
<username>${NU}</username>
<password>${NP}</password>
</server>
</servers>
</settings>
EOF
cd backend-maven
mvn -s ../settings.xml -B -q -Drevision=1.0.$BUILD_NUMBER deploy
'''
}
}
}
}
stage('Gradle') {
steps {
container('gradle') {
withCredentials([usernamePassword(credentialsId: 'nexus-creds', usernameVariable: 'NU', passwordVariable: 'NP')]) {
sh 'cd backend-gradle && gradle --no-daemon -q publish -PnexusUser=$NU -PnexusPass=$NP'
}
}
}
}
stage('Frontend') {
steps {
container('node') {
withCredentials([usernamePassword(credentialsId: 'nexus-creds', usernameVariable: 'NU', passwordVariable: 'NP')]) {
sh '''#!/bin/sh -e
cd frontend
echo "registry=https://nexus.otone.run/repository/npm-proxy/" > .npmrc
npm ci
npm run build
cd pkg
npm version 1.0.$BUILD_NUMBER --allow-same-version --no-git-tag-version
printf '//nexus.otone.run/repository/npm-hosted/:_auth=%s\nregistry=https://nexus.otone.run/repository/npm-hosted/\n' "$(printf '%s:%s' "$NU" "$NP" | base64)" > .npmrc
npm publish
'''
}
}
}
}
stage('Image') {
steps {
container('kaniko') {
withCredentials([usernamePassword(credentialsId: 'harbor-creds', usernameVariable: 'HU', passwordVariable: 'HP')]) {
sh '''#!/busybox/sh -e
mkdir -p /kaniko/.docker
printf '{"auths":{"harbor.otone.run":{"auth":"%s"}}}' "$(printf '%s:%s' "$HU" "$HP" | base64 | tr -d '\n')" > /kaniko/.docker/config.json
/kaniko/executor --context $WORKSPACE --dockerfile docker/backend.Dockerfile --destination harbor.otone.run/otone/demo-api:$TAG --cleanup
/kaniko/executor --context $WORKSPACE --dockerfile docker/frontend.Dockerfile --destination harbor.otone.run/otone/demo-web:$TAG --cleanup
'''
}
}
}
}
stage('Deploy') {
steps {
container('kubectl') {
withCredentials([file(credentialsId: 'kubeconfig', variable: 'KC')]) {
sh 'sed "s/__TAG__/$TAG/g" k8s.yaml | kubectl --kubeconfig $KC apply -f -'
}
}
}
}
stage('Verify') {
steps {
container('maven') {
retry(10) {
sh 'sleep 6; curl -sf https://demo.otone.run/ >/dev/null && curl -sf https://demo-api.otone.run/ >/dev/null && echo ALL-GREEN'
}
}
}
}
}
}