whatwewant / whatwewant.github.io

2 stars 1 forks source link

[CI] travis.ci 坑不少 #6

Open whatwewant opened 6 years ago

whatwewant commented 6 years ago

格式很自由,但是一下是我比较喜欢的方式(.travis.yml)

language: node_js
node_js:
  - 8

jobs:
  include:
    - stage: STAGE_NAME
      before_script:
        - echo "script 1"
        - echo "script 2"
      script:
        - echo "script 3"
        - echo "script 4"
      after_success:
        - echo "script 3"
        - echo "script 4"

      # after_success 执行完才会执行deploy
      deploy: # 支持多个deploy
        - provider: script # 自由脚本provider
          skip_cleanup: true # 保存上面执行过的内容,比如build后的
          script: bash YOUR_SCRIPT.sh # 注意这里的script由于dpl只接受一个字符串,不能像stage的script,否则过不去,坑
          on:
            branch: master  # 只有branch为master的提交才执行这个deploy

        - provider: npm # 内置的provider
          api_key: $NPM_API_KEY
          skip_cleanup: true # 保存上面执行过的内容,比如build后的
          on:
            tags: true  # 只有tag的提交才执行这个deploy
whatwewant commented 6 years ago

什么是stage

jobs:
  - stage: test
     script: echo "script"
   - stage: build
     script: echo "build"
   - stage: deploy
     script: echo "deploy"
   - stage: build
     script: echo "build parallel"

// 执行顺序 test > build = build > deploy,也就是说test 先执行完毕,然后执行build,最后执行deploy,但是两个build是并行执行的
whatwewant commented 6 years ago

什么是 deploy

jobs:
  include:
    - stage: deploy
      script:
        - echo "build for deploy"
        - npm run build
      deploy:
        # deploy to gh-pages
        - provider: script
          skip_cleanup: true
          script: bash deploy.sh
          on:
            tags: true
        # deploy to npm === npm publish
        - provider: npm
          skip_cleanup: true
          api_key: $NPM_API_KEY
          on:
            tags: true
#!/bin/bash
echo "deploy to gh-pages."

rm -rf gh-pages/*
cp -rf dist/* gh-pages
cp CNAME gh-pages
cd gh-pages
git add --all .
git commit -a -m ":sparkles:(TravisCI) automatically update from travis_ci (version: $TRAVIS_TAG)"
git push --quiet "https://${GITHUB_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git" gh-pages:gh-pages
whatwewant commented 6 years ago

script坑 (in stage vs in deploy)

jobs:
  include:
    - stage: deploy
      before_script:
        - echo "before script"
      script:
        - echo "build for deploy"
        - npm run build
      after_success:
        - echo "after script success"
      deploy:
        # deploy to gh-pages
        - provider: script
          skip_cleanup: true
          script: bash script/deploy.sh
          on:
            tags: true
whatwewant commented 6 years ago

深入理解生命周期(LifeCycle)

whatwewant commented 6 years ago

CI初学者的基础与关键

whatwewant commented 6 years ago

Tips

# 1 cache应用 - 缓存node_modules
# 2 skip_cleanup应用 - 保持之前脚本执行环境,默认为false; 当skip_cleanup为false时,Travis CI会清空之前脚本执行结果
# 3 tags应用 - 当on中tags为true时,只允许git tag版本执行部署(deploy)
cache:
  directories:
    - "node_modules"
script: npm test
before_deploy: npm install now --no-save
deploy:
  - provider: script
    script: now --token $NOW_TOKEN && now alias --token $NOW_TOKEN
    skip_cleanup: true
    on:
      tags: true 
whatwewant commented 6 years ago

CI 部署