travis-ci how to push to master branch

只愿长相守 提交于 2019-12-08 00:23:44

问题


I have a travis-ci project connected to Github that tries to update content in the Github repo and push them back to Github, both master and gh-page branches.

However, although my travis-ci log files says everything is ok, I only see the gh-pages branch updated, but not the master branch.

My travis.yml file is

language: node_js
node_js: stable

language: python
python: 3.6

# Travis-CI Caching
cache:
  directories:
    - node_modules
    - pip

# S: Build Lifecycle
install:
  - npm install
  - npm install -g gulp
  - python -m pip install requests
  - python -m pip install bs4
  - python -m pip install lxml

before_script:
  - cd archive_builder
  - python build_archive.py
  - cd ..

script:
  - gulp dist

after_script:
  - cd dist
  - git init
  - git config user.name "my git name"
  - git config user.email "my git email"
  - git add -A
  - git commit -m "travis -- update gh-page"
  - git push --force --quiet "https://${GH_TOKEN}@${GH_REF}" master:gh-pages
  - sh ../purgeCF.sh $CF_ZONE $CF_KEY $CF_EMAIL

  - cd ..
  - git add -A
  - git commit -m "travis -- update master files"
  - git push --quiet "https://${GH_TOKEN}@${GH_REF}" HEAD:master

# E: Build LifeCycle

branches:
  only:
    - master
env:
 global:
   - GH_REF: github.com/mygitname/myprojectname.git

In this script, I first update and build website sourcefiles with gulp, storing them into "dist" folder. Then I push content in "dist" to my gh-pages branch, and push everything else to my master branch.

The credentials are stored as security keys with Travis and should work correctly.

To push "dist/", I created a new ".git/" under "dist/" and force push it as new.

To push everything else, I could not do it because the root repository already contains ".git" folder and I do not want to lose my previous commits. It should work.

Thanks for help.


回答1:


I found most articles or answers were talking about how to deploy to gh-pages branch, and most ways is not work for me , i debug this issue on travis for several days, i will list key steps about how to push to master brach on travis

e.g. Below is my doc repository script, travis will update readme.md automated.

  1. Generate github token, you can refer to the article https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/

  2. Set Environment Variables

    GH_REF githu.com/clownvary/docs.git your repository address

    GITHUB_API_KEY *********** your token generated on step 1

  3. Script

    os: osx
    language: node_js
    cache:
      directories:
        - node_modules
    node_js:
      - 'lts/*'
    before_install:
      - git pull
      - brew install tree
    install:
      - npm install
    script:
      - npm run updateReadme
    after_success:
      - git config user.email "travis@travis.org"
      - git config user.name "travis" # this email and name can be set anything you like
      - git add README.md
      - git commit --allow-empty -m "updated README.md"
      - git push https://clownvary:${GITHUB_API_KEY}@${GH_REF} HEAD:master  #clownvary is my username on github, you need to use yourself , do not use travis or others.
    

Hope this can help you



来源:https://stackoverflow.com/questions/51925941/travis-ci-how-to-push-to-master-branch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!