diff --git a/.aws/task-definition.json b/.aws/task-definition.json new file mode 100644 index 0000000..42ec3de --- /dev/null +++ b/.aws/task-definition.json @@ -0,0 +1,90 @@ +{ + "taskDefinitionArn": "arn:aws:ecs:eu-north-1:345594590074:task-definition/ironoc:latest", + "containerDefinitions": [ + { + "name": "ironoc", + "image": "345594590074.dkr.ecr.eu-north-1.amazonaws.com/conorheffron/ironoc:latest", + "memory": "512", + "portMappings": [ + { + "name": "ironoc-80-tcp", + "containerPort": 8080, + "hostPort": 80, + "protocol": "tcp", + "appProtocol": "http" + } + ], + "essential": true, + "environment": [], + "mountPoints": [], + "volumesFrom": [], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/ironoc", + "mode": "non-blocking", + "awslogs-create-group": "true", + "max-buffer-size": "25m", + "awslogs-region": "eu-north-1", + "awslogs-stream-prefix": "ecs" + } + }, + "systemControls": [] + } + ], + "family": "ironoc", + "taskRoleArn": "arn:aws:iam::345594590074:role/ecsTaskExecutionRole", + "executionRoleArn": "arn:aws:iam::345594590074:role/ecsTaskExecutionRole", + "networkMode": "bridge", + "volumes": [], + "status": "ACTIVE", + "requiresAttributes": [ + { + "name": "com.amazonaws.ecs.capability.logging-driver.awslogs" + }, + { + "name": "ecs.capability.execution-role-awslogs" + }, + { + "name": "com.amazonaws.ecs.capability.ecr-auth" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.28" + }, + { + "name": "com.amazonaws.ecs.capability.task-iam-role" + }, + { + "name": "ecs.capability.execution-role-ecr-pull" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18" + }, + { + "name": "ecs.capability.task-eni" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.29" + } + ], + "placementConstraints": [], + "compatibilities": [ + "EC2", + "FARGATE" + ], + "requiresCompatibilities": [ + "EC2" + ], + "cpu": null, + "memory": null, + "runtimePlatform": { + "cpuArchitecture": "X86_64", + "operatingSystemFamily": "LINUX" + }, + "registeredAt": "2024-09-08T21:13:30.158Z", + "registeredBy": "arn:aws:iam::345594590074:root", + "tags": [] +} \ No newline at end of file diff --git a/.github/workflows/aws.yml b/.github/workflows/aws.yml new file mode 100644 index 0000000..1076541 --- /dev/null +++ b/.github/workflows/aws.yml @@ -0,0 +1,75 @@ +name: Deploy to Amazon ECS + +on: + push: + branches: [ "main" ] + +env: + AWS_REGION: eu-north-1 + ECR_REPOSITORY: conorheffron/ironoc + ECS_SERVICE: IronocService + ECS_CLUSTER: IronocPortfolioCluster + ECS_TASK_DEFINITION: .aws/task-definition.json + CONTAINER_NAME: ironoc + +permissions: + contents: read + +jobs: + deploy: + name: Deploy + runs-on: ubuntu-latest + environment: production + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up JDK 21 + uses: actions/setup-java@v3 + with: + java-version: '21' + distribution: 'temurin' + cache: maven + - name: Build with Maven + run: mvn -B package --file pom.xml + + - 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: ${{ env.AWS_REGION }} + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + + - name: Build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + IMAGE_TAG: ${{ github.sha }} + run: | + # Build a docker container and + # push it to ECR so that it can + # be deployed to ECS. + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT + + - name: Fill in the new image ID in the Amazon ECS task definition + id: task-def + uses: aws-actions/amazon-ecs-render-task-definition@v1 + with: + task-definition: ${{ env.ECS_TASK_DEFINITION }} + container-name: ${{ env.CONTAINER_NAME }} + image: ${{ steps.build-image.outputs.image }} + + - name: Deploy Amazon ECS task definition + uses: aws-actions/amazon-ecs-deploy-task-definition@v1 + with: + task-definition: ${{ steps.task-def.outputs.task-definition }} + service: ${{ env.ECS_SERVICE }} + cluster: ${{ env.ECS_CLUSTER }} + wait-for-service-stability: true diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml deleted file mode 100644 index fb61984..0000000 --- a/.github/workflows/docker-image.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: Docker Image CI - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - -jobs: - - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - name: Build the Docker image - run: docker build . --file Dockerfile --tag conorheffron/ironoc:$(date +%s) diff --git a/Dockerfile b/Dockerfile index 7edd527..fa5523d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,14 @@ FROM eclipse-temurin:21-jdk VOLUME /tmp -#for local -#ADD target/*.war app.war -#for CI/CD -ADD *.war app.war + +COPY target/*.war app.war RUN sh -c 'touch /app.war' ENV RUN_FILE /run.sh -ADD run.sh ${RUN_FILE} +COPY run.sh ${RUN_FILE} RUN chmod +x ${RUN_FILE} -ENTRYPOINT [ "sh", "-c", "${RUN_FILE}" ] \ No newline at end of file +EXPOSE 8080 + +ENTRYPOINT [ "sh", "-c", "${RUN_FILE}" ] diff --git a/README.md b/README.md index e5b007f..14bb279 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ ![Auto Assign](https://github.com/conorheffron/ironoc/actions/workflows/auto-assign.yml/badge.svg) +[![Deploy to Amazon ECS](https://github.com/conorheffron/ironoc/actions/workflows/aws.yml/badge.svg)](https://github.com/conorheffron/ironoc/actions/workflows/aws.yml) + # Docker Image [ironoc Docker Hub Link](https://hub.docker.com/repository/docker/conorheffron/ironoc/general) @@ -14,7 +16,7 @@ Personal website / portfolio http://www.ironoc.com/ (no longer hosted) # Tech Stack -Java 21 (LTS), Spring Boot 3.3, Maven 3.8, HTML5+CSS, JQuery, Docker / Bash +Java 21 (LTS), Spring Boot 3.3, Maven 3.8, HTML5+CSS, JQuery, Docker / Bash, AWS # Run without cloning project: ```