The Cookbook: with Bash

By

Dec 18, 2020

Dec 18, 2020

Contents

Many projects on GitHub use Travis to automatically execute certain scripts on every build. Among these many scripts, there is one that’s definitely the most well known, it’s called Bash. Automate your shell commands, add env vars, and increase your workflow, plus the endless of other things you can do with Bash given it’s flexibility. At the crux – Bash on more occasions than not are crucial to Travis CI builds.

Getting started with Bash (gh-pages example)

Some goals you may have if you’re wanting to automate processes in Bash are automating GH_REF value in gpages_build.sh script with the TRAVIS_REPO_SLUG.

In order to make the GH_REF variable automatic and not have to use process load everytime, Travis CI gives us the TRAVIS_REPO_SLUG which you may have seen in languages like React. The SLUG variable, which is basically the username/repo for whatever GitHub repo you’re working on. You write it out like this: GH_REF="github.com/${TRAVIS_REPO_SLUG}"

Below is an example script of just what I’ve explained above, we can assume this Bash script is entitled pages.sh:

# This script pushes a demo-friendly version of your element and its
# dependencies to gh-pages.

# usage gp Polymer core-item [branch]
# Run in a clean directory passing in a GitHub org and repo name

#!/bin/bash

GH_REF="github.com/${TRAVIS_REPO_SLUG}"

org=`echo ${TRAVIS_REPO_SLUG} | cut -f 1 -d /`
repo=`echo ${TRAVIS_REPO_SLUG} | cut -f 2 -d /`

name="Montana"
email="[email protected]"
branch=${3:-"master"} # default to master, when branch isn't specified

mkdir temp && cd temp # make temp dir 

# make folder (same as input, no checking!)
mkdir $repo
git clone "https://${GH_TOKEN}@${GH_REF}" --single-branch # you can theoretically as Montana likes to do, 'git stash pop' here

# switch to gh-pages branch
pushd $repo >/dev/null
git checkout --orphan gh-pages

# remove all content
git rm -rf -q .

# use bower to install runtime deployment
bower cache clean $repo # ensure we're getting the latest from the desired branch.
git show ${branch}:bower.json > bower.json
echo "{
  \"directory\": \"components\"
}
" > .bowerrc
bower install
bower install $org/$repo#$branch
git checkout ${branch} -- demo
rm -rf components/$repo/demo
mv demo components/$repo/

# redirect by default to the component folder
echo "<META http-equiv="refresh" content=\"0;URL=components/$repo/\">" >index.html

git config user.name $name
git config user.email $email

# send it all to github
git add -A .
git commit -am 'Deploy to GitHub Pages'
git push --force --quiet -u "https://${GH_TOKEN}@${GH_REF}" gh-pages > /dev/null 2>&1

popd >/dev/null

Implementing this in your .travis.yml

So below is a travis.yml file I’ve created, and let’s say you named your script pages.sh, your .travis.yml file would look something like this:

language: bash

sudo: required

script:
    - chmod u+x bash pages.sh

There you have it, you have now successfully used Bash in Travis CI. You can do something a little easier as well – just to test if you have the steps down, maybe make a Bash program just called test.sh and have it read:

#!/bin/bash

echo "Hello Travis"

Then have your .travis.yml file read the following:

language: bash

script: chmod u+x test.sh

You should then see when running the build, Travis printing out Hello Travis. You now know you’re using Bash successfully within Travis CI.

Written By

Reviewed By