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