BookmarkSubscribeRSS Feed

DevOps Applied to SAS 9: SAS Code, GitLab and Jenkins

Started ‎08-19-2020 by
Modified ‎09-07-2020 by
Views 9,270

Where to start when you need to apply DevOps to SAS 9 code development? In this simple example, learn how to bring several pieces together: a SAS 9.4 M6 platform on Windows, Git, a version control system, GitLab, web-based DevOps lifecycle tool and Jenkins, an automation server.

Pre-requisites

The following example assumes a SAS 9.4M6 platform on Windows. You would also need:

Git on Windows

By default, Git is installed on Linux and macOS computers as a command line option. However, Microsoft Windows does not include a Git command. There are also a few ways to install Git on Windows. The most official build is available for download on the Git website. Just go to https://git-scm.com/download/win and the download will start automatically. Note that this is a project called Git for Windows, which is separate from Git itself; for more information on it, go to https://gitforwindows.org.

GitLab repository

GitLab is a web-based DevOps lifecycle tool, using Git technology. You need to set up, or have access to a GitLab repository (GitHub will work in a similar fashion):

1200-SAS9-GitLab-repository.png

The GitLab repository has to be accessible from the SAS 9.4 M6 Windows machine. (In this example, it is hosted on http://<GITSERVER>:<PORT>/gatedemo078/psgel250-devops-applied-to-sas-94m6.git).

Jenkins

Jenkins is an automation server. In Jenkins, you would need to create a Jenkins pipeline, write a pipeline script, and configure a Jenkins agent on the Windows machine where SAS 9.4M6 is running.

Steps

Clone the git repository

On the Windows machine where SAS 9.4 M6 is installed:

  • Create a first folder, for the local git repository: C:\temp\DevOps\git
  • Create a second folder, for the execution results C:\temp\DevOps\batch\
  • Clone the git repository git clone <GITSERVER>:<PORT>/<REPOSITORY>

In Windows, press Start > Command Prompt > write:

cd C:\temp\DevOps\git
git clone http://intviyaXY.race.sas.com:980/gatedemo078/psgel250-devops-applied-to-sas-94m6.git

1210-git-clone-GitLab-repository.png

Write the SAS code

You could use this simple SAS program in a DevOps pipeline:

proc printto print="C:\temp\DevOps\batch\SAScodeoutput.txt" new;run;
proc print data=sashelp.birthwgt (obs=10); run; proc printto; run; quit;

The program will list the output of a dataset to a file. Save the file as SAScode.sas in the location you cloned the git repository: <folder>\git\repository-name\folder\sub-folder i.e., C:\temp\DevOps\git\psgel250-devops-applied-to-sas-94m6\Data-Management\scripts .

Push the code to the remote repository

You now need to synch the local repository with the remote.

In the Command Prompt:

Switch the folder: cd psgel250-devops-applied-to-sas-94m6

Stage the changes (add your new SAS program): git add .

Check the changes to be committed: git status

Commit the changes: git commit -m “New SAS code”

Push the changes remotely: git push origin master

1220-git-add-commit-push-to-GitLab-repository.png

User friendly alternative

If you prefer clicking instead of writing git commands:

  • SAS Studio (from version 3.8) features a Git Integration. You can use it to define and to clone Git repositories.
  • To stage, commit and push you must have SAS Studio hot fix installed.

Create a Jenkins pipeline

1230-Jenkins-pipeline-1.png

 

 

 

1230-Jenkins-pipeline-2.png

Optional, you could set a webhook with your GitLab repository. The advantage is that, when new code is pushed to the remote repository, Jenkins will rebuild a new pipeline. 

 

1230-Jenkins-pipeline-3.png

 

Instruct your pipeline to read the pipeline script from the GitLab repository.

 

1230-Jenkins-pipeline-4.png

 

More details about the pipeline definition in DevOps Applied to SAS Viya 3.5: Run a SAS Program with a Jenkins Pipeline.

Define a Jenkins agent

You also need to allow Jenkins to drive the DevOps pipelines on the Windows machine. You need to set up a Jenkins node, or agent. On the designated machine, Jenkins will install a node service that allows communication back with the controller, the machine (or container) where Jenkins is installed and runs.

 

1250-Jenkins-agent-Windows.png

The agent setup can be tricky, especially a first time.

Create a Jenkinsfile pipeline script

You can create this file directly in the GitLab repository, in the “Data-Management” folder (you specified the location when you created the pipeline).


pipeline {
    agent { label 'sasclient.race.sas.com'}
    stages {
        stage('List Environment variables') {
            steps {
                echo "Running ${env.JOB_NAME} in ${env.WORKSPACE} on ${env.JENKINS_URL}"
            }
        }
        stage('Run SAS program') {
		steps {
			bat '''
			echo "Running SAS 9 code on the Windows machine"
			"C:\\Program Files\\SASHome\\SASFoundation\\9.4\\Sas.exe" -sysin C:\\temp\\DevOps\\workspace\\Data-Management\\scripts\\SAScode.sas -log C:\\temp\\DevOps\\batch\\SAScode.log -nosplash -nologo -icon
			type C:\\temp\\DevOps\\batch\\SAScode.log
			type C:\\temp\\DevOps\\batch\\SAScodeoutput.txt
			'''
			}
        }
    }
    post {
        success {
            echo 'Ran SAS 9.4 artifacts on Windows'
        }
    }
}    

Explanations:

  • Instruct the pipeline to run on the Windows machine (now Jenkins agent) where SAS 9.4M6 is installed: agent { label 'sasclient.race.sas.com'}
  • Environment variables: recover the workspace path defined in the agent ‘Remote root directory’. This is where the code from the GitLab repository will be pulled by Jenkins at build time. ${env.WORKSPACE} corresponds to C:\temp\DevOps\workspace.
  • The SAS program will be pulled from GitLab in the folder C:\temp\DevOps\workspace\PSGEL250-devops-applied-to-sas-viya-3.5\Data-Management\scripts
  • DevOps is about scripting and batch execution. The syntax to run in batch the SAS program is: C:\\Program Files\\SASHome\\SASFoundation\\9.4\\Sas.exe" -sysin C:\\temp\\DevOps\\workspace\\Data-Management\\scripts\\SAScode.sas
  • The execution log is captured in: -log C:\\temp\\DevOps\\batch\\SAScode.log -nosplash -nologo -icon
  • Next, the log is displayed: type C:\\temp\\DevOps\\batch\\SAScode.log
  • Lastly, the SAS program output is displayed: type C:\\temp\\DevOps\\batch\\SAScodeoutput.txt
  • You must use ‘\\’ instead of ‘\’ when referring to Windows paths in a pipeline script.

I strongly advise you to design tests and include them in the pipeline. There are no tests in the pipeline, just to keep the example above simple. Testing will be discussed in a later post.

Build and the result

If you configured a webhook, the Jenkins pipeline will run after each commit in GitLab. The pipeline defined has two stages:

 

Environment variables: Jenkins pulls the code from GitLab in a folder on the Windows machine.

 

1260-Jenkins-pipeline-stages-1.png

1260-Jenkins-pipeline-stages-2.png

Conclusions

The post highlighted the necessary steps to start applying DevOps to SAS 9 code development.

The trick is to keep your code in a version control and instruct an automation server to rebuild when there is a change.

From a technology perspective you combined a SAS 9.4 M6 platform on Windows, with open source tools: Git, a version control system, with GitLab, a web-based DevOps lifecycle tool and Jenkins, an automation server.

The benefits of combining these tools:

  • If your SAS code changes, you only need to push the changes to the remote repository. The automation server rebuilds with the latest changes.
  • If you have new code to integrate, you need to push the changes and add a step in the pipeline script.
  • If you have a new environment and want to execute the code there, you just need to define a new agent in your automation server and adapt the pipeline script.

Additional use-cases might be covered in future posts, such as working with SAS DI jobs or DM Studio jobs.

Acknowledgements

Stephen Foerster, Mark Thomas, Rob Collum.

DevOps Applied to SAS 9 Posts

Git Posts

DevOps Applied to Viya 3.5 Posts

 

Thank you for your time reading this post. Please comment and share your experience with SAS and DevOps.

Comments

Can this be implemented if SAS M6 and Git are all on Linux?

 

Hi @McDiddles : It has to work. Differences: all the batch commands must be replaced by shell or bash. Paths will change.

In fact it will be closer to the SAS Viya on Linux. See DevOps Applied to SAS Viya 3.5: Run a SAS Program with a Jenkins Pipeline .

 

Version history
Last update:
‎09-07-2020 11:05 PM
Updated by:
Contributors

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started