Skip to content

Terraform Apply

Terraform Apply #7

# Workflow that do provision of the staging infrastructure and deploy the project.
name: Terraform Apply
#on:
# pull_request:
# branches:
# - master2-terraform
on: workflow_dispatch
jobs:
terraform:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up AWS CLI
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_TF_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_TF_SECRET_ACCESS_KEY }}
aws-region: eu-west-3
- name: Set up Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.6.4
- name: Terraform Init
run: |
cd terraform
terraform init
if: success()
- name: Terraform Plan
run: |
cd terraform
terraform plan -var-file=terraform.tfvars
if: success()
- name: Terraform Apply
run: |
cd terraform
terraform apply -var-file=terraform.tfvars -auto-approve
if: success()
- name: Extract Public IP
id: server_public_ip
run: echo "::set-output name=server_public_ip::$(cd terraform; terraform output server_public_ip)"
if: success()
- name: Set up SSH key for EC2
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
if: success()
- name: Transfer Docker Image to EC2
run: |
docker save -o 1village-image.tar 1village
scp -o StrictHostKeyChecking=no 1village-image.tar ec2-user@$server:~
ssh -o StrictHostKeyChecking=no ec2-user@$server 'cd ~ && docker load -i 1village-image.tar && docker-compose up -d'
shell: bash
env:
server: ${{steps.server_public_ip.outputs.server_public_ip}}
if: success()