name: Deploy an example server using Terraform and Ansible
on:
workflow_dispatch:
inputs:
exampleName:
description: 'Example Name'
required: true
default: 'speed_test'
env:
TF_VAR_LINODE_API_TOKEN: ${{ secrets.LINODE_API_TOKEN }}
TF_VAR_LINODE_PUBLIC_SSH_KEY: ${{ secrets.LINODE_PUBLIC_SSH_KEY }}
TF_VAR_LINODE_ROOT_PASSWORD: ${{ secrets.LINODE_ROOT_PASSWORD }}
TF_VAR_SELECTED_EXAMPLE: ${{ github.event.inputs.exampleName }}
TF_VAR_REDIS_PASSWORD: ${{ secrets.REDIS_PASSWORD }}
TF_VAR_CA_PRIVATE_KEY: ${{ secrets.SSH_KEY }}
TF_VAR_CA_CERTIFICATE: ${{ secrets.CA_CERTIFICATE }}
jobs:
plan-server-infra:
runs-on: ubuntu-latest
outputs:
plan-exitcode: ${{ steps.plan.outputs.exitcode }}
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Download examples directory
id: download_examples
uses: dawidd6/action-download-artifact@v2
with:
workflow: deploy_example.yml
name: examples
path: examples
workflow_conclusion: completed
continue-on-error: true
- name: Decrypt and untar example directories
run: |
openssl enc -d -aes-256-cbc -pbkdf2 -pass env:PASSWORD -in examples.tar.gz.enc -out examples.tar.gz
tar -zxf examples.tar.gz --skip-old-files
working-directory: examples
shell: bash
env:
PASSWORD: ${{secrets.ARTIFACTS_PASSWORD}}
if: steps.download_examples.outcome == 'success'
- name: Set Up Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.0.4
cli_config_credentials_token: ${{ secrets.TERRAFORM_CLOUD_TOKEN }}
- name: Terraform plan
id: plan
run: |
terraform init
terraform plan -detailed-exitcode -out=./tfplan
working-directory: examples
continue-on-error: true
- name: Verify Terraform plan
if: ${{ steps.plan.outputs.exitcode == 1 }}
run: exit ${{ steps.plan.outputs.exitcode }}
- name: Cache examples dir for plan apply
uses: actions/cache@v2
if: ${{ steps.plan.outputs.exitcode == 2 }}
with:
path: examples
key: examples-to-update-${{ github.run_id }}
- name: Cache final examples dir
uses: actions/cache@v2
if: ${{ steps.plan.outputs.exitcode == 0 }}
with:
path: examples
key: examples-final-${{ github.run_id }}
apply-server-infra:
runs-on: ubuntu-latest
environment: Example
needs: [ plan-server-infra ]
if: ${{ needs.plan-server-infra.outputs.plan-exitcode == 2 }}
steps:
- name: Get examples dir for plan apply from cache
uses: actions/cache@v2
with:
path: examples
key: examples-to-update-${{ github.run_id }}
- name: Set Up Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.0.4
cli_config_credentials_token: ${{ secrets.TERRAFORM_CLOUD_TOKEN }}
- name: Terraform apply
run: |
terraform init
terraform show ./tfplan
terraform apply ./tfplan
working-directory: examples
- name: Cache final examples dir
uses: actions/cache@v2
with:
path: examples
key: examples-final-${{ github.run_id }}
configure-server:
runs-on: ubuntu-latest
needs: [ apply-server-infra ]
if: ${{ !failure() && !cancelled() }}
steps:
- name: Get final examples dir from cache
uses: actions/cache@v2
with:
path: examples
key: examples-final-${{ github.run_id }}
- name: Load SSH key
run: 'echo "$SSH_KEY" > /tmp/id_rsa && chmod 600 /tmp/id_rsa'
shell: bash
env:
SSH_KEY: ${{secrets.SSH_KEY}}
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Get Python dependencies from cache
uses: actions/cache@v2
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('examples/requirements.txt') }}
- name: Install Ansible dependencies
run: python3.9 -m pip install -r requirements.txt
working-directory: examples
- name: Install Ansible playbook dependencies
run: ansible-galaxy install -r ansible_requirements.yml
working-directory: examples
- name: Run Ansible playbook
run: |
cat ansible_inventory || true
ansible-playbook -i ansible_inventory --key-file /tmp/id_rsa ansible_playbook.yml --extra-vars "example_name=${{ github.event.inputs.exampleName }}"
working-directory: examples
upload-examples-directory:
runs-on: ubuntu-latest
needs: [ apply-server-infra ]
if: ${{ !failure() && !cancelled() }}
steps:
- name: Get final examples dir from cache
uses: actions/cache@v2
with:
path: examples
key: examples-final-${{ github.run_id }}
- name: Tar and encrypt Ansible inventory and example directories
run: |
tar -zcf examples.tar.gz $(ls -d */) ansible_inventory
openssl enc -aes-256-cbc -pbkdf2 -pass env:PASSWORD -in examples.tar.gz -out examples.tar.gz.enc
working-directory: examples
shell: bash
env:
PASSWORD: ${{secrets.ARTIFACTS_PASSWORD}}
- name: Upload encrypted examples directory
uses: actions/upload-artifact@v2
with:
name: examples
path: examples/examples.tar.gz.enc