Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    Top Cooking Websites For Food Bloggers

    Katy Perry Goes To Space!

    Mr. Meowski’s Bakery To Re-Locate In St. Charles MO

    Facebook X (Twitter) Instagram
    Tech Empire Solutions
    • Home
    • Cloud
    • Cyber Security
    • Technology
    • Business Solution
    • Tech Gadgets
    Tech Empire Solutions
    Home » How to configure Jenkins using PowerShell
    Tech Gadgets

    How to configure Jenkins using PowerShell

    techempireBy techempire2 Comments6 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Telegram Email

    Are you transitioning from Windows system administrator to DevOps engineer and want to continue using your PowerShell skills? One way to do this is to set up Jenkins to use PowerShell.

    When setting up a Jenkins job, you can execute Linux shell or Windows bash commands to automate tasks. However, the preset does not include executing PowerShell commands.

    The default Jenkins job creation step does not include PowerShellThe default Jenkins job creation step does not include PowerShell

    Fortunately, there is a way to make PowerShell appear as an option in a Jenkins job. This guide describes the steps to execute PowerShell commands as part of the Jenkins build step.

    I use the Jenkins FreeStyle project for demonstration purposes in this guide. Note that in production you may not be able to use this due to limitations of this job type.

    Please follow the steps below to start using PowerShell in Jenkins job automation:

    Step 1: Install the Jenkins PowerShell plugin

    1. Log in to your Jenkins server and click Manage Jenkins => Plugins
    How to set up Jenkins to use PowerShell: Step 1: Install the Jenkins PowerShell pluginHow to set up Jenkins to use PowerShell: Step 1: Install the Jenkins PowerShell plugin
    1. To install the Jenkins PowerShell plug-in, click the “Available plug-ins” node in the left pane. Then, type “powershell” into the search bar, and finally select the box next to the PowerShell plug-in and click “Install.”
    Configuring Jenkins to use PowerShellConfiguring Jenkins to use PowerShell
    1. Finally, wait for the plugin to install, scroll down, and click “Back to Home.”

    Step 2: Test the Jenkins PowerShell plugin

    After completing the first step of setting up Jenkins to use PowerShell, let’s test it out. To achieve this, we will set up a Jenkins FreeStyle job and add a PowerShell command.

    1. To create a FreeStyle project, click + New Project or Create Job + from the Jenkins dashboard.
    Configure Jenkins to use PowerShellConfigure Jenkins to use PowerShell
    1. After that, name the Jenkins job, select the Freestyle project, and click OK. Jenkins will build the job and open it for editing.
    After that, give the Jenkins job a name, select the Freestyle project and click OKAfter that, give the Jenkins job a name, select the Freestyle project and click OK
    1. Scroll down to the “Build Steps” section of the operation and click the “New Build Step” drop-down list.
    How to configure Jenkins using PowerShellHow to configure Jenkins using PowerShell
    1. As the screenshot below confirms, PowerShell is now available as an option! Select it.
    1. Finally, enter a simple command in the PowerShell field – Get-ChildItem /etc/ – and click save button.

    6. Return to the Jenkins project, click the “Build Now” button, and then select “Schedule Build”.

    The build will fail. To view build details, scroll down to Build History and click on one of the operations.

    Configuring Jenkins to use PowerShellConfiguring Jenkins to use PowerShell

    Reviewing the job’s build console output can reveal why it failed. The job attempts to execute a PowerShell (pwsh) command, but PowerShell is not installed on the Jenkins server.

    This is why the job fails. To solve this problem, we need to install PowerShell on the Jenkins server.

    The next section explains the steps to achieve this.

    Step 3: Install PowerShell on the Jenkins server

    The final step in setting up Jenkins to use PowerShell is to install PowerShell on the Jenkins host server.

    If Jenkins is executed directly on the Ubuntu server, PowerShell must be installed on the server. However, if you start Jenkins as a Docker container, you must install PowerShell on the container.

    In my case, my Jenkins server will run as a Docker container, so I will install PowerShell on the container. Here’s a step-by-step walkthrough:

    1. Log in to the Jenkins Docker container as root by executing the following “Docker exec” command:
    sudo docker ps #returns the container ID
    sudo docker exec -u 0 -it 977a52341f9a bash #logs into the container as root

    The Docker exec command requires a container ID. The “docker ps” command returns this information.

    The “-u 0” parameter allows you to log in as root. On the other hand, “-it” lets you log in “interactively”.

    The string 977a52341f9a is the ID of my Jenkins container, and adding “bash” at the end will turn on the BASH prompt character. This is the output of the command.

    The part of the screenshot I marked (1) shows that I’m logged into my Docker container – 977a52341f9a is my container’s ID. Also, the # symbol means I’m logged in as root.

    1. To install PowerShell, we first need to determine the Linux distribution executing on the Docker container. To obtain this information, execute the following command:
    cat /etc/os-release

    This command confirms that my container is running Debian Linux Distro.

    This information will help me identify the Microsoft script used to install PowerShell. So, since the distribution of my container is Dabien, I will use the Installing on Debian script.

    Here is the modified script – I removed “sudo” as we don’t need it on the container – we are logged in as root!

    ###################################
    # Prerequisites
    
    # Update the list of packages
    apt-get update
    
    # Install pre-requisite packages.
    apt-get install -y wget
    
    # Download the PowerShell package file
    wget https://github.com/PowerShell/PowerShell/releases/download/v7.4.0/powershell_7.4.0-1.deb_amd64.deb
    
    ###################################
    # Install the PowerShell package
    dpkg -i powershell_7.4.0-1.deb_amd64.deb
    
    # Resolve missing dependencies and finish the install (if necessary)
    apt-get install -f
    
    # Delete the downloaded package file
    rm powershell_7.4.0-1.deb_amd64.deb
    
    # Start PowerShell
    pwsh

    Before proceeding, execute the “apt update” command to update the package manager.

    Copy the above script, paste it into the container’s console, and press Enter.

    When you execute this command, you will be prompted to confirm the installation. Enter Y and press Enter.

    When the script finishes running, it will open the PowerShell console to confirm that we have successfully installed PowerShell on the Docker container.

    1. To exit PowerShell and return to the container’s shell, execute the “exit” command. Additionally, to exit the container, execute “exit” again.

    Step 4: Rebuild the Jenkins job and test

    Now that we have PowerShell installed on the Jenkins server (a Docker container in my case), we can rebuild the job and see if it runs successfully.

    1. Log in to the Jenkins server through a browser and open the PowerShell project.
    2. After that, click the “Build Now” button and select “Schedule Build.”
    1. When the build job has finished running, scroll down to the Build History section. The build should now be successful. Click on the last build job.
    1. Finally, click on “Console Output”

    The details of the build operation confirm that the “Get-ChildItem” command ran successfully!

    Although the commands we used for this demonstration are simple, it demonstrates that after setting up Jenkins to use PowerShell, you can use PowerShell commands to automate Jenkins jobs.

    We hope you’ll take your DevOps skills to the next level by reading this guide. We’d love to hear your thoughts.

    Use the comment form at the end of this page to share your thoughts or ask questions about this topic or any DevOps issue.

    Other useful resources

    1. Jenkins PowerShell plugin
    2. What is the difference between Freestyle and Pipeline in Jenkins?
    3. Install on Debian 10 or 11 via package repository
    4. How to install Jenkins on Ubuntu using Docker containers

    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    techempire
    • Website

    Related Posts

    8Bitdo’s Ultimate Controller with Charging Dock is back on sale for $56

    Meater Plus smart meat thermometer price drops to record low

    Meta’s Threads gets its own Tweetdeck clone

    YouTube reportedly agrees to block videos of Hong Kong protest songs in the region

    EA Sports Dormant college football will resurface like a cicada on July 19

    OpenAI reaches agreement to put Reddit posts into ChatGPT

    Leave A Reply Cancel Reply

    Top Reviews
    Editors Picks

    Top Cooking Websites For Food Bloggers

    Katy Perry Goes To Space!

    Mr. Meowski’s Bakery To Re-Locate In St. Charles MO

    Pokémon Trading Card Website Making 100k!

    Legal Pages
    • About Us
    • Disclaimer
    • DMCA
    • Privacy Policy
    Our Picks

    Edufox

    Emerging Academic Education Platforms – Sponsored By Edufox

    GTA 6 Release Date

    Top Reviews

    Type above and press Enter to search. Press Esc to cancel.