top of page
  • Writer's picturePierrick Kinif

How to Use Variable Environments for Running Rscripts on Jenkins

Updated: Apr 10, 2023


Jenkins is a versatile tool that allows you to automate software development processes. Its wide range of plugins and integrations makes it an excellent choice for building, testing, and deploying code. In this article, we'll show you how to optimize your workflow by using Jenkins to run an R script stored on GitHub that uses the Sys.getenv() function.


Requirements

Before we begin, here are the requirements:

  • A Jenkins server with R installed.

  • An R script stored on a GitHub repository (let's call it test.R).

  • A GitHub account with read access to the repository containing the R script.

  • Familiarity with the .Renviron file in your R environment.


Step 1: Configure a Jenkins job

The first step is to configure a Jenkins job to run the R script. This job must have read access to the GitHub repository. Here's how:

  1. Log in to your Jenkins server and click "New Item" on the left-hand side.

  2. Enter a name for your new job and select "Freestyle project" as the job type. Click "OK" to create the job.


Step 2: Prepare test.R

Now let's add some code to the test.R script and push it to GitHub:


THIS_IS_SECRET <- 111
cat("The secret is:", THIS_IS_SECRET)


Step 3: Configure the Jenkins job

Once you have configured the Jenkins job and added the code to GitHub, you must configure the job to run the R script. Here's how:

  1. In the job configuration page, scroll down to the "Build" section and click "Add build step."

  2. Select "Execute a script shell" from the dropdown list.

  3. In the field, enter the following code:


Rscript test.R


Step 4: Add a password parameter in Jenkins

To avoid storing your GitHub password in plain text, we'll use a password parameter in Jenkins. Here's how:

  1. In the job configuration page, scroll down to the "Build" section and click "Add Parameter."

  2. Select "Password Parameter" from the dropdown list.

  3. Enter a name for the parameter, such as "SECRET."

  4. Click "Save."


Step 5: Modify the R script

Finally, we'll modify the test.R script to use Sys.getenv() to retrieve the GitHub password. Here's how:


THIS_IS_SECRET <- Sys.getenv("THIS_IS_SECRET")
cat("The secret is:", THIS_IS_SECRET)

When you launch the Jenkins job, the secret will be hidden in Jenkins and used as an environment variable.




Use case

Using variable environments for running Rscripts on Jenkins is useful when you need to automate your workflow, such as running your R code on a schedule or when multiple developers are working on the same project. It lets you keep your sensitive information secure and avoid exposing passwords and other secrets in plain text. By using Jenkins and Sys.getenv(), you can easily integrate your R scripts with your software development pipeline and improve your workflow.



Conclusion

In this article, we have shown you how to use variable environments for running Rscripts on Jenkins. You can create a secure and efficient workflow for your R projects by following the steps outlined above. By using Jenkins and Sys.getenv(), you can automate your R scripts and ensure that your sensitive information is kept safe. With this setup, you can focus on writing quality R code, while Jenkins takes care of the rest.



41 views0 comments

Recent Posts

See All

Opmerkingen


bottom of page