- AWS Account
- IDE
- Install VS Code Extensions (Terraform, terraform-lint)
- Git
- [Python] (https://www.python.org/downloads/)
- [pip] Package Manager(https://pip.pypa.io/en/stable/installation/#get-pip-py)
- Docker Hub Account
- You may clone the sample Python Flask application from this repo:
git clone https://github.com/kbindesh/sample-python-flask-app.git
Note: Update the Docker Hub account details as per your account in Jenkinsfile.
- Install Flask module
pip install Flask
-
Open the application in Visual Studio Code editor.
-
In termial, run below command to run it locally:
python3 app.py
OR
python app.py
- Create a new repo with atleast three branches, namely:
- Development
- Testing
- Production
- Create a new Github repository.
- Check-in the code on feature branch.
- Create a PR to merge the changes on Development/Integration branch.
- Approve the PR request and verify the changes on Development branch.
- AMI: Amazon Linux 2 (you can have any other OS)
- Instance Type: t2.micro
- VPC & Subnet: Default
- Public IP: Enabled
- Security Group: Allow Ingress traffic on SSH(22) and 8080
- Storage: Root Volume(at least 10GB)
- Connect to Jenkins server
- Configure Jenkins On Amazon Linux 2 EC2 Instance
sudo yum install -y java-17-amazon-corretto.x86_64
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
sudo yum -y upgrade
sudo yum install -y jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
-
On other platforms
-
Refer to this link: https://www.jenkins.io/doc/book/installing/
- Execute below commands to install docker:
sudo amazon-linux-extras install -y docker
sudo service docker start
sudo usermod -a -G docker ec2-user
sudo chkconfig docker on
- On Amazon Linux 2/RHEL/CentOS
sudo yum install -y git
- On other OS platforms, refer to this link: https://git-scm.com/downloads
- By default, Amazon Linux 2 comes with Python pre-installed.
- In order to check if the python is present on your machine or not, simply check the python version:
python --version
OR
python3 --version
- In case you do not find any existing python version on the system, to install python (say Python 3.7 version)
sudo yum install python37
- You may download the latest version of Python from this link: https://www.python.org/downloads/
- To check if
pip
package manage already exists on your system:
pip --version
- In case if pip is not present on your machine, download the installation script from https://pypa.io
sudo curl -O https://bootstrap.pypa.io/get-pip.py
- Execute the downloaded script to install latest version of pip package manager:
sudo python3 get-pip.py --user
- Navigate to the Github repo created in Step# 1.3
- Go to Setting tab >> Webhooks >> Add Webhook
- Payload URL: https://<your_jenkins_server_public_ip_or_dns>:8080/github-webhook/
- Content type: application/json
- Which events would you like to trigger this webhook? : Let me select individual events >> Select Pull requests and Pushes checkboxes
- Check the Active checkbox >> Create Webhook
- Access your Jenkins server's dashboard: http://jenkins_server_public_ip_or_dns:8080
- From left-side panel, select Manage Jenkins >> Credentials
- Select scope as System >> Under credentials section, click on Systems >> Global Credentials (Unrestricted) >> New Credential
# Credential (in this lab assuming dockerhub as registry)
Kind: Username and Password
Scope: Global
ID: docker_creds
Username: <YOUR_DOCKER_HUB_USERNAME>
Password: <YOUR_DOCKER_HUB_PASSWORD>
# Add the jenkins user to docker group
sudo usermod -aG docker jenkins
# Restart the the system or restart the jenkins service
sudo shutdown -r now
OR
sudo service jenkins restart
-
Access your Jenkins server's dashboard: http://jenkins_server_public_ip_or_dns:8080
-
From left-side panel, select New item.
-
Jenkins Item Name: say build-docker-img-job-01
-
Type: Pipeline
-
Under Build Triggers section, check GitHub hook trigger for GITScm polling option.
-
Under Pipeline section:
- Definition: Pipeline script from SCM
- SCM: Git
- Repository URL: <YOUR_GITHUB_REPOSITORY_URL>
- Branches to build: <GITHUB_BRANCH_NAME>
- Script Path: Jenkinsfile
- As the github webhook is in place, mere code push or PR merge on main branch will trigger the Jenkins Job.
- From your local system repo, check-in a small change to your github repo.
- Navigate to your dockerhub account, and verify if the docker image got created successfully.