chore : 권한 부여 로직을 수정한다 #14
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
name: Backend Server CD | |
on: | |
push: | |
branches: | |
- main | |
- develop | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Grant execute permission for gradlew | |
run: chmod +x gradlew | |
- name: Execute Gradle build | |
run: ./gradlew clean build | |
- name: List JAR Files | |
run: | | |
ls -la build/libs/ | |
- name: Set up Docker Build | |
uses: docker/setup-buildx-action@v2 | |
- name : Set up SSH Key | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.EC2_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
- name : Copy docker-compose.yml and deploy.sh to EC2 | |
run: | | |
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ./docker-compose.yml [email protected]:/home/ec2-user | |
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ./deploy.sh [email protected]:/home/ec2-user | |
- name: Docker build and push to Docker Hub and copy docker-compose.yml to server | |
run: | | |
sudo docker login -u ${{ secrets.DOCKER_ID }} -p ${{ secrets.DOCKER_PASSWORD }} | |
cd ./api-server | |
sudo docker buildx build --push --platform linux/amd64 -t ${{ secrets.DOCKER_USERNAME }}/server . | |
sudo docker push ${{ secrets.DOCKER_USERNAME }}/server | |
cd ../batch | |
sudo docker buildx build --push --platform linux/amd64 -t ${{ secrets.DOCKER_USERNAME }}/batch . | |
sudo docker push ${{ secrets.DOCKER_USERNAME }}/batch | |
- name: Deploy to Prod | |
uses: appleboy/ssh-action@master | |
with: | |
key: ${{ secrets.EC2_PRIVATE_KEY }} | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USERNAME }} | |
port: 22 | |
script: | | |
sudo docker login -u ${{ secrets.DOCKER_ID }} -p ${{ secrets.DOCKER_PASSWORD }} | |
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/server | |
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/batch | |
- name: Grant Permission to docker-compose.yml and deploy.sh | |
run: | | |
sudo chmod +x deploy.sh | |
sudo chmod +x docker-compose.yml | |
- name: Execute Deploy Script | |
run: | | |
sudo ./deploy.sh | |
shell: bash | |
- name: Remove Docker Images | |
run : sudo docker image prune -f |