{"id":237952,"date":"2020-12-18T05:46:00","date_gmt":"2020-12-18T10:46:00","guid":{"rendered":"https:\/\/wordpress-756359-3782526.cloudwaysapps.com\/?p=237952"},"modified":"2024-09-21T04:53:26","modified_gmt":"2024-09-21T04:53:26","slug":"2020-12-18-bash","status":"publish","type":"post","link":"https:\/\/www.travis-ci.com\/blog\/2020-12-18-bash\/","title":{"rendered":"The Cookbook: with Bash"},"content":{"rendered":"\n<p>Many projects on GitHub use Travis to automatically execute certain scripts on every build. Among these many scripts, there is one that\u2019s definitely the most well known, it\u2019s called Bash. Automate your shell commands, add&nbsp;<code>env vars<\/code>, and increase your workflow, plus the endless of other things you can do with Bash given it\u2019s flexibility. At the crux \u2013 Bash on more occasions than not are crucial to Travis CI builds.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getting-started-with-bash-gh-pages-example\">Getting started with Bash (gh-pages example)<\/h2>\n\n\n\n<p>Some goals you may have if you\u2019re wanting to automate processes in Bash are automating&nbsp;<code>GH_REF<\/code>&nbsp;value in gpages_build.sh script with the&nbsp;<code>TRAVIS_REPO_SLUG<\/code>.<\/p>\n\n\n\n<p>In order to make the&nbsp;<code>GH_REF<\/code>&nbsp;variable automatic and not have to use process load everytime, Travis CI gives us the&nbsp;<code>TRAVIS_REPO_SLUG<\/code>&nbsp;which you may have seen in languages like React. The SLUG variable, which is basically the username\/repo for whatever GitHub repo you\u2019re working on. You write it out like this:&nbsp;<code>GH_REF=\"github.com\/${TRAVIS_REPO_SLUG}\"<\/code><\/p>\n\n\n\n<p>Below is an example script of just what I\u2019ve explained above, we can assume this Bash script is entitled&nbsp;<code>pages.sh<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># This script pushes a demo-friendly version of your element and its\n# dependencies to gh-pages.\n\n# usage gp Polymer core-item &#91;branch]\n# Run in a clean directory passing in a GitHub org and repo name\n\n#!\/bin\/bash\n\nGH_REF=\"github.com\/${TRAVIS_REPO_SLUG}\"\n\norg=`echo ${TRAVIS_REPO_SLUG} | cut -f 1 -d \/`\nrepo=`echo ${TRAVIS_REPO_SLUG} | cut -f 2 -d \/`\n\nname=\"Montana\"\nemail=\"montana@travis-ci.org\"\nbranch=${3:-\"master\"} # default to master, when branch isn't specified\n\nmkdir temp &amp;&amp; cd temp # make temp dir \n\n# make folder (same as input, no checking!)\nmkdir $repo\ngit clone \"https:\/\/${GH_TOKEN}@${GH_REF}\" --single-branch # you can theoretically as Montana likes to do, 'git stash pop' here\n\n# switch to gh-pages branch\npushd $repo &gt;\/dev\/null\ngit checkout --orphan gh-pages\n\n# remove all content\ngit rm -rf -q .\n\n# use bower to install runtime deployment\nbower cache clean $repo # ensure we're getting the latest from the desired branch.\ngit show ${branch}:bower.json &gt; bower.json\necho \"{\n  \\\"directory\\\": \\\"components\\\"\n}\n\" &gt; .bowerrc\nbower install\nbower install $org\/$repo#$branch\ngit checkout ${branch} -- demo\nrm -rf components\/$repo\/demo\nmv demo components\/$repo\/\n\n# redirect by default to the component folder\necho \"&lt;META http-equiv=\"refresh\" content=\\\"0;URL=components\/$repo\/\\\"&gt;\" &gt;index.html\n\ngit config user.name $name\ngit config user.email $email\n\n# send it all to github\ngit add -A .\ngit commit -am 'Deploy to GitHub Pages'\ngit push --force --quiet -u \"https:\/\/${GH_TOKEN}@${GH_REF}\" gh-pages &gt; \/dev\/null 2&gt;&amp;1\n\npopd &gt;\/dev\/null<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementing-this-in-your-travisyml\">Implementing this in your&nbsp;<code>.travis.yml<\/code><\/h2>\n\n\n\n<p>So below is a&nbsp;<code>travis.yml<\/code>&nbsp;file I\u2019ve created, and let\u2019s say you named your script&nbsp;<code>pages.sh<\/code>, your .<code>travis.yml<\/code>&nbsp;file would look something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>language: bash\n\nsudo: required\n\nscript:\n    - chmod u+x bash pages.sh<\/code><\/pre>\n\n\n\n<p>There you have it, you have now successfully used Bash in Travis CI. You can do something a little easier as well &#8211; just to test if you have the steps down, maybe make a Bash program just called&nbsp;<code>test.sh<\/code>&nbsp;and have it read:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\necho \"Hello Travis\"<\/code><\/pre>\n\n\n\n<p>Then have your&nbsp;<code>.travis.yml<\/code>&nbsp;file read the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>language: bash\n\nscript: chmod u+x test.sh<\/code><\/pre>\n\n\n\n<p>You should then see when running the build, Travis printing out&nbsp;<code>Hello Travis<\/code>. You now know you\u2019re using Bash successfully within Travis CI.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many projects on GitHub use Travis to automatically execute certain scripts on every build. Among these many scripts, there is one that\u2019s definitely the most well known, it\u2019s called Bash. Automate your shell commands, add&nbsp;env vars, and increase your workflow, plus the endless of other things you can do with Bash given it\u2019s flexibility. At [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":237959,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_breakdance_hide_in_design_set":false,"_breakdance_tags":"","footnotes":""},"categories":[16],"tags":[7,19,20,5],"class_list":["post-237952","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","tag-community","tag-feature","tag-infrastructure","tag-news"],"_links":{"self":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/237952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/comments?post=237952"}],"version-history":[{"count":1,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/237952\/revisions"}],"predecessor-version":[{"id":242660,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/237952\/revisions\/242660"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/media?parent=237952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/categories?post=237952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/tags?post=237952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}