Skip to content

Run ECS Batch Task

Run ECS Batch Task #4

Workflow file for this run

name: Run ECS Batch Task
on:
workflow_dispatch:
inputs:
command:
description: 'Select Artisan command to run'
required: true
type: choice
options:
- 'about'
- '--version'
env:
AWS_REGION: ap-northeast-1
jobs:
run-batch:
runs-on: ubuntu-latest
environment:
name: production # 環境名を指定(記載しないとSecretを取得不可)
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- uses: actions/checkout@v3
- name: Run ECS Task
run: |
TASK_DEF=$(aws ecs describe-task-definition --task-definition laravel-task --query 'taskDefinition.taskDefinitionArn' --output text)
TASK_INFO=$(aws ecs run-task \
--cluster laravel-ecs-cluster \
--task-definition $TASK_DEF \
--launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[$(aws cloudformation list-exports --query 'Exports[?Name==`laravel-network-stack-SubnetIds`].Value' --output text)],securityGroups=[$(aws ec2 describe-security-groups --filters Name=group-name,Values=*ECSSecurityGroup* --query 'SecurityGroups[0].GroupId' --output text)],assignPublicIp=ENABLED}" \
--overrides "{
\"containerOverrides\": [{
\"name\": \"laravel-app\",
\"command\": [\"artisan\", \"${{ github.event.inputs.command }}\"]
}]
}")
TASK_ARN=$(echo $TASK_INFO | jq -r '.tasks[0].taskArn')
echo "TASK_ARN=$TASK_ARN" >> $GITHUB_ENV
TASK_ID=$(echo $TASK_ARN | cut -d'/' -f3)
echo "TASK_ID=$TASK_ID" >> $GITHUB_ENV
- name: Wait and get logs
run: |
# タスクARNが設定されていることを確認
echo "Waiting for task: $TASK_ARN"
aws ecs wait tasks-stopped --cluster laravel-ecs-cluster --tasks $TASK_ARN
# タスクの最終ステータスを確認
TASK_STATUS=$(aws ecs describe-tasks --cluster laravel-ecs-cluster --tasks $TASK_ARN --query 'tasks[0].lastStatus' --output text)
echo "Task final status: $TASK_STATUS"
# ログを取得
./run.sh logs $TASK_ID