From e0709c1cf3b2fa1293849f1c81b233f74034be59 Mon Sep 17 00:00:00 2001 From: Arabasta Date: Sat, 13 Jul 2024 23:57:43 +0800 Subject: [PATCH] cd: Add deploy to EC2 --- .github/workflows/cd_spring_backend.yml | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cd_spring_backend.yml b/.github/workflows/cd_spring_backend.yml index 188dfbc..9d5786c 100644 --- a/.github/workflows/cd_spring_backend.yml +++ b/.github/workflows/cd_spring_backend.yml @@ -1,4 +1,4 @@ -name: CD +name: CD with Docker and AWS on: push: @@ -36,4 +36,22 @@ jobs: with: context: ./spring_backend push: true - tags: ${{ secrets.KEI_DOCKER_USERNAME }}/oracle_spring_backend:${{ github.sha }} \ No newline at end of file + tags: ${{ secrets.KEI_DOCKER_USERNAME }}/oracle_spring_backend:${{ github.sha }} + + - name: Deploy to EC2 + env: + EC2_SSH_KEY: ${{ secrets.KEI_EC2_SSH_KEY }} + EC2_PUBLIC_IPV4: ${{ secrets.KEI_EC2_PUBLIC_IPV4 }} + run: | + echo "${EC2_SSH_KEY}" > /tmp/key.pem + chmod 600 /tmp/key.pem + ssh -o StrictHostKeyChecking=no -i /tmp/key.pem ec2-user@${{ env.EC2_PUBLIC_IPV4 }} << 'EOF' + docker pull ${{ secrets.KEI_DOCKER_USERNAME }}/oracle_spring_backend:${{ github.sha }} + docker stop oracle_spring_backend || true + docker rm oracle_spring_backend || true + docker run -d \ + --name oracle_spring_backend \ + -p 8080:8080 \ + ${{ secrets.KEI_DOCKER_USERNAME }}/oracle_spring_backend:${{ github.sha }} + docker logs oracle_spring_backend + EOF \ No newline at end of file