Skip to content

Instantly share code, notes, and snippets.

@tsoe77
Created December 11, 2024 21:56
Show Gist options
  • Select an option

  • Save tsoe77/1e06346325926c1b9d84ccb087d9ea3c to your computer and use it in GitHub Desktop.

Select an option

Save tsoe77/1e06346325926c1b9d84ccb087d9ea3c to your computer and use it in GitHub Desktop.
ECS Github Action Workflow
name: ECS Deploy Workflow
on:
push:
branches:
- 'main'
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Configure AWS credentials
id: aws-credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Login to Amazon ECR
id: aws-ecr-login
uses: aws-actions/amazon-ecr-login@v2
with:
registries: ${{ secrets.REGISTRIES }}
mask-password: 'true'
- name: Build, tag, and push docker image to Amazon ECR
id: build
run: |
export IMAGE_TAG=${GITHUB_SHA::7}
docker build -t ${IMAGE_REPO}/${IMAGE_NAME}:${IMAGE_TAG} .
docker push ${IMAGE_REPO}/${IMAGE_NAME}:${IMAGE_TAG}
echo "Built image - ${IMAGE_TAG}" >> $GITHUB_STEP_SUMMARY
echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_OUTPUT
echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_OUTPUT
env:
GITHUB_SHA: ${{ github.sha }}
IMAGE_REPO: ${{ steps.aws-ecr-login.outputs.registry }}
IMAGE_NAME: example
outputs:
IMAGE_TAG: ${{ steps.build.outputs.IMAGE_TAG }}
IMAGE_REPO: ${{ steps.aws-ecr-login.outputs.registry }}
IMAGE_NAME: ${{ steps.build.outputs.IMAGE_NAME }}
test:
name: Test
runs-on: ubuntu-latest
needs: build
steps:
- name: Run tests
run: |
echo "Running tests..."
echo "Tests passed!"
deploy:
name: Deploy
runs-on: ubuntu-latest
needs:
- test
- build
steps:
- name: Configure AWS credentials
id: aws-credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Retrieve current task definition
id: get-task-def
run: |
aws ecs describe-task-definition --task-definition example --query 'taskDefinition' > task-definition.json
- name: Fill in the new image ID in the Amazon ECS task definition
id: update-task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-definition.json
container-name: example
image: "${{ needs.build.outputs.IMAGE_REPO }}/${{ needs.build.outputs.IMAGE_NAME }}:${{ needs.build.outputs.IMAGE_TAG }}"
- name: Deploy to Dev ECS
id: deploy
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
with:
task-definition: ${{ steps.update-task-def.outputs.task-definition }}
service: example
cluster: example
wait-for-service-stability: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment