Skip to content

Commit

Permalink
feat: deploy yaml 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
miro-ring committed Jan 11, 2024
1 parent 186864a commit 13c7ab5
Showing 1 changed file with 42 additions and 32 deletions.
74 changes: 42 additions & 32 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ name: Create and publish a Docker image to AWS ECR ans deploy to EC2
on:
push:
branches:
- main
- feature/BAR-140

env:
REGISTRY: ${{ secrets.AWS_ECR_REGISTRY }}
AWS_REGION: ap-northeast-2
IMAGE_NAME: baro

jobs:
Expand All @@ -18,39 +19,48 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.AWS_ACCESS_KEY_ID }}
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=sha
- name: Build and push Docker image
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2

- name: Build, tag, and push docker image to Amazon ECR
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: ${{ env.IMAGE_NAME }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG .
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
- name: Delete old docker image in ECR
uses: LeiaInc/devops-delete-old-aws-ecr-images@v1
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
repo-name: ${{ env.IMAGE_NAME }}
qtd-images: 5

- name: Deploy to EC2

run: |
echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > private_key.pem
chmod 400 private_key.pem
ssh -i private_key.pem -o StrictHostKeyChecking=no ubuntu@${{ secrets.EC2_PUBLIC_DOMAIN }} "
aws ecr get-login-password --region ap-northeast-2 |
sudo docker login --username AWS --password-stdin ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} &&
if [ \$(sudo docker ps -q -f name=${{ env.IMAGE_NAME }}) ]; then
sudo docker stop ${{ env.IMAGE_NAME }} &&
sudo docker rm ${{ env.IMAGE_NAME }}
fi &&
sudo docker image pull ${{ steps.meta.outputs.tags }} &&
sudo docker container run --name ${{ env.IMAGE_NAME }} -d -p 3000:3000 ${{ steps.meta.outputs.tags }}"
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: ${{ env.IMAGE_NAME }}
IMAGE_TAG: ${{ github.sha }}
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_PUBLIC_DOMAIN }}
username: ubuntu
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
envs: REGISTRY,REPOSITORY,IMAGE_TAG,AWS_REGION,IMAGE_NAME
script: |-
aws ecr get-login-password --region ${{ env.AWS_REGION }} |
sudo docker login --username AWS --password-stdin ${{ env.REGISTRY }}/${{ env.REPOSITORY }}
sudo docker stop ${{ env.IMAGE_NAME }} || true
sudo docker rm ${{ env.IMAGE_NAME }} || true
sudo docker rmi $(sudo docker images -aq) || true
sudo docker pull $REGISTRY/$REPOSITORY:$IMAGE_TAG
sudo docker container run --name ${{ env.IMAGE_NAME }} -d -p 3000:3000 $REGISTRY/$REPOSITORY:$IMAGE_TAG

0 comments on commit 13c7ab5

Please sign in to comment.