From 4e3fa6cfbd28fa28df0b652e98ca070bfbd520df Mon Sep 17 00:00:00 2001 From: Nam Gi Beom Date: Thu, 8 Feb 2024 08:18:57 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20image=20push=20workflows=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/aws-config.yml | 24 -------------- .github/workflows/deploy-fluentbit.yml | 44 ++++++++++++-------------- .github/workflows/push-image.yml | 37 ++++++++++++++++++++++ 3 files changed, 57 insertions(+), 48 deletions(-) delete mode 100644 .github/workflows/aws-config.yml create mode 100644 .github/workflows/push-image.yml diff --git a/.github/workflows/aws-config.yml b/.github/workflows/aws-config.yml deleted file mode 100644 index 839d574..0000000 --- a/.github/workflows/aws-config.yml +++ /dev/null @@ -1,24 +0,0 @@ -name: AWS ECR Login - -on: - workflow_call: - -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: 'true' \ No newline at end of file diff --git a/.github/workflows/deploy-fluentbit.yml b/.github/workflows/deploy-fluentbit.yml index 65f7ccc..bd75d29 100644 --- a/.github/workflows/deploy-fluentbit.yml +++ b/.github/workflows/deploy-fluentbit.yml @@ -9,7 +9,7 @@ on: - '.github/workflows/**' env: - ECR_URI: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-2.amazonaws.com + ENDPOINT: ${{ 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 @@ -21,17 +21,15 @@ env: jobs: deploy-dev: runs-on: ubuntu-latest - env: - working-directory: ./fluentbit steps: - - name: Configure aws - uses: ./.github/workflows/aws-config.yml + - name: Check out code + uses: actions/checkout@v3 - - 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: Push image to Amazon ECR + uses: ./.github/workflows/push-image.yml + with: + uri: ${{ env.ENDPOINT }}/${{ env.DEV_REPO }}:${{ env.TAG }} + working-directory: ./fluentbit - name: Pull image from Amazon ECR and restart container in EC2 uses: appleboy/ssh-action@master @@ -40,24 +38,22 @@ jobs: 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 login -u AWS -p $(aws ecr get-login-password --region ap-northeast-2) ${{ env.ENDPOINT }} + docker pull ${{ env.ENDPOINT }}/${{ 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 }} - + docker run -d --name fluentbit-dev -p 8888:8888 --network dev-network -e env=dev ${{ env.ENDPOINT }}/${{ env.DEV_REPO }}:${{ env.TAG }} + deploy-prod: runs-on: ubuntu-latest - env: - working-directory: ./fluentbit steps: - - name: Configure aws - uses: ./.github/workflows/aws-config.yml + - name: Check out code + uses: actions/checkout@v3 - - 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: Push image to Amazon ECR + uses: ./.github/workflows/push-image.yml + with: + uri: ${{ env.ENDPOINT }}/${{ env.PROD_REPO }}:${{ env.TAG }} + working-directory: ./fluentbit - name: Download task definition run: | @@ -69,7 +65,7 @@ jobs: with: task-definition: task-definition.json container-name: ${{ env.ECS_CONTAINER }} - image: ${{ env.ECR_URI }}/${{ env.PROD_REPO }}:${{ env.TAG }} + image: ${{ env.ENDPOINT }}/${{ env.PROD_REPO }}:${{ env.TAG }} - name: Deploy Amazon ECS task definition id: deploy diff --git a/.github/workflows/push-image.yml b/.github/workflows/push-image.yml new file mode 100644 index 0000000..d650854 --- /dev/null +++ b/.github/workflows/push-image.yml @@ -0,0 +1,37 @@ +name: AWS ECR Login and Push Image + +on: + workflow_call: + inputs: + uri: + description: 'URI of the repository' + required: true + type: string + working-directory: + description: 'Working directory' + required: true + type: string + + +jobs: + push-image: + runs-on: ubuntu-latest + steps: + - 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: 'true' + + - name: Build and push image to Amazon ECR + run: | + docker build -t ${{ inputs.uri}} . + docker push ${{ inputs.uri}} + working-directory: ${{ inputs.working-directory }} \ No newline at end of file