Thursday, April 4, 2013

Check for Authorized Jenkins Builds

This is a PowerShell script to prevent an unauthorized user to run a job in Jenkins. I like to use a single script with parameters to deploy code in each environment. But the script could be used by a developer to deploy code from a Jenkins instance in test directly to production.

To get around this, I added code to check the JENKINS_URL environment variable. If a user on a test Jenkins instance tries to deploy to production, the script will immediately stop the deploy.

$env is a reserved variable by PowerShell that imports the JENKINS_URL variable without passing it to the script.

# Check Jenkins URL
$JENKINS_URL = $env:JENKINS_URL
if (($rngenv -eq "prod") -and ($JENKINS_URL -ne "http://jenkinswin.fbfs.com/jenkins")) {
Write-Host "Production deploys must be done from a production Jenkins instance"
Exit}

No comments:

Post a Comment