Merge pull request #146 from Nexters/feature/132 #95
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: Deploy to EC2 | |
on: | |
push: | |
branches: | |
- develop | |
jobs: | |
push-docker-image: | |
runs-on: ubuntu-latest | |
steps: | |
# Repo checkout | |
- name: Checkout | |
uses: actions/checkout@v3 | |
with: | |
token: ${{ secrets.TOKEN_GITHUB }} | |
submodules: true | |
# JDK 환경 셋팅 | |
- name: Set up JDK 11 | |
uses: actions/setup-java@v3 | |
with: | |
distribution: 'zulu' | |
java-version: '11' | |
cache: gradle | |
# Gradle Permission | |
- name: Grant execute permission for gradlew | |
run: chmod +x gradlew | |
# AWS Config | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v2 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ap-northeast-2 | |
# ECR Login | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
# Build App | |
- name: Gradle Build | |
run: | | |
./gradlew build -x test | |
# uses: gradle/gradle-build-action@v2 | |
# with: | |
# arguments: build | |
# Docker build & Push | |
- name: Build and Push docker image to Amazon ECR | |
env: | |
REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
REPOSITORY: keyme-api-server-ecr | |
IMAGE_TAG: latest | |
run: | | |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG . | |
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG | |
# SSH Connect & Deploy | |
- name: Deploy after connecting to ec2 | |
uses: appleboy/ssh-action@v0.1.10 | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ec2-user | |
key: ${{ secrets.EC2_KEY }} | |
script: | | |
aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin ${{ secrets.ECR_REGISTRY }} | |
ids=$(sudo docker ps -a -q) | |
for id in $ids | |
do | |
sudo docker stop $id && docker rm $id | |
done | |
docker image prune -a -f | |
docker pull ${{ secrets.ECR_REGISTRY }}/keyme-api-server-ecr:latest | |
docker run -d --log-driver=awslogs \ | |
--log-opt awslogs-region=ap-northeast-2 \ | |
--log-opt awslogs-group=keyme-api-log-group \ | |
--log-opt awslogs-stream=apiLog \ | |
-p 80:8080 \ | |
${{ secrets.ECR_REGISTRY }}/keyme-api-server-ecr:latest |