Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to set up TravisCI for projects that push back to github
# Ruby is our language as asciidoctor is a ruby gem.
lang: ruby
- sudo apt-get install pandoc
- gem install asciidoctor
- make
- .travis/
secure: hZJlqgOzA2zIUJSWIka0PylqNaTkfHq+kS48RrHmocrK0vLyCW7ECWrzez2f2RVdTNzPi0b+yJq2uCbFfWjImZqg+XY1I75/CVVdSYMk7PJkYZ/iBDixMYY8CAkRRd5yZft9uZAdZzR4KLCPN18n7qfISv/M9VA8989NKcVyiEU=
setup_git() {
git config --global ""
git config --global "Travis CI"
commit_website_files() {
git checkout -b gh-pages
git add . *.html
git commit --message "Travis build: $TRAVIS_BUILD_NUMBER"
upload_files() {
git remote add origin-pages https://${GH_TOKEN} > /dev/null 2>&1
git push --quiet --set-upstream origin-pages gh-pages

Pushing to github from TravisCI


I run several repositories that need documentation compiling to distributable formats (e.g. AsciiDoc to HTML, MD to PDF), rather than having to build and commit every time I want to update the distributables, I’d like to automate this process. This is where I use TravisCI as a build server.


Build process

The main steps of the process:

  1. Install software necessary for build

  2. Build software

  3. Set up git with username, email, and remote

  4. Add and commit the files you’ve built

  5. Push these files back to github using your personal access token

Guided tutorial

I’ve got a bunch of asciidoc files inside of that I want to build to save people from having to install asciidoctor or pandoc to regenerate these files.

First I go to and generate a token that I call outreach-resources with the permissions public_repo. This secret token needs to be stored somewhere, I don’t want it to be revealed inside my .travis.yml or on the travis build server. Travis supports encrypted environment variables, so I run the command echo GH_TOKEN=my_github_token | travis encrypt --add where you’d replace my_github_token with the access token generated earlier; this command stores the encrypted github token inside the .travis.yml file.

Now that I’ve got an access token available on travis we can write the script that will push things back to github (checkout I set up the username and email address of the git user on travis, checkout the branch I wish to push to, add the files I want and commit using the environment variable $TRAVIS_BUILD_NUMBER which helps me identify which commits correspond to which builds (totally optional). I finally push this commit back to the repository which takes the form: https://${GH_TOKEN}<user_name>/<repo_name>.git, here GH_TOKEN is substituted inside the build server which acts as a username to the repository with full commit rights!

Travis’s build process is instructed by a file inside your repository named .travis.yml which contains information on the language of the repository, build comamands, dependencies, post build hooks etc. In my YAML file you can see I’m using the hooks before_install, script and after_success, all of which take a command, or a list of commands and execute them. You’ll want to keep the commands outside of the YAML file (i.e. don’t get rid of and put them all in after_success as ${GH_TOKEN} won’t be substituted).


This comment has been minimized.

Copy link

suy commented Jan 2, 2016

This was immensely useful to me. A neat way to get started to use Travis, and get a simple project of mine rendered with the full Asciidoctor instead of "just" what Github serves as README. Thank you!


This comment has been minimized.

Copy link

dhimmel commented Aug 9, 2016

Regarding this line in, what's the purpose of > /dev/null 2>&1? I'm guessing it's to prevent leaking the GH_TOKEN to the Travis logs. However, it seems to have the unintended consequence of suppressing output from subsequent commands. For example, I suspect my git push failed because the repo wan't updated, but I don't see any messages in the Travis log.


This comment has been minimized.

Copy link

zbeekman commented Dec 13, 2016

@dhimmel yes you need to make sure your tokens don't leak. I would also pipe stdout and stderr or all push and clone commands to /dev/null as well, sometimes when they fail git prints the url that was used.


This comment has been minimized.

Copy link

Gopikrishna19 commented Jun 1, 2017

travis has a built-in deploy flow:


This comment has been minimized.

Copy link

twistedpair commented Jan 7, 2018

First I go to and generate a token that I call outreach-resources with the permissions public_repo.

FYI, tokens are really at


This comment has been minimized.

Copy link

isaachier commented Feb 26, 2018


This comment has been minimized.

Copy link

neiljackson1984 commented Apr 26, 2018

Thank you for this excellent summary.


This comment has been minimized.

Copy link

Jezorko commented Jul 21, 2018

Great stuff!

Just to let you know, echo GH_TOKEN=<your_token_here> | travis encrypt --add didn't work for me (probably because I have freshly installed travis), a better alternative is:

travis encrypt GH_TOKEN=<your_token_here> --add

From the docs:


This comment has been minimized.

Copy link

clementauger commented Nov 26, 2018

consider using gh_token=... | travis encrypt --add --no-interactive


This comment has been minimized.

Copy link

FezVrasta commented Jan 24, 2019

I think with this approach anyone can do echo $GH_TOKEN | base64 and retrieve your token


This comment has been minimized.

Copy link

CSchank commented Feb 2, 2019

@FezVrasta On a PR build do you mean? Those builds disallow the use of encrypted environment variables automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.