Skip to main content

Deploying a Custom HTTPD Image with Your Own Web Content Using Jenkins Github Webhooks

In this article, we’ll guide you through deploying a custom HTTPD (Apache) Docker image containing your own web content using a Jenkins Freestyle project. This setup allows for easy automation and deployment every time you update your web content in a GitHub repository.


Prerequisites


Before we begin, ensure you have:

- A GitHub account and a repository for your web content.
- Jenkins installed and running.
- Docker installed on your Jenkins server.
- Basic knowledge of Docker and Jenkins.

Step 1: Prepare Your Web Content


1. Create Your Web Content:
   - On your local machine, create a directory for your web content.
   - Inside this directory, create an `index.html` file or any additional files you want to serve.


   <!DOCTYPE html>
   <html lang="en">
   <head>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>My Custom Web Page</title>
   </head>
   <body>
       <h1>Welcome to My Custom Web Page!</h1>
       <p>This is served from a Docker container.</p>
   </body>
   </html>


2. Push Your Web Content to GitHub:
   - Create a new GitHub repository and push your web content to it.

Step 2: Create a Dockerfile


1. Create a Dockerfile:

   - In the same directory as your web content, create a `Dockerfile` to build your custom HTTPD image.


   # Use the official HTTPD image
   FROM httpd:2.4

   # Copy the web content into the container
   COPY ./ /usr/local/apache2/htdocs/


2. Test the Docker Image Locally (Optional):
   - You can build and test the image locally to ensure it works as expected. Run the following commands:

   docker build -t my-custom-httpd .
   docker run -d -p 8080:80 my-custom-httpd


   - Access it in your browser at `http://localhost:8080`.

Step 3: Set Up Jenkins Freestyle Project


Step 3.1: Create a New Jenkins Job


1. Open Jenkins:
   - Navigate to your Jenkins dashboard.

2. Create a New Freestyle Project:
   - Click on New Item, enter a name for your job, select **Freestyle project**, and click **OK**.

Step 3.2: Configure Source Code Management


1. Source Code Management:
   - Under the Source Code Management section, select Git.
   - Enter your GitHub repository URL and any necessary credentials.

Step 3.3: Configure Build Triggers


1. Build Triggers:

   - Check the box for GitHub hook trigger for GITScm polling. This will enable Jenkins to trigger a build when changes are pushed to the repository.

Step 3.4: Configure Build Steps


1. Build Steps:
   - In the Build section, click Add build step and select Execute shell.

2. Shell Command:
   - Enter the following commands to build and run your Docker container:

     # Build the Docker image
   docker build -t my-custom-httpd .

   # Stop any running container with the same name
   docker stop my-custom-httpd || true

   # Remove the container if it exists
   docker rm my-custom-httpd || true

   # Run the Docker container
   docker run -d --name my-custom-httpd -p 8080:80 my-custom-httpd
 

Step 3.5: Save Configuration


1. Save:

   - Click Save to save your job configuration.

Step 4: Set Up GitHub Webhook


1. Go to Your GitHub Repository:

   - Click on the Settings tab of your repository.

2. Webhooks:

   - Click on Webhooks in the left sidebar and then on Add webhook.

3. Payload URL:
   - Enter your Jenkins URL followed by `/github-webhook/`.
   - Example: `http://your-jenkins-url/github-webhook/`.

4.Content Type:
   - Set to `application/json`.

5. Which events would you like to trigger this webhook?:
  
- Select Just the push event.

6. Create Webhook:
   -
Click Add webhook.

Step 5: Test the Setup


1. Make Changes to Your Web Content:

   - Update your web content in the GitHub repository.

2. Push Changes:
   - Push the changes to GitHub.

3. Check Jenkins:

   - Go to your Jenkins job page. You should see a new build triggered by the push.

4. Verify Deployment:

   - After the build is successful, access your web application at `http://<your-jenkins-url>:8080`.

Conclusion


Congratulations! You’ve set up a Jenkins Freestyle project to build and deploy a custom HTTPD Docker image containing your own web content. With GitHub webhooks in place, any changes you make to your repository will automatically trigger a new build and deployment.

This automated setup not only streamlines your development workflow but also ensures that your updates are quickly reflected in your web application. Happy coding!

Comments

Popular posts from this blog

Example of Maven project that interacts with a MySQL database and includes testing

Example Maven project that interacts with a MySQL database and includes testing To install Java, MySQL, Maven, and write a Java program to fetch table data, execute, and create a JAR file using Maven on Ubuntu, you can follow these steps: Step 1: Install Java You can install Java using the following commands: sudo apt update sudo apt install default-jre sudo apt install default-jdk Verify the installation by running: java -version Step 2: Install MySQL You can install MySQL using the following commands: sudo apt update sudo apt install mysql-server During the installation, you'll be prompted to set a root password for MySQL or you can set password at latter stage using following steps.  sudo mysql ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; exit Step 3: Install Maven You can install Maven using the following commands: sudo apt update sudo apt install maven Verify the installation by running: mvn -version Step 4: Create

Maven Create and Build Artifacts

In Maven, you can create and build artifacts using the package phase of the build lifecycle. The package phase is responsible for taking the compiled code and other project resources and packaging them into a distributable format, such as a JAR (Java Archive), WAR (Web Application Archive), or other custom formats. Here are the steps to create and build artifacts using Maven: Configure the Build Output: In your project's pom.xml file, you need to configure the output of the build. This includes specifying the type of artifact you want to create (e.g., JAR, WAR) and any additional resources to include. You do this in the <build> section of your pom.xml: <build>     <finalName>my-artifact</finalName> <!-- Name of the artifact without the extension -->     <plugins>         <!-- Plugin configurations for creating the artifact -->         <!-- For example, maven-jar-plugin or maven-war-plugin -->     </plugins> </build> Depend

Maven Repositories (local, central, global)

Maven relies on repositories to manage dependencies, plugins, and other artifacts required for a project. There are typically three types of repositories in Maven: local, central, and remote/global repositories. Local Repository: Location: The local repository is located on your local development machine. By default, it's in the .m2 directory within your user home directory (e.g., C:\Users\<username>\.m2\repository on Windows or /Users/<username>/.m2/repository on macOS and Linux). Purpose: The local repository is used to store artifacts (JARs, POMs, and other files) that your machine has downloaded or built during previous Maven builds. These artifacts are specific to your local development environment. Benefits: Using a local repository improves build performance since it caches dependencies locally, reducing the need to download them repeatedly. It also ensures reproducibility by maintaining a local copy of dependencies. Central Repository: Location: The central repo