How To Create a Jenkins Build Job to Automate Web Deployments

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

Jenkins is a continuous integration and deployment automation server, which helps developers manage their builds. Jenkins combines the multi-step build process with advanced scheduling tools to allow for better quality control, faster turnaround times, and fewer bugs in the final product.

Jenkins is a popular open-source automation tool that allows developers to build, test and release software. Jenkins also has the ability to create jobs which can automate web deployments. This article will show you how to create a Jenkins job that will automatically deploy your website when it’s ready. Read more in detail here: jenkins automation scripts.

How To Create a Jenkins Build Job to Automate Web Deployments

It might be tough to create and deploy an application. Why not automate the whole procedure with a Jenkins Build Job? You may focus on development while Jenkins takes care of the code deployment.

In this article, you’ll learn how to use Jenkins to automate your code deployment so you can concentrate on other things at the same time.

Ready? It’s time to get right in!

Prerequisites

Hands-on demos are included in this lesson. If you want to follow along, make sure you have the following items on hand:

Although the repository in the images below changes somewhat from the one given above, you will use a different repository URL once forked.

How to Use Docker Compose with Volumes to Run Jenkins in Docker

Creating a Build Job in Jenkins

Jenkins is a free and open-source automation server for automating the software development life cycle. Building, testing, and deploying are all part of this life cycle, which allows for continuous integration and delivery.

In this demonstration, you’ll learn how to set up a Jenkins build job. A build job is a setup for performing tasks for building/packaging software independently.

  1. Log in to your Jenkins dashboard by opening your preferred browser and going to http://localhost:8080/. The Jenkins dashboard is accessible on port 8080 by default.

2. In the left side of your Jenkins Dashboard, select the New Item button to begin generating a build task. This causes the browser to be sent to a new job page.

Creating a Jenkins Job from Scratch Creating a Jenkins Job from Scratch

3. In the next step, choose a project name for the build task in the input space below. The project name in this case is webDeploy.

To allow generating jobs, scripting post-build activities, and configuring project build triggers, choose the Freestyle project option, then click OK. After that, you’ll be sent to the task setup page, which you’ll see in the following step.

Putting together a Jenkins project Putting together a Jenkins project

4. Go to the General page and click the GitHub project box to tell Jenkins where to get your code from. For the example project available on GitHub that you cloned previously, this demo utilizes Git as the source code management. In the Project url area below, paste the GitHub repository URL from your GitHub account.

1647497104_793_How-To-Create-a-Jenkins-Build-Job-to-Automate-Web Connecting Jenkins to a source code management system

5. Finally, pick Git as your source code manager by clicking on the Source Code Management option.

