Push to git master branch from Travis CI

2019-09-04 03:48发布

问题:

I am switching from Jenkins to Travis CI. In Jenkins, I did not have to write a script to push my Java/android library to Git master branch. With Travis, all my research shows that I need to write a custom bash script to execute in after_success.
this is my deploy.sh

#!/bin/bash
rev=$(git rev-parse --short HEAD)
git config user.name "uname"
git config user.password "password"
git add .
git commit -m "travis commit at ${rev}"
git push origin master

and my .travis.yml

branches:
  only:
    - development

language: android
sudo: false

android:
  components:
    - build-tools-22.0.1
    - android-22

script:
  - cd appdir
  - ./gradlew test


after_success:
  - cd ..
  - ./deploy.sh

before_cache:
  - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
cache:
  directories:
    - $HOME/.gradle/caches/
    - $HOME/.gradle/wrapper/

under the script section, I cd from root dir to my appdir and run tests from there (successfully) then in the after_success section, I cd back into root where my deploy.sh is located and call it.
My travis console shows everything is successful but I don't see any change in my master branch.
What am I doing wrong?

回答1:

My experience is that Git doesn't appear know the branch your build is using and you want to push the current HEAD that has the additional commit.

In Travis CI you may have a log message just showing 'Everything up-to-date' as Git is pushing the same master branch.

You could change your script to add HEAD.

#!/bin/bash
rev=$(git rev-parse --short HEAD)
git config user.name "uname"
git config user.password "password"
git add .
git commit -m "committed at ${rev}"
git push origin HEAD:master

This is a similar answer that explains :- git-pushing-to-github-origin-master-does-nothing.