Browse Source

部署脚本

master
bpa 1 year ago
parent
commit
b7ba286a84
4 changed files with 222 additions and 0 deletions
  1. +25
    -0
      Dockerfile
  2. +126
    -0
      Jenkinsfile
  3. +7
    -0
      NuGet.config
  4. +64
    -0
      k8s_groupmeal.yaml

+ 25
- 0
Dockerfile View File

@@ -0,0 +1,25 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["BPA.KitChen.GroupMeal/BPA.KitChen.GroupMeal.csproj", "BPA.KitChen.GroupMeal/"]
COPY ["BPA.KitChen.GroupMeal.Application/BPA.KitChen.GroupMeal.Application.csproj", "BPA.KitChen.GroupMeal.Application/"]
COPY ["BPA.KitChen.GroupMeal.Core/BPA.KitChen.GroupMeal.Core.csproj", "BPA.KitChen.GroupMeal.Core/"]
COPY ["BPA.KitChen.GroupMeal.SqlSugar/BPA.KitChen.GroupMeal.SqlSugar.csproj", "BPA.KitChen.GroupMeal.SqlSugar/"]
RUN dotnet restore "BPA.KitChen.GroupMeal/BPA.KitChen.GroupMeal.csproj"
COPY . .
WORKDIR "/src/BPA.KitChen.GroupMeal"
RUN dotnet build "BPA.KitChen.GroupMeal.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "BPA.KitChen.GroupMeal.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BPA.KitChen.GroupMeal.dll"]

+ 126
- 0
Jenkinsfile View File

@@ -0,0 +1,126 @@

pipeline{
agent any
parameters {
choice(
description: 'dev1:开发环境1 \n dev2:开发环境2 \n dev3:生产环境',
name: 'environment',
choices: ['dev1', 'dev2', 'dev3']
)

choice(
description: '执行操作(发布|回滚)',
name: 'operation',
choices: ['develop', 'rollback']
)

choice(
description: '分支名',
name: 'branchName',
choices: ['master']
)

string(
name: 'tag',
defaultValue: '',
description: '版本tag'
)

}

stages
{
stage('Prepare') {
steps {
script{
echo "1.Prepare Stage"
echo "当前环境${params.environment}"
if(params.operation=='develop')
{
checkout scm
script {
build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
if (env.BRANCH_NAME != 'master') {
build_tag = "${env.BRANCH_NAME}-${build_tag}"
}
}
}
else{
script {
if(params.branchName!= 'master')
{
build_tag = "${params.branchName}-${params.tag}"
}
else
{
build_tag="${params.tag}"
}
}
}
}
}
}
stage('Test') {
steps {
script{
echo "2.Test Stage"
}
}
}
stage('Build') {
steps {
script{
echo "3.Build Docker Image Stage"
if(params.operation=='develop')
{

sh "docker build -t 10.2.1.24:10242/bpa/groupmeal:${build_tag} ."
}
}
}
}
stage('Push') {
steps {
script{
echo "4.Push Docker Image Stage"
withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'harborPassword', usernameVariable: 'harborUser')]) {
sh "docker login -u ${harborUser} -p ${harborPassword} 10.2.1.24:10242"
if(params.operation=='develop')
{
sh "docker push 10.2.1.24:10242/bpa/groupmeal:${build_tag}"
}
}
}
}

}
stage('Deploy') {

steps {
script{
echo "5. Deploy Stage"
// if(params.environment=='test')
// {
// input "确认要部署线上环境吗?"
// }
sh "sed -i 's/<BUILD_TAG>/${build_tag}/' k8s_groupmeal.yaml"
sh "sed -i 's/<BRANCH_NAME>/${env.BRANCH_NAME}/' k8s_groupmeal.yaml"
sh "sed -i 's/<NAMESPACES>/${params.environment}/' k8s_groupmeal.yaml"
sh "kubectl apply -f k8s_groupmeal.yaml --record"


}
}
}
}
}

+ 7
- 0
NuGet.config View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json"/>
<add key="bpa" value="http://10.2.1.25:10243/repository/bpa/index.json"/>
</packageSources>
</configuration>

+ 64
- 0
k8s_groupmeal.yaml View File

@@ -0,0 +1,64 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: groupmeal
namespace: kube-<NAMESPACES>
spec:
selector:
matchLabels:
app: groupmeal
replicas: 1
template:
metadata:
labels:
app: groupmeal
spec:
containers:
- image: 10.2.1.24:10242/bpa/groupmeal:<BUILD_TAG>
imagePullPolicy: IfNotPresent
name: groupmeal
env:
- name: branch
value: <BRANCH_NAME>
- name: TZ
value: Asia/Shanghai
ports:
- containerPort: 80
protocol: TCP
volumeMounts:
- mountPath: "/app/appsettings.json"
name: groupmeal-config
readOnly: true
subPath: appsetting
- mountPath: "/etc/localtime"
name: timezone
resources:
requests:
cpu: "100m"
memory: "112Mi"
limits:
cpu: "500m"
memory: "512Mi"
volumes:
- name: groupmeal-config
configMap:
name: groupmeal-config
- name: timezone
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
---
kind: Service
apiVersion: v1
metadata:
labels:
app: groupmeal
name: groupmeal
namespace: kube-<NAMESPACES>
spec:
type: ClusterIP
ports:
- port: 80
name: http
selector:
app: groupmeal

Loading…
Cancel
Save