常用的一些 CI 脚本

由于负责小组的 CI 公共事项,经常需要配置 CI 流程,或者帮助其他人解决一些问题,整理了一下常用的 CI 脚本,以方便查阅。

1. .gitlab-ci.yml 结构

下面是, GitLab CI 的配置文件结构。.gitlab-ci.yml 文件

相关推荐

站点声明:本站部分内容转载自网络,作品版权归原作者及来源网站所有,任何内容转载、商业用途等均须联系原作者并注明来源。

相关侵权、举报、投诉及建议等,请发邮件至E-mail:service@mryunwei.com

回到顶部
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 一些前置脚本,完成激活环境等操作
before_script:
  - source /data/runner/node/bin/activate
  - which node && node --version
  - which npm && npm --version
  - LANG="zh_CN.utf8"
  - export LC_ALL=zh_CN.UTF-8

1. 编排需要执行的 stage
stages:
  - build
  - deploy

1. 定义 job。job 属于某一个 stage,比如这里的 build 、deploy。GitLab CI 会按照 stages 配置的先后,顺序执行每一个 stage。
stages:
  - build
build-webpack:
  stage: build
  variables:
  CI_REPOSITORY_URL:
    http://$GIT_USERNAME:$GIT_PASSWORD@gitlab.yourdomain.com/$CI_PROJECT_PATH.git
  cache:
    untracked: true
    paths:
      - webpack/node_modules
  script:
    - echo "start build and commit"
    - cd webpack
    1. 可通过关键字优化
    - npm install
    1. 可通过关键字优化
    - npm run build
    - cd ..
    - rm -rf git-dir
    - git clone -b $CI_COMMIT_REF_NAME http://$GIT_USERNAME:$GIT_PASSWORD@gitlab.yourdomain.com/$CI_PROJECT_PATH.git git-dir
    - cd git-dir && rm -rf ./static/dist
    - mv ../static/dist static/
    - git config --global user.name $GITLAB_USER_NAME
    - git config --global user.email $GITLAB_USER_EMAIL
    - git add static/dist
    1. 避免循环构建
    - git commit -m "auto commit [ci skip]`git log -1 --pretty=%B`" || exit 0
    - git push $CI_REPOSITORY_URL $CI_COMMIT_REF_NAME >/dev/null 2>&1 || exit 0
    - echo "end build and commit"
  tags:
    1. 指定 runner
    - linux
    - shell
  only:
    1. 指定分支
    - master
push-to-svn:
  stage: deploy
  variables:
    CI_REPOSITORY_URL:
      http://$GIT_USERNAME:$GIT_PASSWORD@gitlab.yourdomain.com/$CI_PROJECT_PATH.git
  script:
    - echo "start push to svn"
    - echo "step 1/3: git clone"
    - git clone -b $CI_COMMIT_REF_NAME $CI_REPOSITORY_URL git-dir
    - echo "finished"
    - echo "setp 2/3: svn checkout"
    - echo 't' | svn checkout $SVN_PATH svn-dir --username $SVN_USERNAME --password $SVN_PASSWORD --no-auth-cache
    - echo "finshed"
    - echo "step 3/3: push git master to svn trunk"
    1. 确保删除文件操作有效,可优化
    - cd svn-dir && svn delete *
    - rsync -avq git-dir/ svn-dir/
    - cd ./svn-dir
    - svn add * --force
    - echo 't' | svn commit -m "`git log -1 --pretty=%B`" --username $SVN_USERNAME --password $SVN_PASSWORD --no-auth-cache
    - echo "end push to svn"
if [[ $(git log -1 --pretty=%B) = *"["*"install"*"]"* ]]; then npm install; else echo "not npm install"; fi;
if [[ $(git log -1 --pretty=%B) = *"["*"build"*"]"* ]]; then npm run build; else echo "not npm install"; fi;
if [[ $(git log -1 --pretty=%B) = *"["*"delete"*"]"*]]; then cd svn-dir && svn delete * && cd ..; else echo "not svn delete"; fi;
code: test
name: 测试
author: admin
version: 1.2.3
parse_yaml() {
   local prefix=$2
   local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
   sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
        -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $1 |
   awk -F$fs '{
      indent = length($1)/2;
      vname[indent] = $2;
      for (i in vname) {if (i > indent) {delete vname[i]}}
      if (length($3) > 0) {
         vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
         printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
      }
   }'
}
eval $(parse_yaml test.yml "config_")
echo ${config_code}
echo ${config_name}
...
code: test
name: 测试
author: admin
version: 1.2.3
sed -i "/version:/s/[0-9].*$/${VER}/g" test.yml