refactor: parallel jobs #17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: | |
push: | |
branches: | |
- main | |
paths: | |
- 'fluentbit/**' | |
env: | |
ECR_URI: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-2.amazonaws.com | |
DEV_REPO: ${{ secrets.DEV_FLUENTBIT_ECR_REPOSITORY }} | |
PROD_REPO: ${{ secrets.PROD_FLUENTBIT_ECR_REPOSITORY }} | |
TASK_DEFINITION: ${{ secrets.TASK_DEFINITION_NAME }}-prod | |
ECS_CONTAINER: ${{ secrets.ECS_FLUENTBIT_CONTAINER_NAME }} | |
ECS_SERVICE: ${{ secrets.ECS_SERVICE_NAME }}-prod | |
ECS_CLUSTER: ${{ secrets.ECS_CLUSTER_NAME }}-prod | |
TAG: latest | |
WORKING_DIRECTORY: ./fluentbit | |
jobs: | |
aws-config: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out code | |
uses: actions/checkout@v3 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v2 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ap-northeast-2 | |
- name: login to ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
with: | |
mask-password: 'false' | |
deploy-dev: | |
runs-on: ubuntu-latest | |
needs: aws-config | |
steps: | |
- name: Build and push image to Amazon ECR | |
run: | | |
docker build -t ${{ env.ECR_URI }}/${{ env.DEV_REPO }}:${{ env.TAG }} . | |
docker push ${{ env.ECR_URI }}/${{ env.DEV_REPO }}:${{ env.TAG }} | |
working-directory: ${{ env.WORKING_DIRECTORY }} | |
- name: Pull image from Amazon ECR and restart container in EC2 | |
uses: appleboy/ssh-action@master | |
with: | |
key: ${{ secrets.DEV_SERVER_SSH_KEY }} | |
host: ${{ secrets.DEV_SERVER_HOST }} | |
username: ${{ secrets.DEV_SERVER_USER }} | |
script: | | |
docker login -u AWS -p $(aws ecr get-login-password --region ap-northeast-2) ${{ env.ECR_URI }} | |
docker pull ${{ env.ECR_URI }}/${{ env.DEV_REPO }}:${{ env.TAG }} | |
docker stop fluentbit-dev && docker rm fluentbit-dev | |
docker run -d --name fluentbit-dev -p 8888:8888 --network dev-network -e env=dev ${{ env.ECR_URI }}/${{ env.DEV_REPO }}:${{ env.TAG }} | |
deploy-prod: | |
runs-on: ubuntu-latest | |
needs: aws-config | |
steps: | |
- name: Build and push image to Amazon ECR | |
run: | | |
docker build -t ${{ env.ECR_URI }}/${{ env.PROD_REPO }}:${{ env.TAG }} . | |
docker push ${{ env.ECR_URI }}/${{ env.PROD_REPO }}:${{ env.TAG }} | |
working-directory: ${{ env.WORKING_DIRECTORY }} | |
- name: Download task definition | |
run: | | |
aws ecs describe-task-definition --task-definition ${{ env.TASK_DEFINITION }} --query taskDefinition > task-definition.json | |
- 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: task-definition.json | |
container-name: ${{ env.ECS_CONTAINER }} | |
image: ${{ env.ECR_URI }}/${{ env.PROD_REPO }}:${{ env.TAG }} | |
- name: Deploy Amazon ECS task definition | |
id: deploy | |
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 |