CrazyFrog
CrazyFrog

Reputation: 322

Travis CI - How to push into master branch?

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-pages 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.

Upvotes: 2

Views: 1680

Answers (2)

Jerry Chong
Jerry Chong

Reputation: 9240

Even though @gary wang method is working, there is much simpler method that can push into GitHub master branch directly.

Just add target_branch variable under deploy section, and assign it with master.

Documentation on Travis CI GitHub Pages Deployment: https://docs.travis-ci.com/user/deployment/pages/

Sample contents of .travis.yml:

language: node_js
...
...
...
deploy:
  provider: pages
  skip_cleanup: true
  keep_history: true
  github_token: $github_token  # Your GitHub token set in Travis CI console
  target_branch: master        # Add this line - To push into GitHub master branch
  on:
    branch: staging            # Your GitHub repo default branch

This method is tested and working as per expected.

Upvotes: 2

gary wang
gary wang

Reputation: 21

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 "[email protected]"
      - 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

Upvotes: 2

Related Questions