Deploy to EC2 #3
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_dispatch: | |
push: | |
branches: | |
- main | |
paths: | |
- app/** | |
jobs: | |
deploy: | |
name: Deploy to EC2 | |
runs-on: ubuntu-latest | |
env: | |
AWS_INSTANCE_SG_ID: ${{ secrets.AWS_INSTANCE_SG_ID }} | |
AWS_REGION: eu-west-2 | |
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID}} | |
AWS_ROLE_NAME: ${{ secrets.AWS_ROLE_NAME}} | |
steps: | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v2 | |
with: | |
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ secrets.AWS_ROLE_NAME }} | |
aws-region: ${{ env.AWS_REGION }} | |
- name: Get runner public IP address | |
id: ip | |
uses: haythem/public-ip@v1.2 | |
- name: Whitelist GitHub Actions runner IP address | |
run: | | |
echo "Adding runner IP address to security group..." | |
aws ec2 authorize-security-group-ingress \ | |
--group-id $AWS_INSTANCE_SG_ID \ | |
--protocol tcp \ | |
--port 22 \ | |
--cidr ${{ steps.ip.outputs.ipv4 }}/32 | |
- name: Deploy to EC2 | |
uses: appleboy/ssh-action@v0.1.7 | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ubuntu | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
cd ~/yt_translator | |
app/post_update.sh | |
- name: Revoke GitHub Actions runner IP address | |
run: | | |
echo "Removing runner IP address from security group..." | |
aws ec2 revoke-security-group-ingress \ | |
--group-id $AWS_INSTANCE_SG_ID \ | |
--protocol tcp \ | |
--port 22 \ | |
--cidr ${{ steps.ip.outputs.ipv4 }}/32 |