[FIX] error on github actions env #147
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: TWTW Backend CD | |
on: | |
push: | |
branches: | |
- master | |
paths: | |
- '.github/workflows/backend-cd.yml' | |
workflow_run: | |
branches: | |
- "master" | |
workflows: ["TWTW Backend CI", "TWTW Nginx Build"] | |
types: | |
- completed | |
jobs: | |
server-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Configure AWS credentials | |
run: | | |
mkdir -p ~/.aws && touch ~/.aws/credentials | |
echo "[default]" >> ~/.aws/credentials | |
echo "aws_access_key_id=${{ secrets.AWS_ACCESS_KEY_ID }}" >> ~/.aws/credentials | |
echo "aws_secret_access_key=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> ~/.aws/credentials | |
- name: Generate provider.tf | |
run: | | |
echo 'provider "aws" {' > ./terraform/provider.tf | |
echo ' region = "${{ secrets.AWS_REGION }}"' >> ./terraform/provider.tf | |
echo ' access_key = "${{ secrets.AWS_ACCESS_KEY_ID }}"' >> ./terraform/provider.tf | |
echo ' secret_key = "${{ secrets.AWS_SECRET_ACCESS_KEY }}"' >> ./terraform/provider.tf | |
echo '}' >> ./terraform/provider.tf | |
- name: Set up Terraform | |
uses: hashicorp/setup-terraform@v1 | |
with: | |
terraform_version: 1.0.0 | |
- name: Generate environment-var.tf | |
run: | | |
echo "${{ secrets.TF_ENV_FILE }}" > ./terraform/environment-var.tf | |
- name: Run Terraform commands | |
run: | | |
cd ./terraform | |
terraform init | |
terraform apply -auto-approve | |
env: | |
TF_VAR_vpc_cidr_block: ${{ secrets.TF_VAR_vpc_cidr_block }} | |
TF_VAR_public_subnet_cidr: ${{ secrets.TF_VAR_public_subnet_cidr }} | |
TF_VAR_private_subnet_cidr: ${{ secrets.TF_VAR_private_subnet_cidr }} | |
TF_VAR_ami: ${{ secrets.TF_VAR_ami }} | |
TF_VAR_db_password: ${{ secrets.TF_VAR_db_password }} | |
TF_VAR_tags: ${{ secrets.TF_VAR_tags }} | |
TF_VAR_elb_protocol: ${{ secrets.TF_VAR_elb_protocol }} | |
TF_VAR_elb_port: ${{ secrets.TF_VAR_elb_port }} | |
TF_VAR_rabbitmq: ${{ secrets.TF_VAR_rabbitmq }} | |
- name: Get EC2 instance IP | |
id: get_ec2_ip | |
run: | | |
EC2_IP=$(aws ec2 describe-instances --query "Reservations[*].Instances[*].PublicIpAddress" --output=text) | |
echo "::set-output name=ec2_ip::$EC2_IP" | |
RABBITMQ_HOST=$(aws mq describe-broker --broker-id <broker-id> --query "BrokerInstances[0].ConsoleURL" --output text) | |
REDIS_HOST=$(aws elasticache describe-cache-clusters --cache-cluster-id <cluster-id> --query "CacheClusters[0].ConfigurationEndpoint.Address" --output text) | |
MYSQL_HOST=$(aws rds describe-db-instances --db-instance-identifier <db-instance-id> --query "DBInstances[0].Endpoint.Address" --output text) | |
echo "RABBITMQ_HOST=$RABBITMQ_HOST" > /var/www/TWTW/.env | |
echo "REDIS_HOST=$REDIS_HOST" >> /var/www/TWTW/.env | |
echo "MYSQL_HOST=$MYSQL_HOST" >> /var/www/TWTW/.env | |
echo "${{ secrets.ENV_FILE }}" >> /var/www/TWTW/.env | |
- name: Copy docker-compose file to server | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{ steps.get_ec2_ip.outputs.ec2_ip }} | |
username: ubuntu | |
source: "./docker-compose.prod.yml" | |
target: "/var/www/TWTW" | |
- name: Deploy using docker-compose | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ steps.get_ec2_ip.outputs.ec2_ip }} | |
username: ubuntu | |
script: | | |
cd /var/www/TWTW | |
sudo docker-compose -f docker-compose.prod.yml down | |
sudo docker-compose -f docker-compose.prod.yml pull | |
sudo docker-compose -f docker-compose.prod.yml up -d |