diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f1065f909..def41f11a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -31,14 +31,14 @@ jobs: - name: Setup cache environment id: cache-env - uses: shivammathur/cache-extensions@v1 + uses: shivammathur/cache-extensions@v2.323.0 with: php-version: ${{ matrix.php }} extensions: ${{ env.extensions }} key: ${{ env.key }} - name: Cache extensions - uses: actions/cache@v1 + uses: actions/cache@v2.1.8 # Updated to v2 with: path: ${{ steps.cache-env.outputs.dir }} key: ${{ steps.cache-env.outputs.key }} @@ -62,7 +62,7 @@ jobs: echo "COMPOSER_CACHE_DIR=~\AppData\Local\Composer" >> $GITHUB_ENV - name: Cache dependencies installed with composer - uses: actions/cache@v1 + uses: actions/cache@v2 # Updated to v2 with: path: ${{ steps.cache-env.outputs.dir }} key: php${{ matrix.php }}-composer-${{ matrix.dependencies }}-${{ hashFiles('**/composer.json') }} diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 000000000..fe2d05341 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,32 @@ +name: Deploy to EC2 + +on: + push: + branches: + - master + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Log in to Docker Hub + run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin + + - name: Set up SSH + run: | + mkdir -p ~/.ssh + echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts + + - name: Deploy to EC2 via SSH + run: | + ssh -o StrictHostKeyChecking=no ${{ secrets.EC2_USERNAME }}@${{ secrets.EC2_HOST }} << 'EOF' + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + docker pull kubemanan/yii2app-custom:latest + docker service update --image kubemanan/yii2app-custom:latest yii2app_php + EOF diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..09dd5219a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM php:7.4-apache + +# Install PHP extensions for Yii2 +RUN docker-php-ext-install pdo pdo_mysql + +# Enable mod_rewrite for Yii2 +RUN a2enmod rewrite + +# Set DocumentRoot to Yii2's web directory +ENV APACHE_DOCUMENT_ROOT /var/www/html/web + +# Update Apache configuration +RUN sed -ri 's|/var/www/html|${APACHE_DOCUMENT_ROOT}|g' /etc/apache2/sites-available/000-default.conf \ + && sed -ri 's|/var/www/|${APACHE_DOCUMENT_ROOT}|g' /etc/apache2/apache2.conf + +# Set permissions for the Yii2 app +WORKDIR /var/www/html +COPY . /var/www/html +RUN chown -R www-data:www-data /var/www/html && chmod -R 755 /var/www/html + diff --git a/ansible/clone_repo.yml b/ansible/clone_repo.yml new file mode 100644 index 000000000..819fe52e1 --- /dev/null +++ b/ansible/clone_repo.yml @@ -0,0 +1,11 @@ +--- +- name: Clone the Yii2 repo + hosts: all + become: true + + tasks: + - name: Clone repository + git: + repo: 'https://github.com/croodycoder/yii2-app-basic.git' + dest: /app + version: master diff --git a/ansible/deploy_service.yml b/ansible/deploy_service.yml new file mode 100644 index 000000000..dc28d09ec --- /dev/null +++ b/ansible/deploy_service.yml @@ -0,0 +1,8 @@ +--- +- name: Deploy Yii2 app as Swarm service + hosts: all + become: true + + tasks: + - name: Deploy PHP service + shell: docker stack deploy -c /app/docker-compose.yml yii2app diff --git a/ansible/docker.yml b/ansible/docker.yml new file mode 100644 index 000000000..005ebc9e6 --- /dev/null +++ b/ansible/docker.yml @@ -0,0 +1,44 @@ +--- +- name: Install Docker and Docker Compose + hosts: all + become: true + + tasks: + - name: Install required packages for Docker + apt: + name: + - apt-transport-https + - ca-certificates + - curl + - gnupg-agent + - lsb-release + - software-properties-common + state: present + update_cache: yes + + - name: Add Docker GPG key + apt_key: + url: https://download.docker.com/linux/ubuntu/gpg + state: present + + - name: Add Docker repository + apt_repository: + repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" + state: present + + - name: Install Docker CE + apt: + name: docker-ce + state: latest + + - name: Install Docker Compose + curl: + url: https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m) + dest: /usr/local/bin/docker-compose + mode: 'u+x' + + - name: Start and enable Docker service + systemd: + name: docker + enabled: yes + state: started diff --git a/ansible/nginx_config.yml b/ansible/nginx_config.yml new file mode 100644 index 000000000..3214f670e --- /dev/null +++ b/ansible/nginx_config.yml @@ -0,0 +1,27 @@ +--- +- name: Configure NGINX Reverse Proxy for Yii2 App + hosts: all + become: true + + tasks: + - name: Create reverse proxy NGINX config + copy: + dest: /etc/nginx/sites-available/default + content: | + server { + listen 80; + server_name localhost; + + location / { + proxy_pass http://localhost:8000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + } + + - name: Reload NGINX + systemd: + name: nginx + state: reloaded diff --git a/ansible/nginx_php.yml b/ansible/nginx_php.yml new file mode 100644 index 000000000..b890e1b24 --- /dev/null +++ b/ansible/nginx_php.yml @@ -0,0 +1,32 @@ +--- +- name: Install NGINX, Git, PHP dependencies + hosts: all + become: true + + tasks: + - name: Install NGINX + apt: + name: nginx + state: latest + + - name: Install Git + apt: + name: git + state: latest + + - name: Install PHP dependencies + apt: + name: + - php + - php-fpm + - php-mysql + - php-cli + - php-xml + - php-mbstring + state: latest + + - name: Start and enable NGINX service + systemd: + name: nginx + enabled: yes + state: started diff --git a/ansible/swarm.yml b/ansible/swarm.yml new file mode 100644 index 000000000..4bd2c08ab --- /dev/null +++ b/ansible/swarm.yml @@ -0,0 +1,20 @@ +--- +- name: Initialize Docker Swarm + hosts: all + become: true + + tasks: + - name: Initialize Docker Swarm + shell: docker swarm init + when: ansible_facts['distribution'] == 'Ubuntu' + ignore_errors: yes + + - name: Get join token for workers + shell: docker swarm join-token worker -q + register: worker_token + when: ansible_facts['distribution'] == 'Ubuntu' + ignore_errors: yes + + - name: Display join token for workers + debug: + var: worker_token.stdout diff --git a/docker-compose.yml b/docker-compose.yml index 86be3bd0d..f4dcd17a6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,9 +1,25 @@ -version: '2' +version: '3.8' + services: php: - image: yiisoftware/yii2-php:7.4-apache - volumes: - - ~/.composer-docker/cache:/root/.composer/cache:delegated - - ./:/app:delegated + build: + context: . + image: kubemanan/yii2app-custom:latest ports: - '8000:80' + networks: + - app-net + deploy: + replicas: 1 + restart_policy: + condition: on-failure + volumes: + - yii2app-data:/var/www/html # Named volume, safe in Swarm + +volumes: + yii2app-data: + +networks: + app-net: + driver: overlay +