美文网首页
代码部署(二):使用github+circleCI部署代码到多台

代码部署(二):使用github+circleCI部署代码到多台

作者: never615 | 来源:发表于2017-05-08 20:08 被阅读4029次

本文讲解的部署流程是:

  1. 推送代码到github
  2. 触发circleCI,开始进行测试,然后执行部署命令(部署命令的内容就是使用上一篇文章讲解的git部署代码的方式)
  3. 服务器接收到代码,触发钩子,执行其他需要执行的项目升级命令

问题
这样的部署流程有一个问题,当执行到composer update这样的命令的时候,项目是不能正常访问的,这在生产环境中是不能接受的.

解决方式:
在执行部署的时候,新建一个目录执行部署新的版本,完成后,修改软链接到新的目录.
有一些现成的工具(比如:deployer)可以帮助我们完成这些,后面我会写一篇文章专门介绍.
或者你也可以根据自己的需求自己编写部署命令解决这些问题.


接下来,我们结合使用github和circleCI实现更强大的功能.

CircleCI可以直接与github整合,提供构建/测试/部署功能.这篇文章主要介绍如何使用github+circleCI部署代码到服务器.


以一个laravel的php项目为例介绍circleCI的使用

circleCI端的配置

通过github登录circleCI后,可以看到你拥有的github项目,需要哪个项目就添加那个到circleCI就好了,circleCI会自动完成github端的配置.

使用命令ssh-keygen -f deploy就可以生成一对钥匙,然后需要在circleCI项目设置->ssh权限设置中新增刚生成的私钥(如果不设置hostname的话,对所有服务器有效).对应的公钥配置在服务器的~/.ssh/authorized_keys(部署用户的对应.ssh目录)中.

我是所有的项目部署均使用相同的一对钥匙,所以circleCI的上所有的项目配置相同的私钥,然后不配置hostname,所有服务器authorized_keys添加相同的公钥.

circle.yml文件

在项目根目录创建circle.yml文件.
1. 部署代码主要看deployment块,分为staging 和production两个环境,分别对应develop和master分支.当你把代码推送到github的不同分支时,就会执行不同的命令,你可以在这里写git命令来把代码推送到不同服务器.详细的部署配置参考
2. 使用git命令推送之前需要配置好git相应的远程分支.在./script/circleCI/git_production.sh中有体现.

circle.yml(php 项目)示例:

## Customize the test machine
machine:
  timezone:
    Asia/Shanghai # Set the timezone
  php:
    # https://circleci.com/docs/environment#php
    version: 7.1.0

  # Add some environment variables
  environment:
    CIRCLE_ENV: test

## Customize dependencies
dependencies:
  pre:
#    - npm update -g npm
#    - nvm install 6
#    - nvm use 6
#    - node -v
    - yes|cp .env.staging .env
    - yes|cp composer.json.circle composer.json
#    - npm install -g gulp

  override:
# 禁止circleCI自动执行的命令,比如会自动根据有没有package.json执行npm install等
    - echo "Ignore CircleCI defaults"
#    - composer install
#    - composer update
#    - php artisan vendor:publish --tag=laravel-admin
#    - php artisan vendor:publish --tag=easy-mall
#    - php artisan migrate

#    - npm install
#    - gulp --production

  # we automatically cache and restore many dependencies between
  # builds. If you need to, you can add custom paths to cache:
  cache_directories:
    #- "custom_1"   # relative to the build directory
    #- "~/custom_2" # relative to the user's home directory
    - ~/.composer/cache
    - vendor
#    - node_modules


## Customize test commands
test:
  override:
    - echo "Ignore CircleCI defaults"
#    - vendor/bin/phpunit

## dev:开发环境,使用文件系统直接部署
## staging::对应develop分支
## produciton:对应master分支,部署正式环境
## 这个部署分支和环境只是个参考,下一篇文章会专门说明部署环境
deployment:
  staging:
      branch: develop
      commands:
        -  ./script/circleCI/git_staging.sh
  production:
      branch: master
      commands:
       -  ./script/circleCI/git_production.sh

notify:
  webhooks:
    - url: https://jianliao.com/v2/services/webhook/xxx

git_production.sh示例

# 检查git remote是否有对应的部署地址,没有则添加. 用来部署项目时使用
#!/bin/sh
#要部署的项目服务器目录地址,包含生产环境项目地址和预演环境地址
server_paths=(
/app/back_end/temp/production
)
#要部署的服务器地址
servers=(
ssh://username@ipaddress1:port
ssh://mallto@ipaddress2:port
)
remotes=$(/usr/bin/git remote)
echo $remotes
i=0
for server in ${servers[@]}
do
    #echo ${server}
    #部署到指定的server_paths
    for server_path in ${server_paths[@]}
    do
        temp_remote=${server}${server_path}
        echo ${temp_remote}
        remote_name=$(echo -n ${temp_remote} | md5sum | awk '{print $1}')
        echo ${remote_name}
        remote_names[i]=${remote_name}
        i=`expr $i + 1`
        if [[ $remotes == *${remote_name}* ]]
        then
        echo "readd"
        git remote remove ${remote_name}
        git remote add ${remote_name} ${temp_remote}
        else
        echo "first add"
        git remote add ${remote_name} ${temp_remote}
        fi
    done
done
git fetch --unshallow
for remote_name_item in ${remote_names[@]}
do
git push -f  ${remote_name_item} master
done

通知结果

slack虽然好用,但是国内访问有问题,为了保证能够及时收到消息,我比较了零信和简聊.测试中零信经常漏掉circleCI的通知,简聊每次都收到了所以使用简聊.
在简聊的频道中添加circleCI的服务,会得到一个webhooks地址,添加到circleCI的配置文件即可.

总结

实际上把这些脚本/配置都写好之后,以后每次需要新建一个新项目需要配置的就那几步:

  1. github上的项目添加到circleCI
  2. 在circleCI项目设置中的ssh权限设置添加部署私钥
  3. 创建项目目录,然后进入目录
  4. git init
  5. [git checkout -b develop]
  6. git config receive.denyCurrentBranch ignore
  7. vi .git/hooks/post-receive
  8. 粘贴钩子脚本,修改必要参数(如该环境需要cp的配置文件名)
  9. chmod a+x .git/hooks/post-receive
  10. 项目端复制入已经写好的circleCI脚本,修改必要参数(如要部署的目录)
  11. 推送项目到github就可以完成部署了

后端项目在第一次推送之后还需要:

  1. 修改相应目录的写权限
  2. 添加软链接
  3. 其他初始化命令(引入库需要执行的安装命令等)
    或者把这些第一次部署需要执行的命令都编写好脚本.下一篇文章我们使用的第三方部署工具把这些都做好了.

参考

circleCI文档
stackoverflow->protocol error: expected old/new/ref, got 'shallow dee..

相关文章

网友评论

      本文标题:代码部署(二):使用github+circleCI部署代码到多台

      本文链接:https://www.haomeiwen.com/subject/rujvtxtx.html