Skip to content

Merge remote-tracking branch 'origin/main' #605

Merge remote-tracking branch 'origin/main'

Merge remote-tracking branch 'origin/main' #605

# This workflow builds the Docker image for the Todo-App and then deploys the application.
name: 04 - Publish Todo-App
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:
- 'application/**'
- 'cdk/**/*Service*'
- 'cdk/pom.xml'
- '.github/workflows/04-publish-todo-app.yml'
workflow_dispatch:
jobs:
build-and-publish:
runs-on: ubuntu-20.04
name: Build and publish Todo App
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: Build application
working-directory: application
run: ./gradlew build --stacktrace
- name: Zip build reports
if: always()
run: zip -r reports.zip **/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 registry
if: ${{ false }} # disabled for now as we ran out of AWS credits
# if: github.ref == 'refs/heads/main' || contains(github.event.head_commit.message, 'DEPLOY NOW')
env:
DOCKER_IMAGE_TAG: ${{ steps.dockerImageTag.outputs.tag }}
working-directory: application
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
# Superseded by GitHub Action's concurrency support, see below
# - name: Sending deployment event to queue
# if: github.ref == 'refs/heads/main'
# env:
# DOCKER_IMAGE_TAG: ${{ steps.dockerImageTag.outputs.tag }}
# run: |
# export EVENT_PAYLOAD="{\"commitSha\": \"$GITHUB_SHA\", \"ref\": \"main\", \"owner\": \"stratospheric-dev\", \"repo\": \"stratospheric\", \"workflowId\": \"05-update-todo-app-in-staging.yml\", \"dockerImageTag\": \"$DOCKER_IMAGE_TAG\"}"
# aws sqs send-message \
# --queue-url=https://sqs.eu-central-1.amazonaws.com/221875718260/todo-app-deploymentsQueue.fifo \
# --message-group-id default \
# --message-deduplication-id $GITHUB_SHA \
# --message-body "$EVENT_PAYLOAD"
deploy:
runs-on: ubuntu-20.04
name: Deploy Todo App
needs: build-and-publish
timeout-minutes: 15
if: ${{ false }} # disabled for now as we ran out of AWS credits
# if: github.ref == 'refs/heads/main' || contains(github.event.head_commit.message, 'DEPLOY NOW')
concurrency: todo-application-deployment
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: 'maven'
- name: NPM install
working-directory: cdk
run: npm install
- name: Deploy service stack
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 eu-central-1 --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 eu-central-1; done