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.
-
Github repo with TravisCI support enabled - https://travis-ci.org/profile/<YOUR_GITHUB_USERNAME>;, flick the enable switch
-
Github personal access token - https://github.com/settings/applications
-
Travis gem -
sudo gem install travis
The main steps of the process:
-
Install software necessary for build
-
Build software
-
Set up git with username, email, and remote
-
Add and commit the files you’ve built
-
Push these files back to github using your personal access token
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/tokens 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).
@FezVrasta On a PR build do you mean? Those builds disallow the use of encrypted environment variables automatically.