In the input area, paste this GitHub repository URL (https://github.com/Claradev32/webDeploy.git). To store your current settings, click the Save button.

Jenkins and Source Code Manager are linked together (Git) Jenkins and Source Code Manager are linked together (Git)

Creating Jenkins Project Credentials

If you’re working on a client project, you may choose to keep your repository private. If that’s the case, you’ll need to set up credentials for Jenkins to use to clone your project from GitHub.

To setup Jenkins project credentials, you must first produce and upload an SSH key to your GitHub account:

1. Open your Ubuntu terminal and perform the following command to produce an SSH key with the key type ed25519. To add your GitHub email address as a remark to the SSH key you’ll produce, replace [email protected] with your GitHub email address.

2. Copy and paste the following into your public SSH key:

  • Enter a place for the SSH key to be saved, and then press Enter. If you don’t provide a location, the program stores the SSH key in the default location, which is shown below.
  • Enter a safe password for the SSH key, then click Enter to confirm the password. The output of your public SSH key will appear.
  • Take note of where your created SSH key is located since you’ll need it later to add it to your GitHub account.

The SSH key has a Password added to it. The SSH key has a Password added to it.

3. Open your web browser and go to your Account Settings page on GitHub.

4. On the left panel, select the SSH and GPG keys link, then New SSH Key, as shown below, to setup your new SSH key.

Creating an SSH key for Github Creating an SSH key for Github

5. Finally, give the SSH key a name that you choose. Only for internal documentation reasons is the title used. Paste your SSH key into the Key input area from the previously saved file, and then click Add SSH Key to store the key.

It’s important to remember that this is your public key, not your private key!

 New SSH Key is being saved. New SSH Key is being saved.

Jenkins Credentials: Adding SSH Private Key

You must also add your SSH private key to your Jenkins credentials to permit Jenkins access to your GitHub repository now that you’ve produced your SSH key pair to use as a Jenkins credential. But first, make a note of the SSH URL for your GitHub repository.

1. Go to your GitHub repository page, select Code SSH, and then copy the SSH URL to the clipboard using the copy icon displayed below.

Making a note of the GitHub SSH URL Making a note of the GitHub SSH URL

2. Next, go to your Jenkins dashboard and choose Manage Jenkins to go to the project settings page.

Getting to Jenkins' Options Getting to Jenkins’ Options

3. To see a list of your Jenkins credentials, scroll down and pick the Manage Credentials option under the Security section.

Jenkins Credentials Access Jenkins Credentials Access

4. On the Credentials page, click Jenkins to go to the “System” tab, where you may create new credentials.

Adding Credentials to the Page of the System Adding Credentials to the Page of the System

5. Hover your cursor over the Global credentials (unrestricted) option on the Page of the System to see the drop-down menu, then click the arrow down icon.

From the drop-down option, choose Add credentials. The website will redirect to a form where you may set up the new credentials that will be shown in the following step.

Page of the SystemPage of the System

6. In the first input area, choose SSH Username with private key, and then enter a project username in the Username field. The username in this case is CI/CD.

The username is only for internal documentation and is not used for authentication in this situation.

Jenkins Global Credentials: Creating a Username Jenkins Global Credentials: Creating a Username

7. To see your SSH Private key, use the command below, adjusting the location to where your key is kept, as shown below (the key below is located in the home directory). In the following step, you’ll add your SSH Private key to your Jenkins credentials.

Secret Key is being viewed. Secret Key is being viewed.

8. Return to the Global credentials (unrestricted) page after you’ve written down your SSH Private key.

Enter the passphrase you set on the “Creating Jenkins Project Credentials” section (step two) to the Passphrase input field, and click OK. Doing so authorizes Jenkins access to your GitHub repository.

If you didn’t provide a password when establishing the SSH key, type it in and then click the Add button. To store the credentials, type your SSH Private key into the Key input area, as shown below, and click OK.

Jenkins Credentials: Adding GitHub SSH Key Jenkins Credentials: Adding GitHub SSH Key

8. Return to your Jenkins dashboard, hover your cursor over your project, click the arrow down symbol, and then choose Configure, as shown below. The page will take you to the setup page for your project.

Jenkins Project Configuration Jenkins Project Configuration

Finally, in the dropdown box, pick the credentials username (from step six of the “Adding Secret Key to Jenkins Credentials” section). Jenkins uses the CID concept by default to provide continuous integration and deployment of your project.

Jenkins and GitHub integration Jenkins and GitHub integration

Build Triggers and Post-Build Actions Configuration

You’ve now completed the skeleton of a construction project. Will you have to manually relaunch your project if you make changes to it on a frequent basis? Why don’t you create build triggers? Build triggers reinstall your project’s modifications posted to GitHub automatically.

1. On the Build triggers tab of your project’s setup page, check the GitHub hook trigger for GITScm polling option. This option enables you to automatically redeploy updates to your project.

Setting up build triggers Setting up build triggers

2. Select Execute shell from the Add build step drop-down menu on the Build tab to write a script that will execute whenever Jenkins downloads a new version of your code.

Putting Together a Build Script Putting Together a Build Script

Your development environment has an impact on the shell script you write. If you’re using Windows, the script will run, and the same is true for other operating systems.

3. To save the build script, type in the shell instructions below and click Save. The page returns to your project’s dashboard when you save the modifications.

The scripts below grab (pull) your code from a remote location (origin master) and install (npm install) the dependencies needed to execute your project using NodeJS NPM. When you send a push request to your source code management, the commands run.

This project makes use of NodeJS for dependencies, but yours may not.

npm install git pull origin master

The Automated Build Script: Adding Commands The Automated Build Script: Adding Commands

4. To start the build, go to your project’s dashboard and click Build Now.

Executing the Build Executing the Build

5. To view the status of a build, scroll down to the build history. If the build went well, you’ll see something similar to the screenshot below.

Viewing a Completed Project Viewing a Completed Project

If the build fails, you’ll get the following output.

History of failed buildsHistory of failed builds

6. To set the project’s post-build steps, scroll ahead and select Configure.

Configuring Project's Post Build Actions Configuring the Post-Build Actions for a Project

7. Select Git publisher from the Post Build Actions menu, then click Add post-build action. The Git Publisher settings section is expanded on this page (next step).

Getting to the Post-Build Action in Git Publisher Getting to the Post-Build Action in Git Publisher

8. Finally, activate the Push Only If Build Succeeds option and save your changes by clicking the Save button. When you choose this option, Jenkins will only deploy the code if the build runs successfully.

Adding Post-Construction Actions Adding Post-Construction Actions

Conclusion

You’ve now completed this lesson by connecting Jenkins to your source code management system. You’ve also set up build triggers to automate web deployments and established a build script that runs automatically when Jenkins downloads a new version of your code.

Now, why not save yourself some time by using a Jenkins build task to automate your application deployment?

How to Set Up a New Jenkins Build Agent is a related topic.

Jenkins is a continuous integration and delivery tool that allows users to automate web deployments. This article will teach you how to create a Jenkins build job that automatically deploys code to servers. Reference: how does jenkins automatically deploy code to servers.

Related Tags

  • jenkins build once deploy many
  • jenkins deploy plugin
  • automate weblogic deployment using jenkins
  • jenkins pipeline deploy multiple environments
  • jenkins deploy to remote server