<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/letting-travis-keep-a-secret/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://dirk.eddelbuettel.com/blog/2017/02/12#letting_travis_keep_a_secret"> Thinking inside the box </a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>More and more packages, be it for <a target="_blank" rel="nofollow" href="https://www.r-project.org/">R</a> or another language, are now interfacing different application programming interfaces (API) which are exposed to the web. And many of these may require an API key, or token, or account and password.</p>
<p>Which traditionally poses a problem in automated tests such as those running on the popular <a target="_blank" rel="nofollow" href="https://travis-ci.org/">Travis CI</a> service which integrates so well with <a target="_blank" rel="nofollow" href="https://github.com/">GitHub</a>. A case in point is the <a target="_blank" rel="nofollow" href="https://github.com/eddelbuettel/rpushbullet">RPushbullet</a> package where <a target="_blank" rel="nofollow" href="https://github.com/restonslacker">Seth Wenchel</a> and I have been making a few recent changes and additions.</p>
<p>And yesterday morning, I finally looked more closely into providing <a target="_blank" rel="nofollow" href="https://travis-ci.org/">Travis CI</a> with the required API key so that we could in fact run continuous integration with unit tests following each commit. And it turns that it is both <em>easy</em> and <em>quick</em> to do, and yet another great showcase for ad-hoc <a target="_blank" rel="nofollow" href="https://www.docker.com/">Docker</a> use.</p>
<p>The rest of this post will give a quick minimal run-down, this time using the <a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/blog/2017/02/12">gtrendsR</a> package by <a target="_blank" rel="nofollow" href="https://github.com/PMassicotte">Philippe Massicotte</a> and myself. Start by glancing at the <a target="_blank" rel="nofollow" href="https://docs.travis-ci.com/user/encrypting-files/">‘encrypting files’ HOWTO</a> from Travis itself.</p>
<p>We assume you have <a target="_blank" rel="nofollow" href="https://www.docker.com/">Docker</a> installed, and a suitable base package. We will need Ruby, so any base Linux image will do. In what follows, I use Ubuntu 14.04 but many other Debian, Ubunti, Fedora, … flavours could be used provided you know how to pick the relevant packages. What is shown here should work on any recent Debian or Ubuntu flavour ‘as is’.</p>
<p>We start by firing off the Docker engine <em>in the repo directory for which we want to create an encrypted file</em>. The <code>-v $(pwd):/mnt</code> switch mounts the current directory as <code>/mnt</code> in the Docker instance:</p>
<div>
<pre><code><span>edd@max</span>:~/git/gtrendsr(master)$ <span>docker</span> run --rm -ti -v <span>$(</span><span>pwd</span><span>)</span>:/mnt ubuntu:trusty
<span>root@38b478356439</span>:/# apt-get update    <span>## this takes a minute or two</span>
<span>Ign</span> http://archive.ubuntu.com trusty InRelease
<span>Get</span>:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
<span>Get</span>:2 http://archive.ubuntu.com trusty-security InRelease [65.9 kB]
<span># ... a dozen+ lines omitted ...</span>
<span>Get</span>:21 http://archive.ubuntu.com trusty/restricted amd64 Packages [16.0 kB]    
<span>Get</span>:22 http://archive.ubuntu.com trusty/universe amd64 Packages [7589 kB]      
<span>Fetched</span> 22.4 MB in 6min 40s (55.8 kB/s)                                        
<span>Reading</span> package lists... Done
<span>root@38b478356439</span>:/# </code></pre>
</div>
<p>We then install what is needed to actually install the <code>travis</code> (Ruby) gem, as well as <code>git</code> which is used by it:</p>
<div>
<pre><code><span>root@38b478356439</span>:/# apt-get install -y ruby ruby-dev gem build-essential git
<span>Reading</span> package lists... Done
<span>Building</span> dependency tree       
<span>Reading</span> state information... Done
<span>The</span> following extra packages will be installed:
<span># ... lot of output ommitted ...</span>
<span>Processing</span> triggers for ureadahead (0.100.0-16) <span>...</span>
<span>Processing</span> triggers for sgml-base (1.26+nmu4ubuntu1) <span>...</span>
<span>root@38b478356439</span>:/# </code></pre>
</div>
<p>This too may take a few minutes, depending on the networking bandwidth and other factors, and should in general succeed without the need for any intervention. Once it has concluded, we can use the now-complete infrastructure to install the <code>travis</code> command-line client:</p>
<div>
<pre><code><span>root@38b478356439</span>:/# gem install travis
<span>Fetching</span>: multipart-post-2.0.0.gem (100%)
<span>Fetching</span>: faraday-0.11.0.gem (100%)
<span>Fetching</span>: faraday_middleware-0.11.0.1.gem (100%)
<span>Fetching</span>: highline-1.7.8.gem (100%)
<span>Fetching</span>: backports-3.6.8.gem (100%)
<span>Fetching</span>: multi_json-1.12.1.gem (100%
<span># ... many lines omitted ...</span>
<span>Installing</span> RDoc documentation for websocket-1.2.4...
<span>Installing</span> RDoc documentation for json-2.0.3...
<span>Installing</span> RDoc documentation for pusher-client-0.6.2...
<span>Installing</span> RDoc documentation for travis-1.8.6...
<span>root@38b478356439</span>:/#                        </code></pre>
</div>
<p>This in turn will take a moment.</p>
<p>Once done, we can use the <code>travis</code> client to login into GitHub. In my base this requires a password and a two-factor authentication code. Also note that we switch directories first to be in the actual repo we had mounted when launching <code>docker</code>.</p>
<div>
<pre><code><span>root@38b478356439</span>:/# cd /mnt/    <span>## change to repo directory</span>
<span>root@38b478356439</span>:/mnt# travis --login
<span>Shell</span> completion not installed. Would you like to install it now? <span>|y|</span> <span>y</span>
<span>We</span> need your GitHub login to identify you.
<span>This</span> information will not be sent to Travis CI, only to api.github.com.
<span>The</span> password will not be displayed.

<span>Try</span> running with --github-token or --auto if you don<span>'t want to enter your password anyway.</span>

<span>Username: eddelbuettel</span>
<span>Password for eddelbuettel: ****************</span>
<span>Two-factor authentication code for eddelbuettel: xxxxxx</span>
<span>Successfully logged in as eddelbuettel!</span>
<span>root@38b478356439:/mnt# </span></code></pre>
</div>
<p>Now the actual work of encrypting. For this particular package, we need a file <code>.Rprofile</code> containing a short <code>option()</code> segment setting a user-id and password:</p>
<div>
<pre><code><span>root@38b478356439</span>:/mnt# travis encrypt-file .Rprofile
<span>Detected</span> repository as PMassicotte/gtrendsR, is this correct? <span>|yes|</span> 
<span>encrypting</span> .Rprofile for PMassicotte/gtrendsR
<span>storing</span> result as .Rprofile.enc
<span>storing</span> secure env variables for decryption

<span>Please</span> add the following to your build script (before_install stage in your .travis.yml, for instance)<span>:</span>

    <span>openssl</span> aes-256-cbc -K <span>$encrypted_988d19a907a0_key</span> -iv <span>$encrypted_988d19a907a0_iv</span> -in .Rprofile.enc -out .Rprofile -d

<span>Pro</span> Tip: You can add it automatically by running with --add.

<span>Make</span> sure to add .Rprofile.enc to the git repository.
<span>Make</span> sure not to add .Rprofile to the git repository.
<span>Commit</span> all changes to your .travis.yml.
<span>root@38b478356439</span>:/mnt#</code></pre>
</div>
<p>That’s it. Now we just need to follow-through as indicated, committing the <code>.Rprofile.enc</code> file, making sure to <em>not</em> commit its input file <code>.Rprofile</code>, and adding the proper <code>openssl</code> invocation with the <em>keys known only to Travis</em> to the file <code>.travis.yml</code>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://dirk.eddelbuettel.com/blog/2017/02/12#letting_travis_keep_a_secret"> Thinking inside the box </a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/xK-8uHhyZOk"></a></div>