From 215ac3e3cef50f8c622c8af2514ff11308a05a45 Mon Sep 17 00:00:00 2001 From: bpa Date: Wed, 12 Jul 2023 09:57:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=83=A8=E7=BD=B2=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 25 +++ Jenkinsfile | 153 ++++++++++++++++++ backend/.dockerignore | 25 +++ .../BPA.MES.Base.Web.Entry.csproj | 7 + .../BPA.MES.Base.Web.Entry.csproj.user | 2 +- backend/BPA.MES.Base.Web.Entry/Dockerfile | 24 +++ .../Properties/launchSettings.json | 17 +- backend/Dockerfile | 24 +++ frontend/default.conf | 13 ++ frontend/dockerfile | 4 + k8s_wfzapi.yaml | 64 ++++++++ k8s_wfzweb.yaml | 64 ++++++++ 12 files changed, 415 insertions(+), 7 deletions(-) create mode 100644 .dockerignore create mode 100644 Jenkinsfile create mode 100644 backend/.dockerignore create mode 100644 backend/BPA.MES.Base.Web.Entry/Dockerfile create mode 100644 backend/Dockerfile create mode 100644 frontend/default.conf create mode 100644 frontend/dockerfile create mode 100644 k8s_wfzapi.yaml create mode 100644 k8s_wfzweb.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3729ff0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..d5f9fe3 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,153 @@ + +pipeline{ + agent any + parameters { + choice( + description: 'wfzapi:开发环境1', + name: 'environment', + choices: ['hkerp'] + ) + + choice( + description: '前端or后端', + name: 'projectqh', + choices: ['backend','frontend'] + ) + + 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') + { + if(params.projectqh=='backend') + { + sh "docker build -f backend/dockerfile -t 10.2.1.24:10242/bpa/wfzap:${build_tag} ." + } + else if(params.projectqh=='frontend') + { + sh "docker build -f frontend/dockerfile -t 10.2.1.24:10242/bpa/wfzapi:${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') + { + if(params.projectqh=='backend') + { + sh "docker push 10.2.1.24:10242/bpa/wfzapi:${build_tag}" + + } + else if(params.projectqh=='frontend') + { + sh "docker push 10.2.1.24:10242/bpa/wfzweb:${build_tag}" + } + + } + } + } + } + + + + } + stage('Deploy') { + + steps { + script{ + echo "5. Deploy Stage" + // if(params.environment=='test') + // { + // input "确认要部署线上环境吗?" + // } + if(params.projectqh=='backend') + { + + sh "sed -i 's//${build_tag}/' k8s_wfzapi.yaml" + sh "sed -i 's//${env.BRANCH_NAME}/' k8s_wfzapi.yaml" + sh "sed -i 's//${params.environment}/' k8s_wfzapi.yaml" + sh "kubectl apply -f k8s_wfzapi.yaml --record" + } + else if(params.projectqh=='frontend') + { + sh "sed -i 's//${build_tag}/' k8s_wfzweb.yaml" + sh "sed -i 's//${env.BRANCH_NAME}/' k8s_wfzweb.yaml" + sh "sed -i 's//${params.environment}/' k8s_wfzweb.yaml" + sh "kubectl apply -f k8s_wfzweb.yaml --record" + } + + } + } + } + } +} diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 0000000..3729ff0 --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,25 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/backend/BPA.MES.Base.Web.Entry/BPA.MES.Base.Web.Entry.csproj b/backend/BPA.MES.Base.Web.Entry/BPA.MES.Base.Web.Entry.csproj index b6729c3..f686439 100644 --- a/backend/BPA.MES.Base.Web.Entry/BPA.MES.Base.Web.Entry.csproj +++ b/backend/BPA.MES.Base.Web.Entry/BPA.MES.Base.Web.Entry.csproj @@ -6,9 +6,16 @@ enable en-US true + 83c45a54-1a85-4886-9b4b-458e06c3071c + Linux + + + + + diff --git a/backend/BPA.MES.Base.Web.Entry/BPA.MES.Base.Web.Entry.csproj.user b/backend/BPA.MES.Base.Web.Entry/BPA.MES.Base.Web.Entry.csproj.user index 4ba691b..96e7a74 100644 --- a/backend/BPA.MES.Base.Web.Entry/BPA.MES.Base.Web.Entry.csproj.user +++ b/backend/BPA.MES.Base.Web.Entry/BPA.MES.Base.Web.Entry.csproj.user @@ -4,6 +4,6 @@ ProjectDebugger - BPA.MES.Base.Web.Entry + Docker \ No newline at end of file diff --git a/backend/BPA.MES.Base.Web.Entry/Dockerfile b/backend/BPA.MES.Base.Web.Entry/Dockerfile new file mode 100644 index 0000000..11fe5ea --- /dev/null +++ b/backend/BPA.MES.Base.Web.Entry/Dockerfile @@ -0,0 +1,24 @@ +#See https://aka.ms/containerfastmode to understand 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 + +FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +WORKDIR /src +COPY ["BPA.MES.Base.Web.Entry/BPA.MES.Base.Web.Entry.csproj", "BPA.MES.Base.Web.Entry/"] +COPY ["BPA.MES.Base.Web.Core/BPA.MES.Base.Web.Core.csproj", "BPA.MES.Base.Web.Core/"] +COPY ["BPA.MES.Base.Application/BPA.MES.Base.Application.csproj", "BPA.MES.Base.Application/"] +COPY ["BPA.MES.Base.Core/BPA.MES.Base.Core.csproj", "BPA.MES.Base.Core/"] +RUN dotnet restore "BPA.MES.Base.Web.Entry/BPA.MES.Base.Web.Entry.csproj" +COPY . . +WORKDIR "/src/BPA.MES.Base.Web.Entry" +RUN dotnet build "BPA.MES.Base.Web.Entry.csproj" -c Release -o /app/build + +FROM build AS publish +RUN dotnet publish "BPA.MES.Base.Web.Entry.csproj" -c Release -o /app/publish + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "BPA.MES.Base.Web.Entry.dll"] \ No newline at end of file diff --git a/backend/BPA.MES.Base.Web.Entry/Properties/launchSettings.json b/backend/BPA.MES.Base.Web.Entry/Properties/launchSettings.json index 44bd64b..ba371cf 100644 --- a/backend/BPA.MES.Base.Web.Entry/Properties/launchSettings.json +++ b/backend/BPA.MES.Base.Web.Entry/Properties/launchSettings.json @@ -1,4 +1,4 @@ -{ +{ "$schema": "http://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, @@ -12,20 +12,25 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "BPA.MES.Base.Web.Entry": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, - "launchUrl": "", - "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "applicationUrl": "http://localhost:5001;http://localhost:5000", + "dotnetRunMessages": true + }, + "Docker": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", + "publishAllPorts": true, + "useSSL": true } } } \ No newline at end of file diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..11fe5ea --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,24 @@ +#See https://aka.ms/containerfastmode to understand 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 + +FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +WORKDIR /src +COPY ["BPA.MES.Base.Web.Entry/BPA.MES.Base.Web.Entry.csproj", "BPA.MES.Base.Web.Entry/"] +COPY ["BPA.MES.Base.Web.Core/BPA.MES.Base.Web.Core.csproj", "BPA.MES.Base.Web.Core/"] +COPY ["BPA.MES.Base.Application/BPA.MES.Base.Application.csproj", "BPA.MES.Base.Application/"] +COPY ["BPA.MES.Base.Core/BPA.MES.Base.Core.csproj", "BPA.MES.Base.Core/"] +RUN dotnet restore "BPA.MES.Base.Web.Entry/BPA.MES.Base.Web.Entry.csproj" +COPY . . +WORKDIR "/src/BPA.MES.Base.Web.Entry" +RUN dotnet build "BPA.MES.Base.Web.Entry.csproj" -c Release -o /app/build + +FROM build AS publish +RUN dotnet publish "BPA.MES.Base.Web.Entry.csproj" -c Release -o /app/publish + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "BPA.MES.Base.Web.Entry.dll"] \ No newline at end of file diff --git a/frontend/default.conf b/frontend/default.conf new file mode 100644 index 0000000..ad50602 --- /dev/null +++ b/frontend/default.conf @@ -0,0 +1,13 @@ +server { + listen 80; + server_name localhost; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } +} + + + diff --git a/frontend/dockerfile b/frontend/dockerfile new file mode 100644 index 0000000..fab4e01 --- /dev/null +++ b/frontend/dockerfile @@ -0,0 +1,4 @@ +FROM nginx:latest +COPY ./public /usr/share/nginx/html/ +COPY ./default.conf /etc/nginx/conf.d/ +EXPOSE 80 diff --git a/k8s_wfzapi.yaml b/k8s_wfzapi.yaml new file mode 100644 index 0000000..2cfd074 --- /dev/null +++ b/k8s_wfzapi.yaml @@ -0,0 +1,64 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: wfzapi + namespace: kube- +spec: + + selector: + matchLabels: + app: wfzapi + replicas: 1 + template: + metadata: + labels: + app: wfzapi + spec: + containers: + - image: 10.2.1.24:10242/bpa/wfzapi: + imagePullPolicy: IfNotPresent + name: wfzapi + env: + - name: branch + value: + - name: TZ + value: Asia/Shanghai + ports: + - containerPort: 80 + protocol: TCP + volumeMounts: + - mountPath: "/app/appsettings.json" + name: wfzapi-config + readOnly: true + subPath: appsettings + - mountPath: "/etc/localtime" + name: timezone + resources: + requests: + cpu: "100m" + memory: "112Mi" + limits: + cpu: "500m" + memory: "512Mi" + volumes: + - name: wfzapi-config + configMap: + name: wfzapi-config + - name: timezone + hostPath: + path: /usr/share/zoneinfo/Asia/Shanghai +--- +kind: Service +apiVersion: v1 +metadata: + labels: + app: wfzapi + name: wfzapi + namespace: kube- +spec: + type: ClusterIP + ports: + - port: 80 + name: http + selector: + app: wfzapi diff --git a/k8s_wfzweb.yaml b/k8s_wfzweb.yaml new file mode 100644 index 0000000..c02741f --- /dev/null +++ b/k8s_wfzweb.yaml @@ -0,0 +1,64 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: wfzweb + namespace: kube- +spec: + + selector: + matchLabels: + app: wfzweb + replicas: 1 + template: + metadata: + labels: + app: wfzweb + spec: + containers: + - image: 10.2.1.24:10242/bpa/wfzweb: + imagePullPolicy: IfNotPresent + name: wfzweb + env: + - name: branch + value: + - name: TZ + value: Asia/Shanghai + ports: + - containerPort: 80 + protocol: TCP + volumeMounts: + - mountPath: "/app/appsettings.json" + name: wfzweb-config + readOnly: true + subPath: appsettings + - mountPath: "/etc/localtime" + name: timezone + resources: + requests: + cpu: "100m" + memory: "112Mi" + limits: + cpu: "500m" + memory: "512Mi" + volumes: + - name: wfzweb-config + configMap: + name: wfzweb-config + - name: timezone + hostPath: + path: /usr/share/zoneinfo/Asia/Shanghai +--- +kind: Service +apiVersion: v1 +metadata: + labels: + app: wfzweb + name: wfzweb + namespace: kube- +spec: + type: ClusterIP + ports: + - port: 80 + name: http + selector: + app: wfzweb