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