EC2 auto deploy #8
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: EC2 auto deploy | |
on: | |
pull_request: | |
branches: [ main ] | |
types: [closed] | |
workflow_dispatch: | |
jobs: | |
build: | |
runs-on: ubuntu-22.04 | |
steps: | |
# IP取得ライブラリをインストール | |
- name: Public IP Install | |
id: ip | |
uses: haythem/[email protected] | |
# BranchをCheckout | |
- name: Checkout | |
uses: actions/checkout@v2 | |
# AWS CLIをインストールする | |
- name: AWS CLI install | |
run: | | |
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" | |
unzip awscliv2.zip | |
sudo ./aws/install --update | |
aws --version | |
# AWS CLIにキーを設定をする | |
- name: AWS set Credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: us-east-1 | |
# デプロイする | |
- name: Deploy | |
run: | | |
# SSHのセキュリティグループを開放する | |
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.EC2_SECURITY_GROUP_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 | |
# SSH接続して、git pullする | |
echo "${{ secrets.GIT_PRIVATE_KEY }}" > private_key | |
chmod 600 private_key | |
ssh -oStrictHostKeyChecking=no ${{ secrets.EC2_USER_NAME }}@${{ secrets.EC2_HOST_NAME }} -i private_key "cd /home/ubuntu/cc-fullstuck-project/ && git checkout main && git pull origin main" | |
# SSHのセキュリティグループを閉じる | |
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.EC2_SECURITY_GROUP_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 |