1. PowerShell & Secured Credentials Support
- Check for PowerShell support: Manage Jenkins > Plugin Manager > Installed > set filter = PowerShell > verify that ‘PowerShell plugin’ is on the list. If not, the proceed to part (b). Otherwise, skip to part (c)
- Install PowerShell plugin: Manage Jenkins > Manage Plugins > Available > search for keyword ‘PowerShell’ > select PowerShell > click ‘Download now and install’
- Check for Credentials support: Manage Jenkins > Plugin Manager > Installed > set filter = Credentials > verify that ‘Credentials’ is on the list. If not, the proceed to part (d). Otherwise, skip part (d)
- Install Credentials plugin: Manage Jenkins > Manage Plugins > Available > search for keyword ‘Credentials’ > select Credentials > click ‘Download now and install’ > reoeat for ‘Credentials Binding’ plugin
2. Create a new Jenkins job
- New Item > select FreeStyle project > input a job name > Save > Input some values into the Description field
- Put a check mark next to ‘Discard old builds’ > set Max # of builds to keep = 10
- Select ‘This project is parameterized’ > Add Parameter > choose String Parameter > set name = username, Default value = [username to be called by script] > Add Parameter, again > choose Password Parameter > set name = password, Default value = [password to be called by script]
- Under Build Triggers, put a check mark next to Build periodically > set Schedule =
H/5 * * * *
(every 5 minutes) or0 2 * * *
(everyday at 2am). Here’s a quick overview of Cron expressions on the digit positions:
- MINUTES: Minutes in one hour (0-59)
- HOURS: Hours in one day (0-23)
- DAYMONTH: Day in a month (1-31)
- MONTH: Month in a year (1-12)
- DAYWEEK: Day of the week (0-7) where 0 and 7 are sunday
Notice that the the asterisk ‘*’ in the first position is to be substituted with an ‘H’. That is because a hash of the job name as a time value is used to ensure consistent job starting time as well as ‘spreading the load evenly’ with other job starts to avoid overlapping.
- Under Build section, click Add build step > select PowerShell > set Command = powershell.exe -File [location of the script file] > Save
3. Create a script to peruse Jenkins credentials variables
# Obtain credentials being passed by Jenkins
$username=$env:username
$password=$env:password
$encryptedPassword=ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$encryptedPassword;
# The rest of the script...
Categories: