Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jenkins构建h5项目 #33

Open
Pasoul opened this issue Sep 22, 2020 · 0 comments
Open

jenkins构建h5项目 #33

Pasoul opened this issue Sep 22, 2020 · 0 comments

Comments

@Pasoul
Copy link
Owner

Pasoul commented Sep 22, 2020

创建项目

image

参数化构建

如果构建的过程中需要选参数:
image
勾选This project is parameterized
通常我们只会用到Choice Parameter和Git参数(需要安装Git Parameter Plug-In)两种选项:
• Choice Parameter:提供select选择功能
• Git参数:可以自行选择要构建的分支
image

Choice Parameter

• 名称:当前选中的值会赋值给变量SERVER_IP,在shell中通过$SERVER_IP获取
• 选项:select选择框提供的选项,默认选中第一个值
image
实际效果如下:
image

Git参数

• 名称:当前选中的分支会赋值给变量SERVER_IP,在shell中通过$SERVER_IP获取
• 参数类型:分支
• 默认值 + 已选值:指定默认选中哪个分支
image
实际效果如下:
image

源码管理Git

配置远程仓库地址

通常我们配置git仓库地址会报错:
image
需要我们在服务器上生成的id_rsa.pub添加到gitlib的ssh keys下面,具体做法如下:
新的解决方案:
将宿主机的ssh文件拷贝到docker容器中,共享一份ssh key

// 将宿主机的/home/lukou/.ssh拷贝到jenkins容器的/root目录下
sudo docker cp /home/lukou/.ssh jenkins:/root

旧的解决方案:
以在docker镜像为例,如果直接在服务器装的jenkins可以免去这一步:

  1. 进入jenkins(这里是你的容器名)容器,查看有没有id_rsa.pub文件
docker exec -it jenkins /bin/bash
cd ~/.ssh
cat id_rsa.pub
#如果没有该文件需要我们手动创建,一路回车即可
ssh-keygen -t rsa -C "[email protected]"
  1. cat id_rsa.pub 复制到gitlab上
  2. 测试是否配置成功(这一步会在~/.ssh_keys生成know_hosts,不能跳过)
    git ls-remote -h -- [email protected]:frontend/bumblebee.git
  3. 刷新页面看到已经验证成功
    image

指定分支

指定当前构建的分支为$BRANCH,即参数化构建 - 用户手动选中的分支

限制git分支

如果我们生产环境只能构建master分支

  1. 在Git参数 -> 高级这里,设置默认值,这样构建时只能选择master分支
    image
  2. 在shell脚本里面判断当前分支是不是origin/master
# 判断分支
if [ "$BRANCH" != "origin/master" ];then
echo "当前分支是$BRANCH,生产环境必须是master分支,退出构建"
exit 1
fi

# 构建
nvm use v12.9.1
npm -v
node -v

构建shell

#!/bin/bash -ilex
# set enviroment
PROD_NAME="dist"
PROD_ZIP="$PROD_NAME.zip"

USER="lukou"
APP_IP=$SERVER_IP
APP_HOME="/home/lukou/tmp/$DIRECTORY"

# 构建
nvm use v12.9.1
npm -v
node -v
npm config set registry https://registry.npm.taobao.org
npm install
npm run build

zip -rp $PROD_ZIP ./$PROD_NAME
echo $PWD
scp $PROD_ZIP $USER@$APP_IP:$APP_HOME/
scp template/* $USER@$APP_IP:$APP_HOME/

# 开始部署
ssh -T $USER@$APP_IP << EOF

 #sudo chmod 777 -R $APP_HOME
 cd $APP_HOME
 unzip -oq $PROD_ZIP
 rm -rf ./static
 rm -rf ./index.html
 mv ./dist/* ./
 rm -rf ./$PROD_NAME
 rm -rf ./$PROD_ZIP
EOF

常见问题

  1. 发送构建文件到目标服务器报错
+ scp dist.zip [email protected]:/home/lukou/tmp/coupon_qiuyu/
Host key verification failed.
lost connection
Build step 'Execute shell' marked build as failure
Finished: FAILURE

原因:
猜测是因为当前docker容器没有连接过目标服务器所致,查看目标服务器~/.ssh/know_hosts,确实没有当前docker容器的id_rsa.pub,一般连接过的都会在这里产生记录
解决办法:
如果jenkins在docker容器中,此操作在容器中进行
• 复制~/.ssh/id_rsa.pub
• 登录目标服务器

ssh [email protected]
cd ~/.ssh
vi authorized_keys
添加复制的id_rsa.pub

回到docker容器或者原服务器中,使用ssh连接目标服务器即可

root@cb0c931a18d2:~/.ssh# ssh [email protected]
The authenticity of host '121.41.121.230 (121.41.121.230)' can't be established.
RSA key fingerprint is SHA256:KI315BM3JQvdx7RpazSER7WUD4SWPe6KAs5G/lPOynA.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '121.41.121.230' (RSA) to the list of known hosts.
Last login: Sat Jul 11 09:12:17 2020 from 120.26.131.154

Welcome to aliyun Elastic Compute Service!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant