diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 00000000..4740105b --- /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 ./docker/Dockerfile --tag my-image-name:$(date +%s) diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 00000000..66a143d2 --- /dev/null +++ b/Jenkinsfile @@ -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.' + } + } + } \ No newline at end of file diff --git a/ansible/install_docker.yml b/ansible/install_docker.yml new file mode 100644 index 00000000..ac74937d --- /dev/null +++ b/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/app/config.php similarity index 100% rename from website/config.php rename to app/config.php diff --git a/website/content/404.php b/app/content/404.php similarity index 100% rename from website/content/404.php rename to app/content/404.php diff --git a/website/content/about-us.php b/app/content/about-us.php similarity index 100% rename from website/content/about-us.php rename to app/content/about-us.php diff --git a/website/content/contact.php b/app/content/contact.php similarity index 100% rename from website/content/contact.php rename to app/content/contact.php diff --git a/website/content/home.php b/app/content/home.php similarity index 100% rename from website/content/home.php rename to app/content/home.php diff --git a/website/content/products.php b/app/content/products.php similarity index 100% rename from website/content/products.php rename to app/content/products.php diff --git a/website/functions.php b/app/functions.php similarity index 100% rename from website/functions.php rename to app/functions.php diff --git a/website/index.php b/app/index.php similarity index 100% rename from website/index.php rename to app/index.php diff --git a/website/template/template.php b/app/template/template.php similarity index 100% rename from website/template/template.php rename to app/template/template.php diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 00000000..c853fa27 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,4 @@ +FROM devopsedu/webapp +COPY app/ /var/www/html/ +EXPOSE 80 +CMD ["apache2-foreground"] diff --git a/puppet/docker_agent.pp b/puppet/docker_agent.pp new file mode 100644 index 00000000..7412fe85 --- /dev/null +++ b/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/readme.md similarity index 85% rename from website/readme.md rename to readme.md index 9ac786cf..f67e94b9 100644 --- a/website/readme.md +++ b/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