Ready for deployment - infrastructure setup

This commit is contained in:
Favee 2025-04-13 08:26:11 +01:00
parent cc3568567a
commit f389f18101

View file

@ -1,98 +1,98 @@
name: Deploy PetClinic ---
name: Deploy PetClinic
on: on:
workflow_dispatch: workflow_dispatch:
inputs: inputs:
environment: environment:
description: 'Environment to deploy to' description: 'Environment to deploy to'
required: true required: true
default: 'dev' default: 'dev'
type: choice type: choice
options: options:
- dev - dev
- staging - staging
- prod - prod
jobs: jobs:
deploy-infrastructure: deploy-infrastructure:
runs-on: ubuntu-latest runs-on: ubuntu-latest
environment: ${{ github.event.inputs.environment }} environment: ${{ github.event.inputs.environment }}
steps:
- uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-west-2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
- name: Terraform Init
working-directory: ./terraform
run: terraform init
- name: Terraform Plan
working-directory: ./terraform
run: |
terraform plan -var="environment=${{ github.event.inputs.environment }}" \
-var="db_username=${{ secrets.DB_USERNAME }}" \
-var="db_password=${{ secrets.DB_PASSWORD }}" \
-var="vpc_id=${{ secrets.VPC_ID }}"
- name: Terraform Apply
if: github.event.inputs.environment != 'prod'
working-directory: ./terraform
run: |
terraform apply -auto-approve -var="environment=${{ github.event.inputs.environment }}" \
-var="db_username=${{ secrets.DB_USERNAME }}" \
-var="db_password=${{ secrets.DB_PASSWORD }}" \
-var="vpc_id=${{ secrets.VPC_ID }}"
- name: Terraform Apply (Production)
if: github.event.inputs.environment == 'prod'
working-directory: ./terraform
run: |
terraform apply -var="environment=${{ github.event.inputs.environment }}" \
-var="db_username=${{ secrets.DB_USERNAME }}" \
-var="db_password=${{ secrets.DB_PASSWORD }}" \
-var="vpc_id=${{ secrets.VPC_ID }}"
steps: build-and-deploy:
- uses: actions/checkout@v3 needs: deploy-infrastructure
runs-on: ubuntu-latest
- name: Configure AWS credentials environment: ${{ github.event.inputs.environment }}
uses: aws-actions/configure-aws-credentials@v1 steps:
with: - uses: actions/checkout@v3
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} - name: Set up JDK 17
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} uses: actions/setup-java@v3
aws-region: us-west-2 with:
java-version: '17'
- name: Setup Terraform distribution: 'temurin'
uses: hashicorp/setup-terraform@v2 cache: maven
- name: Configure AWS credentials
- name: Terraform Init uses: aws-actions/configure-aws-credentials@v1
working-directory: ./terraform with:
run: terraform init aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Terraform Plan aws-region: us-west-2
working-directory: ./terraform - name: Login to Amazon ECR
run: terraform plan -var="environment=${{ github.event.inputs.environment }}" -var="db_username=${{ secrets.DB_USERNAME }}" -var="db_password=${{ secrets.DB_PASSWORD }}" -var="vpc_id=${{ secrets.VPC_ID }}" id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Terraform Apply - name: Build, tag, and push image to Amazon ECR
if: github.event.inputs.environment != 'prod' env:
working-directory: ./terraform ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: terraform apply -auto-approve -var="environment=${{ github.event.inputs.environment }}" -var="db_username=${{ secrets.DB_USERNAME }}" -var="db_password=${{ secrets.DB_PASSWORD }}" -var="vpc_id=${{ secrets.VPC_ID }}" ECR_REPOSITORY: petclinic-${{ github.event.inputs.environment }}
IMAGE_TAG: ${{ github.sha }}
- name: Terraform Apply (Production - with approval) run: |
if: github.event.inputs.environment == 'prod' docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
working-directory: ./terraform docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:latest
run: terraform apply -var="environment=${{ github.event.inputs.environment }}" -var="db_username=${{ secrets.DB_USERNAME }}" -var="db_password=${{ secrets.DB_PASSWORD }}" -var="vpc_id=${{ secrets.VPC_ID }}" docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
build-and-deploy: - name: Deploy to ECS
needs: deploy-infrastructure run: |
runs-on: ubuntu-latest aws ecs update-service --cluster petclinic-${{ github.event.inputs.environment }} \
environment: ${{ github.event.inputs.environment }} --service petclinic-service \
--force-new-deployment
steps: - name: Verify Deployment
- uses: actions/checkout@v3 run: |
echo "Waiting for deployment to complete..."
- name: Set up JDK 17 aws ecs wait services-stable --cluster petclinic-${{ github.event.inputs.environment }} \
uses: actions/setup-java@v3 --services petclinic-service
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-west-2
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: petclinic-${{ github.event.inputs.environment }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:latest
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
- name: Deploy to ECS
run: |
aws ecs update-service --cluster petclinic-${{ github.event.inputs.environment }} \
--service petclinic-service \
--force-new-deployment
- name: Verify Deployment
run: |
echo "Waiting for deployment to complete..."
aws ecs wait services-stable --cluster petclinic-${{ github.event.inputs.environment }} --services petclinic-service