refactor ci/cd

This commit is contained in:
jagger 2024-03-04 09:43:31 +08:00
parent 9728ed7a26
commit 98cb200798
6 changed files with 31 additions and 188 deletions

View File

@ -1,92 +0,0 @@
version: 2
name: 腾讯云dev环境
description: ""
global:
concurrent: 1
param:
- ref: secret_name
name: ""
value: '"jcce-aliyuncs"'
required: false
type: STRING
hidden: false
- ref: project_name
name: ""
value: '"kubex-frontend"'
required: false
type: STRING
hidden: false
trigger:
webhook: gitlink@1.0.0
event:
- ref: create_tag
ruleset:
- param-ref: tag
operator: EQ
value: '""'
ruleset-operator: AND
workflow:
- ref: start
name: 开始
task: start
- ref: git_clone_0
name: git clone
task: git_clone@1.2.6
input:
ssh_key: ((test.gitlink_sshkey))
remote_url: '"git@code.gitlink.org.cn:JCCE/kubex-frontend.git"'
ref: '"refs/heads/earth"'
commit_id: '""'
depth: 1
needs:
- start
- ref: docker_image_build_0
name: docker镜像构建
task: docker_image_build@1.6.0
input:
docker_username: ((test.docker_user))
docker_password: ((test.docker_password))
image_name: '"registry.cn-hangzhou.aliyuncs.com/jcce/kubex-frontend"'
image_tag: git_clone_0.commit_time
registry_address: '"registry.cn-hangzhou.aliyuncs.com"'
docker_build_path: git_clone_0.git_path
workspace: git_clone_0.git_path
image_clean: true
image_push: true
build_args: '""'
needs:
- shell_0
- ref: end
name: 结束
task: end
needs:
- kubectl_deploy_0
- ref: kubectl_deploy_0
name: kubectl部署资源
task: kubectl_deploy@1.1.0
input:
command: '"apply"'
resource_file_path: git_clone_0.git_path + '/deploy/k8s/kubex-frontend.yaml'
certificate_authority_data: ((dev.k8s_cad))
server: '"https://119.45.100.73:6443"'
client_certificate_data: ((dev.k8s_ccd))
client_key_data: ((dev.k8s_ckd))
hosts: '""'
needs:
- docker_image_build_0
- ref: shell_0
name: shell
image: docker.jianmuhub.com/library/debian:buster-slim
env:
IMAGE_NAME: '"registry.cn-hangzhou.aliyuncs.com/jcce/kubex-frontend"'
IMAGE_TAG: git_clone_0.commit_time
SECRET_NAME: global.secret_name
PROJECT_NAME: global.project_name
PROJECT_PATH: git_clone_0.git_path + '/deploy/k8s/'
script:
- cd ${PROJECT_PATH}
- sed -i "s#image_name#${IMAGE_NAME}:${IMAGE_TAG}#" ${PROJECT_NAME}.yaml
- sed -i "s#secret_name#${SECRET_NAME}#" ${PROJECT_NAME}.yaml
- cat ${PROJECT_NAME}.yaml
needs:
- git_clone_0

View File

@ -34,7 +34,7 @@ workflow:
task: git_clone@1.2.6
input:
ssh_key: ((test.gitlink_sshkey))
remote_url: '"git@code.gitlink.org.cn:JCCE/kubex-frontend.git"'
remote_url: '"https://gitlink.org.cn/JointCloud/JCC-RIP.git"'
ref: '"refs/heads/earth"'
commit_id: '""'
depth: 1
@ -46,7 +46,7 @@ workflow:
input:
docker_username: ((test.docker_user))
docker_password: ((test.docker_password))
image_name: '"registry.cn-hangzhou.aliyuncs.com/jcce/kubex-frontend"'
image_name: '"registry.cn-hangzhou.aliyuncs.com/jcce/jcc-rip'
image_tag: git_clone_0.commit_time
registry_address: '"registry.cn-hangzhou.aliyuncs.com"'
docker_build_path: git_clone_0.git_path
@ -66,7 +66,7 @@ workflow:
task: kubectl_deploy@1.1.0
input:
command: '"apply"'
resource_file_path: git_clone_0.git_path + '/deploy/k8s/kubex-frontend.yaml'
resource_file_path: git_clone_0.git_path + '/deploy/k8s/jcc-rip-frontend.yaml'
certificate_authority_data: ((test.k8s_cad))
server: '"https://47.92.39.128:6443"'
client_certificate_data: ((test.k8s_ccd))
@ -78,7 +78,7 @@ workflow:
name: shell
image: docker.jianmuhub.com/library/debian:buster-slim
env:
IMAGE_NAME: '"registry.cn-hangzhou.aliyuncs.com/jcce/kubex-frontend"'
IMAGE_NAME: '"registry.cn-hangzhou.aliyuncs.com/jcce/jcc-rip"'
IMAGE_TAG: git_clone_0.commit_time
SECRET_NAME: global.secret_name
PROJECT_NAME: global.project_name

View File

