Welcome back to the #90DaysOfDevOps series! Today's focus is on Lab 14 - Automate Deployment with Jenkins. In this hands-on guide, I'll walk through the steps to set up Jenkins for automating your deployment process.
Why Automate Deployment?
Before we get started, let's quickly revisit the importance of automating deployment in your DevOps pipeline. Automating deployment with tools like Jenkins not only saves time but also reduces the risk of human error, ensuring consistent and reliable releases. By automating the deployment process, you empower your team to deliver software faster and more efficiently.
Prerequisites
Make sure you have the following components set up before we dive into the deployment automation:
Jenkins Server: Ensure Jenkins is installed and accessible. You can download it from the official website (https://www.jenkins.io/download/) and follow the installation instructions.
Docker: Install Docker on your server or local machine. You can find installation guides for various platforms on the Docker website (https://docs.docker.com/get-docker/).
Sample Application: Have a sample application ready in a version-controlled repository. For this guide, I'll be using a simple Python Flask application.
Step 1: Create a Dockerfile
Navigate to your application's root directory and create a Dockerfile. This file contains instructions for building a Docker image of your application.
# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 5000 available to the world outside this container
EXPOSE 5000
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
Adjust the FROM image, package installation, and CMD based on your application's requirements.
Step 2: Create a requirements.txt File
In the same directory as your Dockerfile, create a requirements.txt file listing the Python packages required for your application.
Flask==1.1.2
Adjust the package versions based on your application's dependencies.
Step 3: Build the Docker Image
Open a terminal and navigate to your application's directory. Run the following command to build the Docker image:
docker build -t your-image-name .
Replace your-image-name with a meaningful name for your Docker image.
Step 4: Push the Docker Image to a Registry (Optional)
If you're using a Docker registry like Docker Hub, push your image to the registry for accessibility by Jenkins.
docker login
docker tag your-image-name your-docker-username/your-image-name
docker push your-docker-username/your-image-name
Step 5: Set Up Jenkins Job
1. Open Jenkins in your web browser and log in.
2. On the Jenkins dashboard, click on "New Item" to create a new job.
3. Enter a name for your job, and select "Freestyle project" as the project type.
4. Scroll down to the "Source Code Management" section:
If your code is in a Git repository, select "Git" and provide the repository URL.
Configure the credentials if needed.
Specify the branch you want to build.
5. In the "Build" section:
Click on "Add build step" and choose "Execute shell" (for Unix-like systems) or "Execute Windows batch command" (for Windows).
In the command box, enter the Docker build commands:
cd path/to/your/application
docker build -t your-image-name .
docker push your-docker-username/your-image-name
Replace path/to/your/application, your-image-name, and your-docker-username with your actual application path, Docker image name, and Docker Hub username.
6. Optionally, if you pushed your Docker image to a registry, add a post-build action:
Click on "Add post-build action" and choose "Send build artifacts over SSH."
Configure the SSH server (host, username, and password or private key) to transfer files to your server.
In the "Source files" field, enter the path to your Docker Compose file or any other necessary files.
Set the "Remote directory" to the location on your server where you want to deploy the application.
7. Save your Jenkins job configuration.
Step 6: Trigger the Jenkins Job
Now, let's trigger the Jenkins job manually:
1. From the Jenkins dashboard, find your newly created job and click on its name.
2. On the job page, click on "Build Now" on the left side. This will manually start the build process.
3. Monitor the build progress in the Jenkins console output. If everything is configured correctly, you should see the Docker build and push steps executed successfully.
Congratulations! You've successfully set up a Jenkins job to automate the deployment of your application. You can further enhance this setup by configuring webhooks to trigger builds automatically when changes are pushed to your version control system.
Keep exploring, and stay tuned for more in the #90DaysOfDevOps series!
Thank you for reading!
*** Explore | Share | Grow ***
Comentarios