Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Running Your First PowerShell Scripts With Jenkins and Git

Jenkins is the the top and leading open source automation server, It offers hundreds of plugins to help building (develop), deploying and automating any project. It is a great orchestration tool for automation. In this article I will use it to schedule a simple Job for PowerShell script using Git. 

I have installed Jenkins server on Microsoft Windows OS. To use PowerShell and Jenkins together first I will install the necessary PowerShell plugin in the Jenkins server. After login into Jenkins, Click Manage Jenkins on the dashboard. Select Manage Plugins from list.

Microsoft Powershell Manage Jenkins pipelines devops jobs freestyle job process ci cd configure system nodes and clouds automation.jpg

Under Available plugins, search for PowerShell, Select PowerShell Plugin from list, Either click Install without restart or Download now and install after restart button. PowerShell Plugin allows Jenkins to invoke PowerShell as build scripts. It uses PowerShell.exe on Windows and pwsh on Linux.

hadson jenkins Available plugins Powershell pwsh Microsoft azure devops automation ps1 Git Configuration updates installed plugins manager.jpg

Once the PowerShell plugin is installed successfully, you can find it under Installed plugins option, search for the plugin and verify that it is installed. Make sure it is Enabled.

Installed plugins jenkins azure devops powershell manager updates management configuration automation jobs schedule pipelines ci cd.jpg

I have written a small and one-liner PowerShell script which only echo/print write output Hello World! text on the console. I have pushed all my scripts on the git server (On premises git server), you can check the article Step by Step guide to push your first project to github.com. I will use the configured git URL, I will copy it and use it in the later steps in the Jenkins configuration.

Note: the PowerShell ps1 script name is psjen-demo.ps1.

Microsoft windows browser bonobo git server github gitlab powershell microsoft pwsh jenkins demo azure devops ps1 script schedule jobs configuration.jpg

Next on the Jenkins Dashboard click New Item to create a new PowerShell job.

Microsoft Windows Github bonobo git server jenkins new Item freestyle jobs pipeline parameters people build history project fingerprint manage jenkins pint.jpg

Type enter an item name, Select Freestyle Project (This is the central feature of Jenkins. Jenkins will build your project, combining any SCM with any build system and this can be even used for something other than software build). Click OK button.

Microsoft Powershell Freestyle project jenkins hadson 8080 new job scm git ps1 software build configuration pipeline multi-configuration folder multibranch.jpg

In the freestyle job configuration page, On the General tab Provide job a Description and make sure it is Enabled.

Jenkins powershell demo general sourcecode management build triggers build environment build steps post build actions githunb trottle builds.jpg

Click Source Code Management, choose Git, Enter repository URL as copied earlier from git server. If required add Credentials. Click Save button. If you are seeing git related error make sure you install SCM Git on the operating system where this Jenkins is installed.

Microsoft VMware Soruce Code Management powershell jenkins scm git add credentials build triggers build environment ps1 file.jpg

Next choose Build Steps, and click Add build step, from drop down list select PowerShell option (If you don't see PowerShell option in the drop down list make sure you have installed PowerShell Plugin in Jenkins). 

jenkins add build app build steps powershell bash command ps1 git scm configuration.jpg

In the Command textbox enter the PowerShell ps1 filename as . dot sourced. This ps1 file is the same uploaded on the git server earlier as shown  Step by Step guide to push your first project to github.com. Save the Job. (Instead of script file you can add multiple commands directly)

Microsoft powershell IIS bonobo git server jenkins demo with powershell ps1 script build steps post build actions source code management general post build.jpg

After saving the Project Job, click Build Now button. It will execute the build. Navigate Under Build History, You can see the first task is executed successfully with green check mark.

Jenkins microsoft powershell build now build history jobs filter permalinks project.jpg

After Clicking on the Job, It shows more information and options of executed task. Click Console Output to view more information. I see git fetch is successful and it executed PS1 script file with PowerShell execution is successful and it shows successful message with results.

jenkins powershell console output view as plain text console output jenkins git build data microsoft auzre devops tags fetch git.exe remotes refs origin master .jpg

Useful Articles
How to Install and Use Microsoft PowerShell on Linux
Configure PowerShell remoting between Windows and Linux
Get-PSRepository WARNING Unable to find module repositories
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send
Creating an internal PowerShell module repository
How to sign PowerShell ps1 scripts
PowerShell Convert MAC address to Link-local address IPv6
PowerShell fix repair The trust relationship between this workstation and the primary domain failed
Resolved issue with PowerShell - Trust relationship Rejoin computers in domain without restart
PowerShell Invoke-WebRequest The request was aborted Could not create SSL TLS secure channel
PowerShell Invoke-WebRequest The underlying connection was closed: Could not establish trust relationship for the SSL TLS secure channel.

Go Back

Comment

Blog Search

Page Views

11384436

Follow me on Blogarama