Update deploy.yml #6
This file contains hidden or 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 via SSM | |
on: | |
push: | |
branches: [ main ] | |
jobs: | |
deploy: | |
name: Deploy Yii2 App | |
runs-on: ubuntu-latest | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
AWS_REGION: ${{ secrets.AWS_REGION }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Install AWS CLI | |
run: | | |
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" | |
unzip awscliv2.zip | |
sudo ./aws/install | |
- name: Get EC2 Instance ID by Tag | |
id: get_instance | |
run: | | |
INSTANCE_ID=$(aws ec2 describe-instances \ | |
--filters "Name=tag:Name,Values=${{ secrets.EC2_INSTANCE_NAME_TAG }}" \ | |
"Name=instance-state-name,Values=running" \ | |
--query "Reservations[].Instances[].InstanceId" \ | |
--output text) | |
echo "INSTANCE_ID=$INSTANCE_ID" >> $GITHUB_ENV | |
echo "Found instance: $INSTANCE_ID" | |
- name: Send deployment command via SSM | |
run: | | |
aws ssm send-command \ | |
--instance-ids "$INSTANCE_ID" \ | |
--document-name "AWS-RunShellScript" \ | |
--comment "Deploy Yii2 App using Docker" \ | |
--parameters 'commands=["cd /home/ubuntu/app", "git pull origin master", "docker compose down", "docker compose up --build -d"]' \ | |
--region $AWS_REGION \ | |
--output text |