Jenkins 部署项目

343 阅读1分钟

1. 安装 Jenkins

# 安装
sudo wget -O /etc/yum.repos.d/jenkins.repo \
    https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
sudo yum upgrade
sudo systemctl daemon-reload

# 启动
systemctl start jenkins

# 停止
systemctl stop jenkins

# 故障解决
systemctl status jenkins # 查看启动文件
vim /etc/rc.d/init.d/jenkins # 修改 java 环境地址

# https 配置
https://jansipke.nl/enable-https-jenkins/

2. 安装 Jenkins 插件

  1. git
    This plugin integrates with jenkins
  2. NodeJs
    NodeJS plugin executes NodeJS script a build step.
  3. Publish Over SSH
    Send build artifacts over SSH

3. 配置 Global Tool

  1. Maven Configuration

  2. JDK

  3. Git

  4. Maven

  5. NodeJS

4. 配置 Credentials

add credential > select username with password / ssh

5. 新建项目

  1. New Item
    Enter an item name -> select Freestyle project

  2. General
    1. 填写 description
    2. 选择 JDK 版本

  3. Soruce Code Management
    1. 选择 Git
    2. 填写 url 地址,选择 Credentials

  4. Build Triggers
    手动触发,这里不勾选任何选项

  5. Build Environment
    1. 选择 Provide Node & npm bin/folder to PATH
    2. 值选择默认即可

  6. Build
    1. 选择 Execute Shell
    2. 起草 shell

    # 安装依赖
    npm install
    # 打包
    npm run build
    # 打印当前目录
    pwd
    
    # 拷贝 dist 目录到/usr/local/vue2-admin
    cp ./dist /usr/local/vue2-admin -R
    
  7. Post-build Actions
    1. 选择 Send build artifacts over SSH
    2. 填写 主机

    aliyun_fw
    

    3. Source files

    *nginx.conf,*Dockerfile,dist/**/*
    

    4. Remove prefix

    # 相当于拷贝完 dist 目录后,删除 dist 目录,但保留 dist 目录下的内容
    dist
    

    5. Remote directory

    # 这个文件夹必须存在
    /usr/local/vue2-admin
    

    6. Exec command

    cd /usr/local/vue2-admin/
    docker build -t vue2-admin .
    

8. Email Notification

选择 E-mail Notification
填写 email地址
勾选,失败也发送

6. 附注

Publish Over SSH,有风险

安装有 Gitlab 的机器因漏洞被非法控制,很容易造成控制权的横向传播,因此,尽量避免使用

Publish Over SSH。

使用时候做到:

1. 定期更换秘钥

2. 更改 22 端口为其他数字

3. 权限低的用户配置 .ssh 

4. 禁止 root 用户远程登录