Inserting security in Github pull requests! — Part 2 (using Github Actions)

Inserting security in GitHub pull requests! — Part 2 (using GitHub Actions)

This post builds up in a previous post about inserting code analysis into GitHub pull requests, in this post, we will focus on implementing this workflow based on GitHub Actions for a Java project— “actual code snippets and video to follow”

We can configure the entire workflow for analyzing a Java app in four simple steps.

Add “ShiftLeft code analysis” into your build action.

You can create a specific action (coming soon) or you may simply invoke code analysis from build Action workflow. In this case we can assume that we have a “main.yml” workflow file in your source code repository in the path


in your main.yml file, you can add following snippet to follow your build job. Essentially this snippet adds a small code to execute a shell script that runs the analysis.

Now, we need to add a file names with the below code to the root of the project code repository (you can change the name if you do make the change in above snippet too).

PS:- Add your JAR/WAR file name analyze code step

PULL_REQUEST=$(curl "$GITHUB_REPO/pulls?state=open" \
-H "Authorization: Bearer $GITHUB_TOKEN" | jq ".[] | select(.merge_commit_sha==\"$GITHUB_SHA\") | .number")

echo "Got pull request $PULL_REQUEST for branch $GITHUB_BRANCH"
# Install ShiftLeft
curl > /tmp/sl.tar.gz && sudo tar -C /usr/local/bin -xzf /tmp/sl.tar.gz
# Analyze code
sl analyze --version-id "$GITHUB_SHA" --tag branch="$GITHUB_BRANCH" --app "$GITHUB_PROJECT" --java --cpg --wait <jar/war file name>
# Run Build rule check  
BUILDRULECHECK=$(sl check-analysis --app "$GITHUB_PROJECT" --branch "$GITHUB_BRANCH")
if [ -n "$BUILDRULECHECK" ]; then
PR_COMMENT="Build rule failed, click here for vulnerability list! - $URL"
curl -XPOST "$GITHUB_REPO/issues/$PULL_REQUEST/comments" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"body\": \"$PR_COMMENT\"}"
exit 1
PR_COMMENT="Build rule succeeded, click here for vulnerability list! - $URL"
curl -XPOST "$GITHUB_REPO/issues/$PULL_REQUEST/comments" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"body\": \"$PR_COMMENT\"}"
exit 0

The previous steps require that you have an appropriate ShiftLeft Inspect license and have added the necessary tokens (Org ID, Access Token, and API Token) as environment variables for your GitHub pipelines. You also need to configure a GITHUB token in GitHub for API access. The script above also assumes you have configured your build rules as explained below.

Configure Build rules

ShiftLeft Inspect has a powerful build rule feature where you can define the security approval conditions for a merge/pull request. To invoke it during the merge request approval process, check-in a file named shifleft.yml in the root repository of your project code repository. A sample code snippet for shiftleft.yml file is below.

Configure static analysis and run as mandatory status check for GITs

Making mandatory checks on GIT through branch protection

A Git administrator can configure any tool to be run as part of a pull request as mandatory status-check. ShiftLeft Inspect code analysis as a mandatory status-check can be enabled through branch protection rules.

Bringing it all together

Now we are all set. Here is a quick video of the workflow in action.

You can try ShiftLeft for free by signing up here.

Inserting security in Github pull requests! — Part 2 (using Github Actions) was originally published in ShiftLeft Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.

*** This is a Security Bloggers Network syndicated blog from ShiftLeft Blog - Medium authored by Alok Shukla. Read the original post at: