公司电商项目环境使用腾讯云服务,考虑将线下gitlab和jenkins迁到腾讯云CODING。测试几天后的使用心情
jsonpipeline {
agent any
stages {
stage('检出') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: GIT_BUILD_REF]],
userRemoteConfigs: [[
url: GIT_REPO_URL,
credentialsId: CREDENTIALS_ID
]]])
}
}
stage('打包构建') {
steps {
sh 'mvn clean install -Dmaven.test.skip=true'
}
}
stage('推送镜像') {
steps {
withCredentials([ usernamePassword(credentialsId:'a34f2866-bd24-4c0f-a641-c7af9b0e0e15',usernameVariable:'USERNAME',passwordVariable:'PASSWORD')
]) {
sh '''cd ${MODULE_NAME}-service && docker build -t ${tcr_repositity_url}/test/${tcr_repository_name}:${tag_name}-$CI_BUILD_NUMBER . //镜像TAG使用环境+构建ID号
docker login -u ${USERNAME} -p ${PASSWORD} ${tcr_repositity_url}
docker push ${tcr_repositity_url}/test/${tcr_repository_name}:${tag_name}-$CI_BUILD_NUMBER'''
}
}
}
stage('是否部署') {
steps {
input(message: '是否继续执行构建', submitter: 'PKWiWDJoEj')
}
}
stage('部署服务') {
steps {
withCredentials([ usernamePassword(credentialsId:'1bfd5149-d5db-446c-9e2a-b54260457521',usernameVariable:'FTP_USERNAME',passwordVariable:'FTP_PASSWORD')
]) {
sh '''curl --basic -u ${FTP_USERNAME}:${FTP_PASSWORD} -o ${config_name} ${nginx_url}/${config_name} //拉取nginx服务器上的config配置文件
cat ./${config_name} >/root/.kube/config
kubectl set image deployment/${MODULE_NAME} ${MODULE_NAME}=${tcr_repositity_url}/xhzx/${tcr_repository_name}:${tag_name}-$CI_BUILD_NUMBER -n ${nsname} //直接更新服务的image,
kubectl scale --replicas=${replicas} deploy/${MODULE_NAME} -n ${nsname}
if [ $? -eq 0 ]; then
echo "部署应用成功,开始检查应用状态..."
kubectl rollout status deploy/${MODULE_NAME} -n ${nsname}
if [ $? -eq 0 ]; then
echo "应用运行状态正常"
else
echo "应用运行状态异常"
fi
else
echo "部署应用失败"
fi'''
}
}
}
}
environment {
tcr_repositity_url = 'ccr.ccs.tencentyun.com'
tag_name = 'test'
tcr_repository_name = 'system'
config_name = 'test-ns-test-lan'
MODULE_NAME = 'system'
MODULE_port = '80'
//nginx服务器地址
nginx_url = 'http://10.0.20.42'
deploy_yaml = 'deploy-template.yaml'
nsname = 'test'
limits_cpu = '2000m'
limits_memory = '2048Mi'
run_env = 'test'
}
}
yaml[root@ftp-server download]# cat deploy-template.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: ${MODULE_NAME}
namespace: ${nsname:-test}
labels:
app: ${MODULE_NAME}
spec:
minReadySeconds: 60
replicas: ${replicas:-1}
revisionHistoryLimit: 3
strategy:
rollingUpdate:
maxSurge: 30%
maxUnavailable: 30%
type: RollingUpdate
selector:
matchLabels:
app: ${MODULE_NAME}
template:
metadata:
labels:
app: ${MODULE_NAME}
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- ${MODULE_NAME}
topologyKey: "kubernetes.io/hostname"
imagePullSecrets:
- name: tx-secret
containers:
- name: ${MODULE_NAME}
image: ${tcr_repositity_url}/test/${tcr_repository_name}:${tag_name}-$CI_BUILD_NUMBER
imagePullPolicy: IfNotPresent
ports:
- containerPort: ${project_port}
protocol: TCP
resources:
limits:
cpu: ${limits_cpu:-1000m}
memory: ${limits_memory:-2048Mi}
requests:
cpu: ${requests_cpu:-100m}
memory: ${requests_memory:-256Mi}
#args: ${app_args}
env: ${env_args}
volumeMounts:
- name: logs
mountPath: /app/log/
volumes:
- name: logs
hostPath:
path: /home/app/log/
type: DirectoryOrCreate
---
[root@ftp-server download]# cat svc-template.yaml
apiVersion: v1
kind: Service
metadata:
name: ${MODULE_NAME}
namespace: ${nsname:-test}
labels:
name: ${MODULE_NAME}
spec:
type: ClusterIP
selector:
app: ${MODULE_NAME}
ports:
- port: ${project_port}
protocol: TCP
targetPort: ${project_port}
name: http
---
# test命名空间权限
[root@ftp-server download]# cat test-ns-test-lan
apiVersion: v1
kind: Config
clusters:
- name: kubernetes
cluster:
certificate-authority-data: "*******"
server: https://10.0.6.11
contexts:
- name: kubernetes
context:
cluster: kubernetes
user: test
namespace: "test"
current-context: kubernetes
users:
- name: test
user:
token: "************"
本文作者:曹操
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!