Deploy to EC2 #235
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: | |
workflow_run: | |
workflows: ['Docker Image CI'] | |
types: | |
- completed | |
workflow_dispatch: | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
env: | |
EC2_IP: ${{ secrets.AWS_EC2_GIN_IP }} | |
steps: | |
- name: Setup SSH Key and Scan EC2 Host | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-keyscan -H ${{ env.EC2_IP }} >> ~/.ssh/known_hosts | |
- name: Execute Deployment Commands on EC2 | |
run: | | |
ssh -i ~/.ssh/id_rsa ubuntu@${{ env.EC2_IP }} " | |
docker pull ghcr.io/yju-okura/minori_gin:latest; | |
docker stop minori_gin; | |
docker rm minori_gin; | |
IMAGE_ID=$(sudo docker images -q ghcr.io/yju-okura/minori_gin:latest | head -n 1); | |
if [ ! -z \"$IMAGE_ID\" ]; then | |
docker images -q | grep -v $IMAGE_ID | xargs -r sudo docker rmi; | |
fi; | |
docker run -d -p 80:8080 --env-file /volume/.env --restart always --name minori_gin ghcr.io/yju-okura/minori_gin:latest" |