Skip to content

Commit

Permalink
Feat: staging, main 환경 별도로 분리, staging 환경 VPC 이동 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
BYEONGRYEOL committed Aug 6, 2024
1 parent dd817c9 commit f3da606
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions .github/workflows/cicd-ec2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: CI/CD using github actions & docker
# main이나 staging 브랜치에 push가 되었을 때 실행
on:
push:
branches: [ "main", "staging" ]
branches: [ "main", "staging", "main-test" ]

env:
AWS_REGION: ap-northeast-2
Expand Down Expand Up @@ -92,23 +92,37 @@ jobs:
aws-region: ap-northeast-2

- name: Add Github Actions IP to Security group
if: contains(github.ref, 'staging')
run: |
aws ec2 authorize-security-group-ingress --group-name ${{ env.AWS_SG_NAME }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ap-northeast-2

- name: Upload docker compose file to deploy server

- name: Login to aws ECR
if: contains(github.ref, 'main')
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST_DEPLOY }}
username: ubuntu
key: ${{ secrets.EC2_KEY }}
port: 22
source: "./docker/deploy/*"
target: "/home/ubuntu/workspace/"
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push image to aws ECR
if: contains(github.ref, 'main')
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: genti-deploy
IMAGE_TAG: latest
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
- name: Upload docker-compose, appspec, afterInstall file to S3
if: contains(github.ref, 'main')
run: |
zip -r ./$GITHUB_SHA.zip ./scripts appspec.yml docker-compose.yml .env
aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://$S3_BUCKET_NAME
- name: Upload docker compose file to staging server
if: contains(github.ref, 'staging')
Expand All @@ -122,12 +136,14 @@ jobs:
target: "/home/ubuntu/workspace/"

# docker build & push to deploy server
- name: Docker build & push to deploy
- name: Deploy to EC2 with CodeDeploy
if: contains(github.ref, 'main')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile_deploy -t ${{ secrets.DOCKER_USERNAME }}/genti-deploy .
docker push ${{ secrets.DOCKER_USERNAME }}/genti-deploy
aws deploy create-deployment \
--application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
--deployment-config-name CodeDeployDefault.AllAtOnce \
--deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \
--s3-location bucket=$S3_BUCKET_NAME,key=$GITHUB_SHA.zip,bundleType=zip
# docker build & push to staging
- name: Docker build & push to staging
Expand All @@ -137,24 +153,6 @@ jobs:
docker build -f Dockerfile_staging -t ${{ secrets.DOCKER_USERNAME }}/genti-staging .
docker push ${{ secrets.DOCKER_USERNAME }}/genti-staging
## deploy to deploy server
- name: Deploy to deploy server
uses: appleboy/ssh-action@master
id: deploy-deploy
if: contains(github.ref, 'main')
with:
host: ${{ secrets.HOST_DEPLOY }} # EC2 퍼블릭 IPv4 DNS
username: ubuntu
key: ${{ secrets.EC2_KEY }}
envs: GITHUB_SHA
script: |
sudo docker ps
cd /home/ubuntu/workspace/docker/deploy
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/genti-deploy
sudo docker compose up -d
sudo docker image prune -f
## deploy to staging server
- name: Deploy to staging server
uses: appleboy/ssh-action@master
Expand All @@ -175,6 +173,7 @@ jobs:
sudo docker image prune -f
- name: delete github actions ip from aws security group
if: contains(github.ref, 'staging')
run: |
aws ec2 revoke-security-group-ingress --group-name ${{ env.AWS_SG_NAME }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
env:
Expand Down

0 comments on commit f3da606

Please sign in to comment.