filters and matrix in workflow #49
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build & Deploy | ||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| - filters/matrix | ||
| workflow_dispatch: | ||
| jobs: | ||
| detect-changes: | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| backend: ${{ steps.changes.outputs.backend }} | ||
| frontend: ${{ steps.changes.outputs.frontend }} | ||
| gateway: ${{ steps.changes.outputs.gateway }} | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - id: changes | ||
| uses: dorny/paths-filter@v3 | ||
| with: | ||
| filters: | | ||
| backend: | ||
| - 'backend/**' | ||
| frontend: | ||
| - 'frontend/**' | ||
| gateway: | ||
| - 'gateway/**' | ||
| build-and-push: | ||
| needs: detect-changes | ||
| name: Build and Push ${{ matrix.service }} to ECR | ||
| runs-on: ubuntu-latest | ||
| if: | | ||
| (${{ matrix.service }} == 'backend' && needs.detect-changes.outputs.backend == 'true') || | ||
| (${{ matrix.service }} == 'frontend' && needs.detect-changes.outputs.frontend == 'true') || | ||
| (${{ matrix.service }} == 'gateway' && needs.detect-changes.outputs.gateway == 'true') | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| service: [backend, frontend, gateway] | ||
| include: | ||
| - service: backend | ||
| context: ./backend | ||
| dockerfile: ./backend/Dockerfile | ||
| image: movie-api | ||
| - service: frontend | ||
| context: ./frontend | ||
| dockerfile: ./frontend/Dockerfile | ||
| image: frontend | ||
| - service: gateway | ||
| context: ./gateway | ||
| dockerfile: ./gateway/Dockerfile | ||
| image: gateway | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| - name: Configure AWS credentials | ||
| uses: aws-actions/configure-aws-credentials@v4 | ||
| with: | ||
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
| aws-region: eu-west-1 # change to your AWS region | ||
| - name: Log in to Amazon ECR | ||
| id: login-ecr | ||
| uses: aws-actions/amazon-ecr-login@v2 | ||
| - name: Build, Tag, and Push Image to ECR | ||
| env: | ||
| ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | ||
| REPO_NAME: movie_api | ||
| run: | | ||
| docker build -t $ECR_REGISTRY/$REPO_NAME:${{matrix.image}} -f ${{ matrix.dockerfile }} ${{ matrix.context }} | ||
| docker push $ECR_REGISTRY/$REPO_NAME:${{matrix.image}} | ||
| deploy: | ||
| needs: build-and-push | ||
| runs-on: ubuntu-latest | ||
| if: always() # Ensures deploy runs even if no images were built | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| - name: Set up SSH key | ||
| uses: kielabokkie/ssh-key-and-known-hosts-action@v1 | ||
| with: | ||
| ssh-private-key: ${{ secrets.EC2_SSH_KEY }} | ||
| ssh-host: ${{ vars.SSH_PROXY_HOST }} | ||
| - name: Add SSH config | ||
| run: | | ||
| printf "Host ec2-docker\nHostName %s\nUser ubuntu\nIdentityFile ~/.ssh/id_rsa\nStrictHostKeyChecking no\nProxyCommand ssh -W %%h:%%p -q pure@xanderbit.cgitverse.com\n" "${{ secrets.EC2_HOST }}" > ~/.ssh/config | ||
| chmod 600 ~/.ssh/config | ||
| - name: Create .env file | ||
| run: | | ||
| echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env | ||
| echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env | ||
| echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env | ||
| echo "DB_USER=${{ secrets.DB_USER }}" >> .env | ||
| echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env | ||
| echo "REDIS_HOST=${{ secrets.REDIS_HOST }}" >> .env | ||
| echo "REDIS_PORT=${{ secrets.REDIS_PORT }}" >> .env | ||
| echo "DATA_SOURCE_NAME=${{ secrets.DATA_SOURCE_NAME }}" >> .env | ||
| - name: Configure AWS credentials | ||
| uses: aws-actions/configure-aws-credentials@v4 | ||
| with: | ||
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
| aws-region: eu-west-1 | ||
| - name: Log in to Amazon ECR | ||
| id: login-ecr | ||
| uses: aws-actions/amazon-ecr-login@v2 | ||
| - name: Install Ansible | ||
| run: | | ||
| sudo apt-get update | ||
| sudo apt-get install -y ansible | ||
| - name: Upgrade Ansible | ||
| run: | | ||
| pip install --upgrade ansible | ||
| - name: Run Ansible Playbook | ||
| run: | | ||
| ansible-playbook ansible/deploy.yml \ | ||
| -i ansible/inventory.ini | ||