Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Docker Image CI

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Build the Docker image
run: docker build . --file ./docker/Dockerfile --tag my-image-name:$(date +%s)
105 changes: 105 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
pipeline {
agent any

environment {
// Use the Jenkins SSH credentials ID for your EC2 key
SSH_KEY = 'WebAppPaC'
EC2_USER = 'ubuntu'
EC2_HOST = '3.131.141.176'
REPO_URL = 'https://github.com/designking06/DevOpsProjCert.git'
APP_DIR = '/home/ubuntu/DevOpsProjCert' // repo clone location on slave
}

stages {
stage('Prepare Slave Node') {
steps {
sshagent (credentials: [SSH_KEY]) {
sh """
ssh -o StrictHostKeyChecking=no ${EC2_USER}@${EC2_HOST} '
# Ensure directories exist
mkdir -p ${APP_DIR}
cd ${APP_DIR}

# Update system and install prerequisites
sudo apt update
sudo apt install -y git python3 python3-pip docker.io

# Install ansible if not present
if ! command -v ansible-playbook > /dev/null; then
sudo apt install -y ansible
fi
'
"""
}
}
}

stage('Clone Repository') {
steps {
sshagent (credentials: [SSH_KEY]) {
sh """
ssh -o StrictHostKeyChecking=no ${EC2_USER}@${EC2_HOST} '
if [ ! -d "${APP_DIR}/.git" ]; then
git clone ${REPO_URL} ${APP_DIR}
else
cd ${APP_DIR} && git pull
fi
'
"""
}
}
}

stage('Install Docker via Ansible') {
steps {
sshagent (credentials: [SSH_KEY]) {
sh """
ssh -o StrictHostKeyChecking=no ${EC2_USER}@${EC2_HOST} '
cd ${APP_DIR}/ansible
ansible-playbook install_docker.yml -i ${EC2_HOST}, --user ${EC2_USER} --private-key /dev/null
'
"""
}
}
}

stage('Deploy PHP Docker Container') {
steps {
sshagent(['WebAppPaC']) {
sh """
ssh -o StrictHostKeyChecking=no ubuntu@3.131.141.176 '
# Clone repo if it doesn'"'"'t exist, else pull latest
if [ ! -d /home/ubuntu/DevOpsProjCert ]; then
git clone https://github.com/designking06/DevOpsProjCert.git /home/ubuntu/DevOpsProjCert
else
cd /home/ubuntu/DevOpsProjCert && git pull
fi

cd /home/ubuntu/DevOpsProjCert

# Build Docker image from the docker/ subdirectory
sudo docker build -t webapp:latest -f docker/Dockerfile .

# Stop and remove old container if it exists
if [ \$(sudo docker ps -aq -f name=webapp) ]; then
sudo docker stop webapp
sudo docker rm webapp
fi

# Run new container
sudo docker run -d -p 80:80 --name webapp webapp:latest
'
"""
}
}
}
}
post {
success {
echo 'Pipeline completed successfully!'
}
failure {
echo 'Pipeline failed. Check logs.'
}
}
}
21 changes: 21 additions & 0 deletions ansible/install_docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
- name: Install Docker on Test Server
hosts: test
become: true
tasks:
- name: Install dependencies
apt:
name:
- ca-certificates
- curl
- gnupg
state: present

- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present

- name: Install Docker
apt:
name: docker-ce
state: present
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM devopsedu/webapp
COPY app/ /var/www/html/
EXPOSE 80
CMD ["apache2-foreground"]
10 changes: 10 additions & 0 deletions puppet/docker_agent.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class docker_agent {
package { 'puppet-agent':
ensure => installed,
}

service { 'puppet':
ensure => running,
enable => true,
}
}
9 changes: 9 additions & 0 deletions website/readme.md → readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ If you are a beginner and would like me to explain something in the code, or if
## Lisence

MIT

## DevOps
Connecting to EC2
1. Locally, run
- chmod 400 ~/.ssh/DevOpsProj.pem
- ssh -i ~/.ssh/DevOpsProj.pem ubuntu@3.131.141.176

2. In ssh, run
- sudo apt update