Skip to content

Instantly share code, notes, and snippets.

@willprice
Last active June 15, 2024 04:29
Show Gist options
  • Save willprice/e07efd73fb7f13f917ea to your computer and use it in GitHub Desktop.
Save willprice/e07efd73fb7f13f917ea 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 "travis@travis-ci.org"
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/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).

@notedit
Copy link

notedit commented Jun 20, 2019

it seems this does not work anymore.

@TobiTenno
Copy link

yeah, i've started getting password prompts

@lermana
Copy link

lermana commented Jan 2, 2020

Thank you for this great run through! I had to make a couple of changes that I wanted to transmit:

  1. I had to chmod the push script:
chmod +x .push.sh; ./.push.sh
  1. I prefaced the origin addition with a clause that removes an existing origin if it's already there:
  if  git ls-remote --exit-code origin > /dev/null 2>&1; then
	  git remote rm origin
  fi

@Keimeno
Copy link

Keimeno commented Jan 8, 2020

Doesn't work for me this way anymore.
As @lermana mentioned you have to give travis execute permissions for the push.sh script at first. (Happened to me when creating the file on Windows.).
Also if you want to push it, it only works when entering:

rev=$(git rev-parse --short HEAD)
git push myOrigin HEAD:myBranch

as it would otherwise say, that there were no changes

@capsulecorplab
Copy link

Has anyone encountered an unbound variable error with GH_TOKEN? https://travis-ci.com/capsulecorplab/code-coffee-compendium/builds/149233745#L214

@kubk
Copy link

kubk commented Feb 23, 2020

In 2020 Travis CI provides this functionality out of the box: https://docs.travis-ci.com/user/deployment/pages/
I configured automatic push of my userscript with a minimal effort here: https://github.com/kubk/github-package-links/blob/master/.travis.yml#L16

@Phil-Barber
Copy link

In case anyone else experiences issues with this step:
echo GH_TOKEN=my_github_token | travis encrypt --add
(was getting the error The input stream is exhausted 😴 )
I found using travis encrypt --add --interactive sorted me out

@ogerardin
Copy link

I prefaced the origin addition with a clause that removes an existing origin if it's already there

You can also use the full URL directly in place of a remote name in git commands, so you don't have to worry about that.

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