Linux + .Net Core + Nginx + Supervisor +Certbot + Jenkins 自动化部署程序

忘忧 2021年12月15日 631次浏览

本机环境

  • CentOS 7.6
  • .Net5
  • Nginx
  • Certbot
  • Supervisor

Jenkins 教程
https://wangyou233.wang/archives/41

项目地址 https://gitee.com/wangyouqaq/demo-linux-nginx.git

特别鸣谢

MK1UUR17ZNDZ_VL.png
3元

image.png
0.25元

安装环境

更新系统&准备工作

yum update -y
systemctl stop firewalld.service
systemctl disable firewalld.service
yum install zip -y
yun install unzip -y
mkdir -p /var/www/apps/demo-linux

Jenkins 主机安装

yum install sshpass -y
yum install zip -y
touch 1.txt 
scp 1.txt root@121.43.171.239:/root //信任证书

安装.Net5

rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
yum install dotnet-sdk-5.0 -y
dotnet dev-certs
cp /etc/pki/tls/cert.pem  /usr/local/openssl/cert.pem

安装Supervisor

yum install yum-utils -y
yum install supervisor -y
systemctl enable supervisord
systemctl start supervisord
systemctl status supervisord 
supervisorctl -c /etc/supervisord.conf

安装Nginx

yum install epel-release -y
yum install nginx - y
systemctl start nginx
systemctl status nginx
systemctl enable nginx

image.png

安装CertBot

yum install snapd -y
systemctl enable --now snapd.socket
ln -s /var/lib/snapd/snap /snap
snap install core
snap refresh core
snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot

新建一个WebApi项目

image.png

将代码传到gitee上

https://gitee.com/wangyouqaq/demo-linux-nginx.git
image.png

新增prod.Jenkinsfile文件内容如下

def remote = [:]
remote.name = 'TService'
remote.host = '121.43.171.239'
remote.user = 'root'
remote.password = 'Yexuba521.'
remote.allowAnyHosts = true
pipeline {
    agent any
    environment {
        RELEASE_PATH = "${WORKSPACE}/releases"
        COMMIT_ID = sh(returnStdout: true, script: 'git rev-parse HEAD')
    }
    stages {
        stage("Set ENV") {
            steps {
                sh "printenv"
            }
        }
        stage('Build') {
            steps {
                echo "${GIT_COMMIT_USER}"
                echo "${GIT_COMMIT_EMAIL}"
                echo "${GIT_COMMIT_MESSAGE}"
                echo 'Building...'
                sh "/usr/share/dotnet/dotnet restore"
                sh "/usr/share/dotnet/dotnet build"
            }
        }
        stage('Test') {
            steps {
                echo 'Testing...'
            }
        }
        stage('Publish') {
            steps {
                echo 'Publish....'
                sh "/usr/share/dotnet/dotnet publish -c=Release -o=${RELEASE_PATH}/DevOps demo-linux/demo-linux.csproj"
                echo RELEASE_PATH
                writeFile(file: "${RELEASE_PATH}/DevOps/commit-${COMMIT_ID}.txt", text: "")
                dir('releases') {
                    sh "zip -q -r ${RELEASE_FILE_NAME} *"
                }
            }
        }
        stage('Publish Service'){
            steps {
                sh "scp releases/${RELEASE_FILE_NAME} ${remote.user}@${remote.host}:/home/root"
                sshCommand remote: remote, command: "cd /home/root"
                sshCommand remote: remote, command: "unzip -q -o -d /var/www/apps/demo-linux /home/root/${RELEASE_FILE_NAME}"
                sshCommand remote: remote, command: " supervisorctl restart demo-linux"
                sshCommand remote: remote, command: " supervisorctl status demo-linux"
                echo "Publish Service Success"
            }
         
        }
        stage('Delete Publish Release') {
            steps {
                echo "Delete Files..."
                sh "rm -rf ${RELEASE_PATH}"
            }
        }
    }
    post {
        success{
                emailext (
                subject: '${DEFAULT_SUBJECT}',
                body: '${DEFAULT_CONTENT}',
                to: "915589928@qq.com")
        }
        failure {
            emailext (
                subject: '${DEFAULT_SUBJECT}',
                body: '${DEFAULT_CONTENT}',
                to: "915589928@qq.com")
        }
  
   }
}

Jenkins创建流水线项目

image.png
配置项目
image.png

创建Supervisor 守护进程

cd /etc/supervisord.d
vim demo-linux.ini
supervisorctl reload

demo-linux.ini配置文件

[program:demo-linux]
command=/usr/share/dotnet/dotnet /var/www/apps/demo-linux/demo-linux.dll --urls http://*:5008
autostart=true
autorestart=true
startsecs=5
priority=1
stopasgroup=true
killasgroup=true
 supervisorctl reload

试着运行发布脚本

image.png
image.png
发布成功

配置Nginx反向代理

cd /etc/nginx/conf.d
vim demo.conf

demo.conf

server {
    listen       80;
    server_name  demo.linux.wangyou233.wang;
    root /var/www/apps/demo-linux;

    location / {
    proxy_pass http://127.0.0.1:5008;
proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass_header Server;
        proxy_connect_timeout 3s;
        proxy_read_timeout 10s;
    }
}

重载nginx

nginx -t
nginx -s reload

image.png
image.png

配置域名和Https证书

certbot --nginx //申请证书

image.png
image.png
访问已经变成https了
image.png

总结

完结撒花