From 31eb338055f125b46e163df81751985710fc65c0 Mon Sep 17 00:00:00 2001 From: Caleb Crawley Date: Wed, 17 Dec 2025 14:06:57 -0500 Subject: [PATCH 1/7] initial changes to repo, add DevOps --- website/Jenkinsfile | 55 +++++++++++++++++++++++++ website/ansible/install_docker.yml | 21 ++++++++++ website/{ => app}/config.php | 0 website/{ => app}/content/404.php | 0 website/{ => app}/content/about-us.php | 0 website/{ => app}/content/contact.php | 0 website/{ => app}/content/home.php | 0 website/{ => app}/content/products.php | 0 website/{ => app}/functions.php | 0 website/{ => app}/index.php | 0 website/{ => app}/template/template.php | 0 website/docker/Dockerfile | 4 ++ website/puppet/docker_agent.pp | 10 +++++ website/readme.md | 9 ++++ 14 files changed, 99 insertions(+) create mode 100644 website/Jenkinsfile create mode 100644 website/ansible/install_docker.yml rename website/{ => app}/config.php (100%) rename website/{ => app}/content/404.php (100%) rename website/{ => app}/content/about-us.php (100%) rename website/{ => app}/content/contact.php (100%) rename website/{ => app}/content/home.php (100%) rename website/{ => app}/content/products.php (100%) rename website/{ => app}/functions.php (100%) rename website/{ => app}/index.php (100%) rename website/{ => app}/template/template.php (100%) create mode 100644 website/docker/Dockerfile create mode 100644 website/puppet/docker_agent.pp diff --git a/website/Jenkinsfile b/website/Jenkinsfile new file mode 100644 index 00000000..789ca662 --- /dev/null +++ b/website/Jenkinsfile @@ -0,0 +1,55 @@ +pipeline { + agent any + + triggers { + pollSCM('H/2 * * * *') + } + + stages { + + stage('Checkout Code') { + steps { + git branch: 'master', url: 'https://github.com/designking06/DevOpsProjCert.git' + } + } + + stage('Install Puppet Agent (Mocked)') { + steps { + sh ''' + ssh -i ~/.ssh/DevOpsProj.pem ubuntu@EC2_PUBLIC_IP << EOF + echo "Simulating Puppet Agent install" + sudo apt update + sudo apt install -y puppet-agent || true + EOF + ''' + } + } + + stage('Run Ansible (Dry Run)') { + steps { + sh ''' + ansible-playbook ansible/install_docker.yml \ + -i EC2_PUBLIC_IP, \ + --user ubuntu \ + --private-key ~/.ssh/DevOpsProj.pem \ + --check + ''' + } + } + + stage('Docker Build (Mocked)') { + steps { + sh ''' + echo "docker build -t php-webapp ." + echo "docker run -d php-webapp" + ''' + } + } + } + + post { + failure { + sh 'echo "Rollback: removing container (mock)"' + } + } +} diff --git a/website/ansible/install_docker.yml b/website/ansible/install_docker.yml new file mode 100644 index 00000000..ac74937d --- /dev/null +++ b/website/ansible/install_docker.yml @@ -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 diff --git a/website/config.php b/website/app/config.php similarity index 100% rename from website/config.php rename to website/app/config.php diff --git a/website/content/404.php b/website/app/content/404.php similarity index 100% rename from website/content/404.php rename to website/app/content/404.php diff --git a/website/content/about-us.php b/website/app/content/about-us.php similarity index 100% rename from website/content/about-us.php rename to website/app/content/about-us.php diff --git a/website/content/contact.php b/website/app/content/contact.php similarity index 100% rename from website/content/contact.php rename to website/app/content/contact.php diff --git a/website/content/home.php b/website/app/content/home.php similarity index 100% rename from website/content/home.php rename to website/app/content/home.php diff --git a/website/content/products.php b/website/app/content/products.php similarity index 100% rename from website/content/products.php rename to website/app/content/products.php diff --git a/website/functions.php b/website/app/functions.php similarity index 100% rename from website/functions.php rename to website/app/functions.php diff --git a/website/index.php b/website/app/index.php similarity index 100% rename from website/index.php rename to website/app/index.php diff --git a/website/template/template.php b/website/app/template/template.php similarity index 100% rename from website/template/template.php rename to website/app/template/template.php diff --git a/website/docker/Dockerfile b/website/docker/Dockerfile new file mode 100644 index 00000000..c853fa27 --- /dev/null +++ b/website/docker/Dockerfile @@ -0,0 +1,4 @@ +FROM devopsedu/webapp +COPY app/ /var/www/html/ +EXPOSE 80 +CMD ["apache2-foreground"] diff --git a/website/puppet/docker_agent.pp b/website/puppet/docker_agent.pp new file mode 100644 index 00000000..7412fe85 --- /dev/null +++ b/website/puppet/docker_agent.pp @@ -0,0 +1,10 @@ +class docker_agent { + package { 'puppet-agent': + ensure => installed, + } + + service { 'puppet': + ensure => running, + enable => true, + } +} diff --git a/website/readme.md b/website/readme.md index 9ac786cf..f67e94b9 100644 --- a/website/readme.md +++ b/website/readme.md @@ -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 \ No newline at end of file From 27a6c478002d81ce5a5d6eb38635543f4dc1ae0d Mon Sep 17 00:00:00 2001 From: Caleb Crawley Date: Wed, 17 Dec 2025 14:30:39 -0500 Subject: [PATCH 2/7] Add IP --- website/Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/Jenkinsfile b/website/Jenkinsfile index 789ca662..45757eba 100644 --- a/website/Jenkinsfile +++ b/website/Jenkinsfile @@ -16,7 +16,7 @@ pipeline { stage('Install Puppet Agent (Mocked)') { steps { sh ''' - ssh -i ~/.ssh/DevOpsProj.pem ubuntu@EC2_PUBLIC_IP << EOF + ssh -i ~/.ssh/DevOpsProj.pem ubuntu@3.131.141.176 << EOF echo "Simulating Puppet Agent install" sudo apt update sudo apt install -y puppet-agent || true From 50779987aee5c0c6cdfd36a4ff0a59edaafe09be Mon Sep 17 00:00:00 2001 From: Caleb Crawley Date: Wed, 17 Dec 2025 14:33:20 -0500 Subject: [PATCH 3/7] Move files outside of website folder --- website/Jenkinsfile => Jenkinsfile | 0 {website/ansible => ansible}/install_docker.yml | 0 {website/app => app}/config.php | 0 {website/app => app}/content/404.php | 0 {website/app => app}/content/about-us.php | 0 {website/app => app}/content/contact.php | 0 {website/app => app}/content/home.php | 0 {website/app => app}/content/products.php | 0 {website/app => app}/functions.php | 0 {website/app => app}/index.php | 0 {website/app => app}/template/template.php | 0 {website/docker => docker}/Dockerfile | 0 {website/puppet => puppet}/docker_agent.pp | 0 website/readme.md => readme.md | 0 14 files changed, 0 insertions(+), 0 deletions(-) rename website/Jenkinsfile => Jenkinsfile (100%) rename {website/ansible => ansible}/install_docker.yml (100%) rename {website/app => app}/config.php (100%) rename {website/app => app}/content/404.php (100%) rename {website/app => app}/content/about-us.php (100%) rename {website/app => app}/content/contact.php (100%) rename {website/app => app}/content/home.php (100%) rename {website/app => app}/content/products.php (100%) rename {website/app => app}/functions.php (100%) rename {website/app => app}/index.php (100%) rename {website/app => app}/template/template.php (100%) rename {website/docker => docker}/Dockerfile (100%) rename {website/puppet => puppet}/docker_agent.pp (100%) rename website/readme.md => readme.md (100%) diff --git a/website/Jenkinsfile b/Jenkinsfile similarity index 100% rename from website/Jenkinsfile rename to Jenkinsfile diff --git a/website/ansible/install_docker.yml b/ansible/install_docker.yml similarity index 100% rename from website/ansible/install_docker.yml rename to ansible/install_docker.yml diff --git a/website/app/config.php b/app/config.php similarity index 100% rename from website/app/config.php rename to app/config.php diff --git a/website/app/content/404.php b/app/content/404.php similarity index 100% rename from website/app/content/404.php rename to app/content/404.php diff --git a/website/app/content/about-us.php b/app/content/about-us.php similarity index 100% rename from website/app/content/about-us.php rename to app/content/about-us.php diff --git a/website/app/content/contact.php b/app/content/contact.php similarity index 100% rename from website/app/content/contact.php rename to app/content/contact.php diff --git a/website/app/content/home.php b/app/content/home.php similarity index 100% rename from website/app/content/home.php rename to app/content/home.php diff --git a/website/app/content/products.php b/app/content/products.php similarity index 100% rename from website/app/content/products.php rename to app/content/products.php diff --git a/website/app/functions.php b/app/functions.php similarity index 100% rename from website/app/functions.php rename to app/functions.php diff --git a/website/app/index.php b/app/index.php similarity index 100% rename from website/app/index.php rename to app/index.php diff --git a/website/app/template/template.php b/app/template/template.php similarity index 100% rename from website/app/template/template.php rename to app/template/template.php diff --git a/website/docker/Dockerfile b/docker/Dockerfile similarity index 100% rename from website/docker/Dockerfile rename to docker/Dockerfile diff --git a/website/puppet/docker_agent.pp b/puppet/docker_agent.pp similarity index 100% rename from website/puppet/docker_agent.pp rename to puppet/docker_agent.pp diff --git a/website/readme.md b/readme.md similarity index 100% rename from website/readme.md rename to readme.md From c6714bb3aeb778bab45b6440c971c6f9d30acb21 Mon Sep 17 00:00:00 2001 From: Caleb Crawley Date: Wed, 17 Dec 2025 14:52:21 -0500 Subject: [PATCH 4/7] Remove absolute paths for file refs --- Jenkinsfile | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 45757eba..780d7217 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -16,23 +16,20 @@ pipeline { stage('Install Puppet Agent (Mocked)') { steps { sh ''' - ssh -i ~/.ssh/DevOpsProj.pem ubuntu@3.131.141.176 << EOF - echo "Simulating Puppet Agent install" - sudo apt update - sudo apt install -y puppet-agent || true - EOF - ''' - } + ssh -i ~/.ssh/DevOpsProj.pem ubuntu@3.131.141.176 \ + "sudo apt update && sudo apt install -y puppet-agent" + '''} } - stage('Run Ansible (Dry Run)') { + stage('Install Docker w Ansible') { steps { sh ''' - ansible-playbook ansible/install_docker.yml \ - -i EC2_PUBLIC_IP, \ - --user ubuntu \ - --private-key ~/.ssh/DevOpsProj.pem \ - --check + export PATH=/opt/homebrew/bin:/usr/local/bin:$PATH + ansible-playbook --version + ansible-playbook ansible/install_docker.yml \ + -i 3.131.141.176, \ + --user ubuntu \ + --private-key ~/.ssh/DevOpsProj.pem ''' } } From 560e3f0ab7de15d7fcceee11b4cee0967ede175c Mon Sep 17 00:00:00 2001 From: Caleb Crawley Date: Wed, 17 Dec 2025 14:53:46 -0500 Subject: [PATCH 5/7] Add Docker Image CI workflow --- .github/workflows/docker-image.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/workflows/docker-image.yml diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 00000000..793d8e0e --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -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 Dockerfile --tag my-image-name:$(date +%s) From bd4fcf23305a3264e91141aaedc1c27b6da1717b Mon Sep 17 00:00:00 2001 From: Caleb Crawley Date: Wed, 17 Dec 2025 14:56:10 -0500 Subject: [PATCH 6/7] update route to dockerfile --- .github/workflows/docker-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 793d8e0e..f2611231 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -15,4 +15,4 @@ jobs: steps: - uses: actions/checkout@v4 - name: Build the Docker image - run: docker build . --file Dockerfile --tag my-image-name:$(date +%s) + run: docker build . --file ../../docker/Dockerfile --tag my-image-name:$(date +%s) From 69a5dfa0cafcdcf8cc05eb1ab818c5004b02394c Mon Sep 17 00:00:00 2001 From: Caleb Crawley Date: Wed, 17 Dec 2025 17:28:00 -0500 Subject: [PATCH 7/7] Adjust jenkinsfile for slave node... still needs adjustments --- .github/workflows/docker-image.yml | 2 +- Jenkinsfile | 111 +++++++++++++++++++++-------- 2 files changed, 83 insertions(+), 30 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index f2611231..4740105b 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -15,4 +15,4 @@ jobs: steps: - uses: actions/checkout@v4 - name: Build the Docker image - run: docker build . --file ../../docker/Dockerfile --tag my-image-name:$(date +%s) + run: docker build . --file ./docker/Dockerfile --tag my-image-name:$(date +%s) diff --git a/Jenkinsfile b/Jenkinsfile index 780d7217..66a143d2 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,52 +1,105 @@ pipeline { agent any - triggers { - pollSCM('H/2 * * * *') + 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('Checkout Code') { + stage('Prepare Slave Node') { steps { - git branch: 'master', url: 'https://github.com/designking06/DevOpsProjCert.git' + 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('Install Puppet Agent (Mocked)') { + stage('Clone Repository') { steps { - sh ''' - ssh -i ~/.ssh/DevOpsProj.pem ubuntu@3.131.141.176 \ - "sudo apt update && sudo apt install -y puppet-agent" - '''} + 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 w Ansible') { + stage('Install Docker via Ansible') { steps { - sh ''' - export PATH=/opt/homebrew/bin:/usr/local/bin:$PATH - ansible-playbook --version - ansible-playbook ansible/install_docker.yml \ - -i 3.131.141.176, \ - --user ubuntu \ - --private-key ~/.ssh/DevOpsProj.pem - ''' + 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('Docker Build (Mocked)') { + stage('Deploy PHP Docker Container') { steps { - sh ''' - echo "docker build -t php-webapp ." - echo "docker run -d php-webapp" - ''' + 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 { - failure { - sh 'echo "Rollback: removing container (mock)"' + success { + echo 'Pipeline completed successfully!' + } + failure { + echo 'Pipeline failed. Check logs.' + } } - } -} + } \ No newline at end of file