add cloudwatch #13
This file contains 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
# This workflow builds the Docker image for the Todo-App and then deploys the application. | |
name: 02 - Deploy Service | |
env: | |
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }} | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
AWS_REGION: ${{ secrets.AWS_REGION }} | |
on: | |
push: | |
paths: | |
- 'app/src/main/**' | |
- 'app/Dockerfile' | |
- 'app/build.gradle.kts' | |
- 'cdk/src/main/**' | |
- 'cdk/cdk.json' | |
- '.github/workflows/02-service.yml' | |
workflow_dispatch: | |
jobs: | |
build-and-publish: | |
runs-on: ubuntu-20.04 | |
name: Build App to ECR | |
outputs: | |
dockerImageTag: ${{ steps.dockerImageTag.outputs.tag }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
distribution: 'temurin' | |
java-version: 17 | |
cache: 'gradle' | |
- name: Setup Gradle | |
uses: gradle/actions/setup-gradle@v4 | |
with: | |
cache-overwrite-existing: true | |
- name: Build application | |
working-directory: app | |
run: ../gradlew build --stacktrace | |
- name: Zip build reports | |
if: always() | |
run: zip -r reports.zip . -i **/build/reports | |
- name: Upload build reports | |
uses: actions/upload-artifact@v4 | |
if: failure() | |
with: | |
name: reports | |
path: reports.zip | |
- name: Create Docker image tag | |
id: dockerImageTag | |
run: echo "tag=$(date +'%Y%m%d%H%M%S')-${GITHUB_SHA}" >> $GITHUB_OUTPUT | |
- name: Publish Docker image to ECR | |
if: github.ref == 'refs/heads/main' || contains(github.event.head_commit.message, 'DEPLOY NOW') | |
env: | |
DOCKER_IMAGE_TAG: ${{ steps.dockerImageTag.outputs.tag }} | |
working-directory: app | |
run: | | |
docker build -t todo-app . | |
docker tag todo-app ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/todo-app:${DOCKER_IMAGE_TAG} | |
docker tag todo-app ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/todo-app:latest | |
aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com | |
docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/todo-app:${DOCKER_IMAGE_TAG} | |
docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/todo-app:latest | |
deploy: | |
runs-on: ubuntu-20.04 | |
name: Deploy App on ECS | |
needs: build-and-publish | |
timeout-minutes: 15 | |
if: github.ref == 'refs/heads/main' || contains(github.event.head_commit.message, 'DEPLOY NOW') | |
concurrency: todo-app-deploy | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
distribution: 'temurin' | |
java-version: 17 | |
cache: 'gradle' | |
- name: Setup Gradle | |
uses: gradle/actions/setup-gradle@v4 | |
with: | |
cache-overwrite-existing: true | |
- name: NPM install | |
working-directory: cdk | |
run: npm install | |
- name: Deploy Service | |
working-directory: cdk | |
run: npm run service:deploy -- -c dockerImageTag=${{ needs.build-and-publish.outputs.dockerImageTag }} | |
# Each Service stack updates creates a new parameter stack that CloudFormation does not clean up for us. | |
# This step deletes all "*Service-Parameters*" stacks except the latest one to keep the house clean. | |
- name: Clean up old parameter stacks | |
working-directory: cdk | |
run: | | |
aws cloudformation describe-stacks --region ${AWS_REGION} --query "Stacks[].StackName" --output text | sed -e "s/\s\+/\n/g" | grep -e "staging-todo-app-Service-Parameters" | sort -r | tail -n+2 > stacks_to_be_deleted.txt | |
for stack in $(cat stacks_to_be_deleted.txt); do aws cloudformation delete-stack --stack-name $stack --region ${AWS_REGION}; done |