Create hetzner infra #21
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: Hetzner Setup | |
run-name: Create hetzner infra | |
on: workflow_dispatch | |
env: | |
BUCKET_TF_STATE: ${{ secrets.BUCKET_TF_STATE}} | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
jobs: | |
terraform: | |
name: Create Hetzner Infra | |
runs-on: ubuntu-latest | |
steps: | |
- run: echo "Job is running on ${{ runner.os }}" | |
- run: echo "Branch name is ${{ github.ref }} and repository is ${{ github.repository }}." | |
- name: Check out repository code | |
uses: actions/checkout@v3 | |
- name: HashiCorp - Setup Terraform | |
uses: hashicorp/[email protected] | |
- name: Terraform init | |
id: init | |
run: terraform init -backend-config="bucket=$BUCKET_TF_STATE" | |
working-directory: hetzner | |
- name: Terraform validate | |
id: validate | |
run: terraform validate | |
working-directory: hetzner | |
- name: Terraform plan | |
id: plan | |
run: terraform plan -no-color -input=false -var "hcloud_token=${{ secrets.HCLOUD_TOKEN }}" -var "tailscale_key=${{ secrets.TAILSCALE_KEY }}" | |
continue-on-error: true | |
working-directory: hetzner | |
- name: Terraform Plan Status | |
if: steps.plan.outcome == 'failure' | |
run: exit 1 | |
- name: Terraform Apply | |
run: terraform apply -auto-approve -input=false -var "hcloud_token=${{ secrets.HCLOUD_TOKEN }}" -var "tailscale_key=${{ secrets.TAILSCALE_KEY }}" | |
working-directory: hetzner | |
- name: Sleep for 2 minutes while server reboots | |
run: sleep 2m | |
shell: bash | |
- run: echo "Job is ${{ job.status }}." | |
ansible: | |
name: Setup Nomad & Consul | |
needs: terraform | |
runs-on: ubuntu-latest | |
steps: | |
- name: Set up Python | |
uses: actions/setup-python@v2 | |
with: | |
python-version: "3.11" | |
- name: Install dependencies | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y ansible | |
python -m pip install --upgrade pip | |
- name: Tailscale | |
uses: tailscale/github-action@v1 | |
with: | |
authkey: ${{ secrets.TAILSCALE_EPHEMERAL_KEY }} | |
- name: Check out repository code | |
uses: actions/checkout@v3 | |
- name: Configure SSH | |
run: | | |
mkdir -p ~/.ssh/ | |
echo "$SSH_KEY" > ~/.ssh/server.key | |
chmod 600 ~/.ssh/server.key | |
cat >>~/.ssh/config <<END | |
Host server | |
HostName $SSH_HOST | |
User $SSH_USER | |
IdentityFile ~/.ssh/server.key | |
StrictHostKeyChecking no | |
END | |
env: | |
SSH_USER: ${{ secrets.SSH_USER }} | |
SSH_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
SSH_HOST: ${{ secrets.SSH_HOST }} | |
- name: Run Ansible Playbook | |
run: | | |
ansible-playbook ansible/setup-server.yml -i ansible/inventory.ini |