Skip to content

Instantly share code, notes, and snippets.

@serefarikan
Forked from willprice/.travis.yml
Created January 16, 2018 20:23
Show Gist options
  • Save serefarikan/dffafb721a893258af0435f15c4b925a to your computer and use it in GitHub Desktop.
Save serefarikan/dffafb721a893258af0435f15c4b925a to your computer and use it in GitHub Desktop.
How to set up TravisCI for projects that push back to github
# Ruby is our language as asciidoctor is a ruby gem.
lang: ruby
before_install:
- sudo apt-get install pandoc
- gem install asciidoctor
script:
- make
after_success:
- .travis/push.sh
env:
global:
secure: hZJlqgOzA2zIUJSWIka0PylqNaTkfHq+kS48RrHmocrK0vLyCW7ECWrzez2f2RVdTNzPi0b+yJq2uCbFfWjImZqg+XY1I75/CVVdSYMk7PJkYZ/iBDixMYY8CAkRRd5yZft9uZAdZzR4KLCPN18n7qfISv/M9VA8989NKcVyiEU=
#!/bin/sh
setup_git() {
git config --global user.email "[email protected]"
git config --global user.name "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}@github.com/MVSE-outreach/resources.git > /dev/null 2>&1
git push --quiet --set-upstream origin-pages gh-pages
}
setup_git
commit_website_files
upload_files

Pushing to github from TravisCI

Introduction

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.

Requirements

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 https://github.com/MVSE-Outreach/resources that I want to build to save people from having to install asciidoctor or pandoc to regenerate these files.

First I go to https://github.com/settings/applications 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 push.sh). 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}@github.com/<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 push.sh commands outside of the YAML file (i.e. don’t get rid of push.sh and put them all in after_success as ${GH_TOKEN} won’t be substituted).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment