Day 23 : Jenkins Freestyle Project For DevOps Engineers.
What is CI/CD?
CI/CD is an essential part of DevOps and any modern software development practice. A purpose-built CI/CD platform can maximize development time by improving an organization's productivity, increasing efficiency, and streamlining workflows through built-in automation, testing, and collaboration.
It decreases tedious and time-consuming manual development work and legacy approval processes, freeing DevOps teams to be more innovative in their software development. Automation makes processes predictable and repeatable so that there is less opportunity for error from human intervention.
continuous integration (CI)?
Continuous integration is the practice of integrating all your code changes into the main branch of a shared source code repository. Automatically testing each change when you commit or merge them, an
d automatically kick off a build. With continuous integration, errors and security issues can be identified and fixed more easily, and much earlier in the development process.
continuous delivery (CD)?
Continuous delivery is a software development practice that works in conjunction with CI to automate the infrastructure provisioning and application release process. Once code has been tested and built as part of the CI process, CD takes over during the final stages.
With CD, the software is built so that it can be deployed to production at any time.
What is continuous deployment?
Continuous deployment enables organizations to deploy their applications automatically, eliminating the need for human intervention. With continuous deployment, DevOps teams set the criteria for code releases ahead of time and when those criteria are met and validated, the code is deployed into the production environment.
What is Freestyle Projects ?
A freestyle project in Jenkins is a type of project that allows you to build, test, and deploy software using a variety of different options and configurations. Here are a few tasks that you could complete when working with a freestyle project in Jenkins
Tasks:
01. Create a agent for your app.
To create an agent in Jenkins, you can follow these steps:
Log in to your Jenkins server and navigate to the dashboard.
Click on “Manage Jenkins” from the left-hand menu, then select “Manage Nodes and Clouds”.
Click on the “New Node” button to create a new agent.
Give your new agent a name, such as “ExampleAgent”.
Select “Permanent Agent” as the type of agent.
In the “Remote root directory” field, specify the location on the agent machine where Jenkins should store its files and workspaces.
Under the “Launch method” section, select the method to use to start the agent. For example, you can use the “Launch agents via SSH” method to connect to a remote machine using SSH.
Enter the connection details for the agent machine, such as the IP address, SSH username, and SSH private key.
Click “Save” to create the new agent.
Once you have created your new agent, you can use it in your Jenkins jobs by specifying the “Restrict where this project can be run” option and selecting the name of the agent you just created. Jenkins will then automatically use that agent to run the job on that machine.
To create a new Jenkins Freestyle Project for an app, you can follow these steps:
Log in to your Jenkins server and navigate to the dashboard.
Click on “New Item” from the left-hand menu.
Enter a name for your new project, such as “ExampleApp”.
Select “Freestyle project” as the type of project.
Click “OK” to create the new project.
02. Create a new Jenkins freestyle project for your app.
Once you have created your new project, you can configure it to build your app by following these steps:
On the project configuration page, navigate to the “Source Code Management” section and select the type of source code repository you are using, such as Git or Subversion.
Enter the repository URL and credentials, if required.
Under the “Build Triggers” section, select the trigger type that you want to use to start the build, such as “Build periodically” to run the build on a schedule.
Under the “Build” section, add the build steps that you want to use to build your app. For example, you could add a “Shell script” build step to compile the code and run any tests.
Save your changes to the project configuration.
Once your project is configured, you can manually trigger a build by clicking on the “Build Now” button on the project page, or the build will be triggered automatically based on the build trigger that you selected.
Note that this is just a basic example of how to create a Jenkins Freestyle Project for an app. The exact configuration will depend on your specific app and build process.
03. In the “Build” section of the project, add a build step to run the “docker build” command to build the image for the container.
To add a build step to run the “docker build” command to build the image for the container in your Jenkins Freestyle Project, you can follow these steps:
Navigate to the project configuration page for your Jenkins Freestyle Project.
Scroll down to the “Build” section and click on the “Add build step” button.
Select “Execute shell” from the list of build step options.
In the “Command” field, enter the command to build the Docker image, for example:
docker build -t exampleapp .
This command assumes that you have a Dockerfile in the root of your project directory and that you want to tag the image with the name “exampleapp”. You may need to adjust the command to match the specific requirements of your project.
- Click “Save” to save your changes to the project configuration.
Once you have added this build step, Jenkins will run the “docker build” command during the build process for your project, which will create a Docker image for your app. You can then use additional build steps to push the image to a Docker registry or deploy it to a production environment.
04. Add a second step to run the “docker run” command to start a container using the image created in step 3.
To add a second build step to run the “docker run” command to start a container using the Docker image created in step 3 of your Jenkins Freestyle Project, you can follow these steps:
Navigate to the project configuration page for your Jenkins Freestyle Project.
Scroll down to the “Build” section and click on the “Add build step” button.
Select “Execute shell” from the list of build step options.
In the “Command” field, enter the command to start the Docker container using the image, for example:
docker run -p 8080:80 exampleapp
This command assumes that you want to map port 80 in the container to port 8080 on the host machine and that the Docker image is tagged with the name “exampleapp”. You may need to adjust the command to match the specific requirements of your project.
- Click “Save” to save your changes to the project configuration.
Once you have added this build step, Jenkins will run the “docker run” command during the build process for your project, which will start a Docker container using the image created in the previous build step. You can then access your app by visiting the appropriate URL in a web browser, depending on the port mapping you specified in the “docker run” command.
05. Create Jenkins project to run “docker-compose up -d” command to start the multiple containers defined in the compose file.
To create a Jenkins project to run the “docker-compose up -d” command to start multiple containers defined in a Docker Compose file, you can follow these steps:
Log in to your Jenkins server and navigate to the dashboard.
Click on “New Item” from the left-hand menu.
Enter a name for your new project, such as “ExampleAppCompose”.
Select “Freestyle project” as the type of project.
Click “OK” to create the new project.
On the project configuration page, navigate to the “Source Code Management” section and select the type of source code repository you are using, such as Git or Subversion.
Enter the repository URL and credentials, if required.
Under the “Build” section, click on the “Add build step” button and select “Execute shell”.
In the “Command” field, enter the command to start the Docker Compose services, for example:
docker-compose up -d
This command assumes that you have a Docker Compose file named “docker-compose.yml” in the root of your project directory. You may need to adjust the command to match the specific requirements of your project.
- Click “Save” to save your changes to the project configuration.
Once you have added this build step, Jenkins will run the “docker-compose up -d” command during the build process for your project, which will start the Docker Compose services defined in the Compose file. You can then access your app by visiting the appropriate URL in a web browser, depending on the ports and URLs defined in the Compose file.
Note that this is just a basic example of how to create a Jenkins project to run the “docker-compose up -d” command. The exact configuration will depend on your specific app and Docker Compose setup. You may also need to add additional build steps to perform tasks such as building Docker images or running tests.
06. Set up a cleanup step in the Jenkins project to run “docker-compose down” command to stop and remove the containers defined in the compose file.
Here’s an example of how to set up a cleanup step in a Jenkins project to run the docker-compose down
command:
Open your Jenkins project and click on the “Configure” option.
Scroll down to the “Build” section and click on the “Add build step” dropdown.
Select “Execute shell” from the dropdown.
In the “Command” field, enter the following command:
docker-compose down
This command will stop and remove the containers defined in the docker-compose.yml
file.
5. Click “Save” to save the changes to the Jenkins project.
That’s it! Now, when your Jenkins project finishes running, it will execute the docker-compose down
command to clean up the containers. This will ensure that any resources used by the containers are freed up and can be used by other processes.