Skip to content

Commit

Permalink
chore: ci-cd 문법 재배치
Browse files Browse the repository at this point in the history
  • Loading branch information
0702Yoon committed Dec 12, 2024
1 parent 79e7e5b commit 086bfc2
Showing 1 changed file with 33 additions and 63 deletions.
96 changes: 33 additions & 63 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,26 @@ on:

jobs:
build_and_push:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

- name: make application.yml
- name: Make application.yml
run: |
cd ./src/main/resources
touch ./application.yml
echo "${{ secrets.YML }}" > ./application.yml
shell: bash
echo "${{ secrets.YML }}" > application.yml
- name: make application-prod.yml
- name: Make application-prod.yml
run: |
cd ./src/main/resources
touch ./application-prod.yml
echo "${{ secrets.PROD_YML }}" > ./application-prod.yml
shell: bash
echo "${{ secrets.PROD_YML }}" > application-prod.yml
- name: Gradle Caching
uses: actions/cache@v3
Expand All @@ -43,7 +39,7 @@ jobs:
${{ runner.os }}-gradle-
- name: Setup Gradle
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
uses: gradle/actions/[email protected]

- name: Build with Gradle Wrapper
run: ./gradlew clean build
Expand All @@ -62,33 +58,22 @@ jobs:
registry-type: public

- name: Delete previous image in Amazon ECR Public
env:
REGISTRY_ALIAS: ${{ secrets.DEV_ECR_REGISTRY_ALIAS }}
REPOSITORY: distribute/dev
run: |
aws ecr-public batch-delete-image \
--repository-name distribute/dev \
--image-ids imageTag=latest \
--region us-east-1 || echo "No existing image with tag latest found."
--repository-name distribute/dev \
--image-ids imageTag=latest \
--region us-east-1 || echo "No existing image with tag latest found."
- name: Build, tag, and push docker image to Amazon ECR Public
env:
REGISTRY: ${{ steps.login-ecr-public.outputs.registry }}
REGISTRY_ALIAS: ${{ secrets.DEV_ECR_REGISTRY_ALIAS }}
REPOSITORY: distribute/dev
IMAGE_TAG: latest
run: |
echo "REPOSITORY: $REPOSITORY"
docker build -t $REGISTRY/$REGISTRY_ALIAS/$REPOSITORY:$IMAGE_TAG .
docker push $REGISTRY/$REGISTRY_ALIAS/$REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$REGISTRY/$REGISTRY_ALIAS/$REPOSITORY:$IMAGE_TAG"
docker build -t ${{ steps.login-ecr-public.outputs.registry }}/${{ secrets.DEV_ECR_REGISTRY_ALIAS }}/distribute/dev:latest .
docker push ${{ steps.login-ecr-public.outputs.registry }}/${{ secrets.DEV_ECR_REGISTRY_ALIAS }}/distribute/dev:latest
- name: Logout of Amazon ECR
run: docker logout ${{ env.ECR_REGISTRY }}
run: docker logout ${{ steps.login-ecr-public.outputs.registry }}

deploy:
needs: build_and_push

runs-on: ubuntu-latest

steps:
Expand All @@ -103,39 +88,32 @@ jobs:

- name: Login to Amazon ECR Public
run: |
aws ecr-public get-login-password --region us-east-1 | sudo docker login --username AWS --password-stdin public.ecr.aws
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
- name: SSH into EC2 and Deploy with Docker Compose
env:
SSH_KEY: ${{ secrets.DEV_EC2_SSH_KEY }}
HOST: ${{secrets.SERVER_IP}}
USER: ubuntu
run: |
echo "${SSH_KEY}" > ec2_key.pem
echo "${{ secrets.DEV_EC2_SSH_KEY }}" > ec2_key.pem
chmod 600 ec2_key.pem
# Connect to EC2 and run deployment commands
ssh -o StrictHostKeyChecking=no -i ec2_key.pem $USER@$HOST << 'EOF'
sudo usermod -aG docker $USER || true
newgrp docker || true
# Stop existing containers
if [ -f docker-compose.yml ]; then
echo "Stopping existing containers..."
sudo docker-compose down
else
echo "No existing Docker Compose setup. Proceeding with deployment..."
fi
# Clean up previous files
sudo rm -f docker-compose.yml
ssh -o StrictHostKeyChecking=no -i ec2_key.pem ubuntu@${{ secrets.SERVER_IP }} << 'EOF'
sudo usermod -aG docker $USER || true
newgrp docker || true
# Stop existing containers
if [ -f docker-compose.yml ]; then
echo "Stopping existing containers..."
sudo docker-compose down
else
echo "No existing Docker Compose setup. Proceeding with deployment..."
fi
# Clean up previous files
sudo rm -f docker-compose.yml
EOF
- name: Install Docker if not present
uses: appleboy/[email protected]
with:
host: ${{secrets.SERVER_IP}}
host: ${{ secrets.SERVER_IP }}
username: ubuntu
key: ${{ secrets.DEV_EC2_SSH_KEY }}
script: |
Expand All @@ -148,24 +126,16 @@ jobs:
fi
- name: Copy docker-compose.yml to EC2
env:
SSH_KEY: ${{ secrets.DEV_EC2_SSH_KEY }}
HOST: ${{secrets.SERVER_IP}}
USER: ubuntu
run: |
echo "$SSH_KEY" > ec2_key.pem
echo "${{ secrets.DEV_EC2_SSH_KEY }}" > ec2_key.pem
chmod 600 ec2_key.pem
scp -o StrictHostKeyChecking=no -i ec2_key.pem ./docker-compose.yml $USER@$HOST:~/docker-compose.yml
scp -o StrictHostKeyChecking=no -i ec2_key.pem ./docker-compose.yml ubuntu@${{ secrets.SERVER_IP }}:~/docker-compose.yml
- name: Start services using Docker Compose
env:
SSH_KEY: ${{ secrets.DEV_EC2_SSH_KEY }}
HOST: ${{secrets.SERVER_IP}}
USER: ubuntu
run: |
echo "$SSH_KEY" > ec2_key.pem
echo "${{ secrets.DEV_EC2_SSH_KEY }}" > ec2_key.pem
chmod 600 ec2_key.pem
ssh -o StrictHostKeyChecking=no -i ec2_key.pem $USER@$HOST << 'EOF'
ssh -o StrictHostKeyChecking=no -i ec2_key.pem ubuntu@${{ secrets.SERVER_IP }} << 'EOF'
cd /home/ubuntu
docker-compose -f docker-compose.yml pull
docker-compose -f docker-compose.yml up -d
Expand Down

0 comments on commit 086bfc2

Please sign in to comment.