修改jenkins流水线

This commit is contained in:
qiaoxinjiu
2026-04-13 19:49:09 +08:00
parent 3057e557b9
commit 40b6a3f4b9

View File

@@ -12,13 +12,16 @@ pipeline {
GIT_URL = 'https://wdxz-gitea.best-envision.com/qiaoxinjiu/effekt-interface-frontend.git' GIT_URL = 'https://wdxz-gitea.best-envision.com/qiaoxinjiu/effekt-interface-frontend.git'
GIT_BRANCH = 'master' GIT_BRANCH = 'master'
REPO_HOST = '39.170.26.156:8443'
REGISTRY = '39.170.26.156:8443' REGISTRY = '39.170.26.156:8443'
HARBOR_PROJECT = 'effekt' HARBOR_PROJECT = 'effekt'
IMAGE_REPO = 'effekt-interface-frontend' IMAGE_REPO = 'effekt-interface-frontend'
IMAGE_NAME = "${REGISTRY}/${HARBOR_PROJECT}/${IMAGE_REPO}" IMAGE_NAME = "${REGISTRY}/${HARBOR_PROJECT}/${IMAGE_REPO}"
BUILD_IMAGE = '39.170.26.156:8443/cicd/ubuntu:22.04-docker-py312-jdk8-nodejs20-mvn389'
DEPLOY_HOST = '39.170.26.156' DEPLOY_HOST = '124.220.32.45'
DEPLOY_PORT = '8022' DEPLOY_PORT = '22'
DEPLOY_USER = 'user'
CONTAINER_NAME = 'effekt-interface-frontend' CONTAINER_NAME = 'effekt-interface-frontend'
HOST_PORT = '8080' HOST_PORT = '8080'
CONTAINER_PORT = '80' CONTAINER_PORT = '80'
@@ -34,7 +37,7 @@ pipeline {
stage('Checkout Code') { stage('Checkout Code') {
steps { steps {
git branch: "${GIT_BRANCH}", git branch: "${GIT_BRANCH}",
credentialsId: 'git-effekt-frontend', credentialsId: 'ebe42f70-146f-4a4b-8090-eded24a77173',
url: "${GIT_URL}" url: "${GIT_URL}"
} }
} }
@@ -43,13 +46,12 @@ pipeline {
steps { steps {
sh ''' sh '''
set -e set -e
echo "=== whoami ===" echo "=== Build Container Environment ==="
whoami whoami
echo "=== Check Docker ==="
docker --version docker --version
echo "=== Check Node ==="
node -v node -v
npm -v npm -v
git --version
echo "=== Workspace ===" echo "=== Workspace ==="
pwd pwd
ls -la ls -la
@@ -69,31 +71,26 @@ pipeline {
stage('Build Docker Image') { stage('Build Docker Image') {
steps { steps {
sh ''' withDockerRegistry(registry: [credentialsId: '3a4a4463-784d-4e91-9457-9dfd64722ecb', url: 'https://39.170.26.156:8443']) {
set -e sh '''
docker build \ set -e
-t ${IMAGE_NAME}:${BUILD_NUMBER} \ docker build --no-cache \
-t ${IMAGE_NAME}:latest \ -t ${IMAGE_NAME}:${BUILD_NUMBER} \
. -t ${IMAGE_NAME}:latest \
''' -f Dockerfile \
.
'''
}
} }
} }
stage('Push Image To Harbor') { stage('Push Image To Harbor') {
steps { steps {
withCredentials([ withDockerRegistry(registry: [credentialsId: '3a4a4463-784d-4e91-9457-9dfd64722ecb', url: 'https://39.170.26.156:8443']) {
usernamePassword(
credentialsId: 'harbor-effekt',
usernameVariable: 'HARBOR_USER',
passwordVariable: 'HARBOR_PASS'
)
]) {
sh ''' sh '''
set -e set -e
echo "$HARBOR_PASS" | docker login ${REGISTRY} -u "$HARBOR_USER" --password-stdin
docker push ${IMAGE_NAME}:${BUILD_NUMBER} docker push ${IMAGE_NAME}:${BUILD_NUMBER}
docker push ${IMAGE_NAME}:latest docker push ${IMAGE_NAME}:latest
docker logout ${REGISTRY}
''' '''
} }
} }
@@ -101,36 +98,12 @@ pipeline {
stage('Deploy To Server') { stage('Deploy To Server') {
steps { steps {
withCredentials([ script {
usernamePassword( deploy_node_by_password(
credentialsId: 'docker-host-ssh', remote_host: env.DEPLOY_HOST,
usernameVariable: 'DEPLOY_USER', remote_port: env.DEPLOY_PORT,
passwordVariable: 'DEPLOY_PASS' credentials_id: '82d14a67-250f-46b0-8b13-50e874a11933'
),
usernamePassword(
credentialsId: 'harbor-effekt',
usernameVariable: 'HARBOR_USER',
passwordVariable: 'HARBOR_PASS'
) )
]) {
sh '''
set -e
sshpass -p "$DEPLOY_PASS" ssh \
-o StrictHostKeyChecking=no \
-p "$DEPLOY_PORT" \
"$DEPLOY_USER@$DEPLOY_HOST" "
echo \\"$HARBOR_PASS\\" | docker login $REGISTRY -u \\"$HARBOR_USER\\" --password-stdin &&
docker pull $IMAGE_NAME:latest &&
docker rm -f $CONTAINER_NAME || true &&
docker run -d \
--name $CONTAINER_NAME \
--restart always \
-p $HOST_PORT:$CONTAINER_PORT \
$IMAGE_NAME:latest &&
docker logout $REGISTRY
"
'''
} }
} }
} }
@@ -145,12 +118,45 @@ pipeline {
} }
always { always {
sh ''' sh '''
if command -v docker >/dev/null 2>&1; then docker image prune -f || true
docker image prune -f || true
else
echo "docker command not found, skip image prune"
fi
''' '''
} }
} }
} }
def deploy_node_by_password(Map args) {
withCredentials([
usernamePassword(
credentialsId: args.credentials_id,
passwordVariable: 'DEPLOY_PASS',
usernameVariable: 'DEPLOY_USER_CREDENTIAL'
),
usernamePassword(
credentialsId: '3a4a4463-784d-4e91-9457-9dfd64722ecb',
passwordVariable: 'HARBOR_PASS',
usernameVariable: 'HARBOR_USER'
)
]) {
def remote = [:]
remote.name = args.remote_host
remote.host = args.remote_host
remote.port = Integer.valueOf(args.remote_port)
remote.allowAnyHosts = true
remote.user = DEPLOY_USER_CREDENTIAL
remote.password = DEPLOY_PASS
def deployCommand = """
echo '${HARBOR_PASS}' | docker login '${REGISTRY}' -u '${HARBOR_USER}' --password-stdin &&
docker pull '${IMAGE_NAME}:latest' &&
docker rm -f '${CONTAINER_NAME}' || true &&
docker run -d \
--name '${CONTAINER_NAME}' \
--restart always \
-p '${HOST_PORT}:${CONTAINER_PORT}' \
'${IMAGE_NAME}:latest' &&
docker logout '${REGISTRY}' || true
"""
sshCommand remote: remote, sudo: false, command: deployCommand
}
}