请帮助我解决使用 GitHub Action 将我的 Spring Java 应用程序部署到 AWS(EC2、ECS)的问题。
因此,我遵循 GitHub Actions 上“部署到 Amazon ECS”的默认工作流程文件中提供的建议。
首先,我在AWS上创建了一个私有存储库,并使用AWS中的“查看推送命令”构建了一个映像。 在此输入图片描述 在此输入图片描述
此外,我创建了任务定义和集群。你可以看到下一个。
我的泊坞窗文件: 在此输入图片描述
我的工作流程生活:
name: Deploy to Amazon ECS
on:
push:
branches: \[ "master" \]
pull_request:
branches: \[ "master" \]
env:
AWS_REGION: eu-north-1 # set this to your preferred AWS region, e.g. us-west-1
ECR_REPOSITORY: techtask # set this to your Amazon ECR repository name
ECS_SERVICE: techtaskservice # set this to your Amazon ECS service name
ECS_CLUSTER: techtask-claster # set this to your Amazon ECS cluster name
ECS_TASK_DEFINITION: ./td-test.json # set this to the path to your Amazon ECS task definition
\# file, e.g. .aws/task-definition.json
CONTAINER_NAME: container # set this to the name of the container in the
\# containerDefinitions section of your task definition
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: run compile
run: mvn clean install -DskipTests
- 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
但是,当我尝试创建服务时,遇到了问题。任务和服务未创建,并且始终具有“Provisioning”(对于任务)和“CREATE_IN_PROGRESS”(对于服务)状态。
我的用户和角色具有以下权限:
AdministratorAccess
AmazonEC2ContainerRegistryFullAccess
AmazonEC2ContainerServiceRole
AmazonEC2FullAccess
AmazonEC2RoleforAWSCodeDeploy
AmazonECS_FullAccess
AWSCodeDeployRoleForECS
EC2InstanceConnect
并且,当我在 Hithub Action 中运行工作流文件(将我的应用程序部署到 AWS)时,进程在“部署 Amazon ECS 任务定义”步骤处停止并且未完成。
我也无法连接到实例:
Failed to connect to your instance
EC2 Instance Connect is unable to connect to your instance. Ensure your instance network settings are configured correctly for EC2 Instance Connect. For more information, see EC2 Instance Connect Prerequisites at https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-connect-prerequisites.html.
I tried setting a HEALTHCHECK in the Dockerfile with:
HEALTHCHECK --interval=5s --timeout=10s --retries=3
CMD curl --silent --fail http://localhost:8080 || exit 1
我在创建任务定义文件时也尝试创建 HEALTHCHECK,但它不起作用。 我尝试在不使用“查看推送命令”(手动构建和推送图像)的情况下创建存储库,但它也不起作用。
请帮我找出问题所在。
问题是没有足够的内存来运行任务。我使用了容量更高的实例类型 (t3.small),并增加了任务定义中的限制。之后,一切都很顺利! 感谢 Mark B 的回答。