@ -18,7 +18,7 @@ RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.sjtug.sjtu.edu.cn/g' /etc/apk/repos
apk add --update tzdata && \
rm -rf /var/cache/apk/*
COPY --from=builder /app/dist /usr/share/nginx/html/jcce
COPY --from=builder /app/dist /usr/share/nginx/html/jcc-rip
RUN rm /etc/nginx/conf.d/default.conf
COPY deploy/nginx/default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80

70
Jenkinsfile vendored
View File

@ -1,70 +0,0 @@
def JOB_NAME = "${env.JOB_NAME}"
def BUILD_NUMBER = "${env.BUILD_NUMBER}"
def label = "jenkins-${JOB_NAME}-${BUILD_NUMBER}-${UUID.randomUUID().toString()}"
def secret_name = "harbor-auth"
podTemplate(label: label, containers: [
containerTemplate(name: 'nodejs', image: 'node:14.20.0-alpine3.16', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'docker', image: 'docker:latest', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'kubectl', image: 'jcce/kubectl:1.23.7', command: 'cat', ttyEnabled: true)
], serviceAccount: 'jenkins', volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
]) {
node(label) {
def myRepo = checkout scm
// 获取 git commit id 作为镜像标签
def imageTag = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
// 仓库地址
def registryUrl = "hub.jcce.dev:18443/repository/docker-hub"
def imageEndpoint = "jcce/kubex-frontend"
// 镜像
def image = "${registryUrl}/${imageEndpoint}:${imageTag}"
def imageLatest = "${registryUrl}/${imageEndpoint}:latest"
stage('单元测试') {
echo "1.测试阶段"
}
stage('代码编译打包') {
try {
container('nodejs') {
echo "2.代码编译打包阶段"
sh('npm install --registry=https://registry.npmmirror.com')
sh('npm run build:prod')
}
} catch (exc) {
println "构建失败 - ${currentBuild.fullDisplayName}"
throw(exc)
}
}
stage('构建 Docker 镜像') {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'docker-auth',
usernameVariable: 'DOCKER_USER',
passwordVariable: 'DOCKER_PASSWORD']]) {
container('docker') {
echo "3. 构建 Docker 镜像阶段"
sh('cat /etc/resolv.conf')
sh("docker login '${registryUrl}' -u '${DOCKER_USER}' -p '${DOCKER_PASSWORD}' ")
sh("docker build -t '${image}' -t '${imageLatest}' .")
sh("docker push '${image}'")
sh("docker push '${imageLatest}'")
sh("docker rmi '${image}' '${imageLatest}'")
}
}
}
stage('运行 Kubectl 部署到k8s平台') {
withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
container('kubectl') {
echo "5.部署应用"
sh('mkdir -p ~/.kube && cp ${KUBECONFIG} ~/.kube/config')
sh("sed -i 's#IMAGE_NAME#${image}#' deploy/k8s/deployment.yaml")
sh("sed -i 's#SECRET_NAME#${secret_name}#' deploy/k8s/deployment.yaml")
sh('kubectl apply -f deploy/k8s/')
sh('sleep 3')
echo "6.查看应用"
sh('kubectl get all -n jcce-system -l app=${JOB_NAME}')
}
}
}
}
}

View File

@ -1,25 +1,25 @@
kind: Deployment
apiVersion: apps/v1
metadata:
name: kubex-frontend-deployment
name: jcc-rip-frontend
namespace: jcce-system
labels:
k8s-app: kubex-frontend
k8s-app: jcc-rip
spec:
replicas: 1
selector:
matchLabels:
k8s-app: kubex-frontend
k8s-app: jcc-rip
template:
metadata:
name: kubex-frontend
name: jcc-rip
labels:
k8s-app: kubex-frontend
k8s-app: jcc-rip
spec:
imagePullSecrets:
- name: secret_name
containers:
- name: kubex-frontend
- name: jcc-rip
image: image_name
resources: {}
imagePullPolicy: Always
@ -49,12 +49,12 @@ apiVersion: v1
kind: Service
metadata:
namespace: jcce-system
name: kubex-frontend-service
name: jcc-rip-service
labels:
k8s-service: kubex-frontend
k8s-service: jcc-rip
spec:
selector:
k8s-app: kubex-frontend
k8s-app: jcc-rip
ports:
- name: web
protocol: TCP

View File

@ -1,5 +1,9 @@
upstream backend-up {
server jcce-gateway-service:8000;
upstream jcc-backend {
server dev.jointcloud.net:443;
}
upstream pcm-backend {
server pcm-core-api:8999;
}
server {
@ -10,21 +14,21 @@ server {
error_log /var/log/nginx/error.log error;
location / {
root /usr/share/nginx/html/jcce; #站点目录
root /usr/share/nginx/html/jcc-rip; #站点目录
index index.html index.htm;
}
#monitor项目
location /monitor {
alias /usr/share/nginx/html/jcce/;
alias /usr/share/nginx/html/jcc-rip/;
try_files $uri $uri/ /monitor/index.html; #解决页面刷新404问题
index index.html index.htm;
autoindex on;
}
location /apis/ {
location /pcm/ {
proxy_http_version 1.1;
proxy_pass http://backend-up/;
proxy_pass http://pcm-backend/pcm/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@ -33,14 +37,15 @@ server {
index index.html index.htm;
}
#虚拟机
location ^~/jcc-vm/ {
location /apis/ {
proxy_http_version 1.1;
proxy_pass https://jcc-backend/apis/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass https://10.101.14.2/;
client_max_body_size 12288m;
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;