Skip to content

Terraform Apply

Terraform Apply #24

# 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: get_server_ip
#run: echo "::set-output name=server_public_ip::$(cd terraform; terraform output -raw server_public_ip)"
#run: echo "server_public_ip={$(cd terraform; terraform output -raw server_public_ip)}" >> $GITHUB_OUTPUT
run: |
cd terraform
#echo "::set-output name=server_public_ip::$(terraform output -raw server_public_ip)"
#echo "server_public_ip=$(terraform output -raw server_public_ip)" >> $GITHUB_OUTPUT
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "server_public_ip<<$EOF" >> $GITHUB_OUTPUT
echo "$(terraform output server_public_ip)" >> $GITHUB_OUTPUT
echo "$EOF" >> $GITHUB_OUTPUT
if: success()
- name: Show IP
run: |
echo "Public IP ${{steps.get_server_ip.outputs.server_public_ip}}"
- name: Create .env file
env:
HOST_URL: ${{ steps.get_server_ip.outputs.server_public_ip }}
PLM_CLIENT_ID: ${{ secrets.STAGING_CLIENT_ID }}
run: |
cat << EOF > .env
NEXT_PUBLIC_BASE_APP=/api
NEXT_PUBLIC_HOST_URL=$HOST_URL
NEXT_PUBLIC_PLM_HOST=https://prof.parlemonde.org
NEXT_PUBLIC_CLIENT_ID=$PLM_CLIENT_ID
EOF
- name: Build Docker Image
run: |
docker build -t 1village .
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@${{ steps.get_server_ip.outputs.server_public_ip }}:/home/ec2-user
ssh -o StrictHostKeyChecking=no ec2-user@${{ steps.get_server_ip.outputs.server_public_ip }} 'cd ~ && docker load -i 1village-image.tar && docker-compose up -d'
shell: bash
#env:
# server: ${{ steps.get_server_ip.outputs.server_public_ip }}
if: failure()
- name: Terraform Destroy
run: |
cd terraform
terraform destroy