Create GCP VM #47
This file contains hidden or 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: Create GCP VM | |
| on: | |
| workflow_dispatch: | |
| push: | |
| branches: | |
| - 'deploy-*' | |
| tags: | |
| - 'v*' | |
| jobs: | |
| create-vm: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Load vars | |
| run: | | |
| eval $(yq e '. | to_entries | .[] | "echo \(.key)=\(.value);"' .github/workflows/vars.yaml) >> $GITHUB_ENV | |
| - name: Install Ansible | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y ansible | |
| ansible --version | |
| ansible-galaxy collection install google.cloud | |
| # Had to do it outside Ansible because the builtin copy module breaks the ssh file somehow | |
| - name: Save SSH key for ansible | |
| run: | | |
| echo "${{ secrets.GCP_SSH_PRIVATE_KEY }}" > ${{ env.SSH_PATH }} | |
| chmod 600 ${{ env.SSH_PATH }} | |
| - name: Extract branch/tag name | |
| id: extract | |
| run: | | |
| ref_name=${GITHUB_REF#refs/*/} | |
| vm_name="tsb-${ref_name//./-}" | |
| echo "vm_name=$vm_name" >> $GITHUB_OUTPUT | |
| echo "ref_name=$ref_name" >> $GITHUB_OUTPUT | |
| - name: Run Ansible Playbook | |
| env: | |
| GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }} | |
| GCP_SSH_PRIVATE_KEY: ${{ secrets.GCP_SSH_PRIVATE_KEY }} | |
| GCP_SSH_PUBLIC_KEY: ${{ secrets.GCP_SSH_PUBLIC_KEY }} | |
| GCP_PROJECT_ID: ${{ env.GCP_PROJECT_ID }} | |
| GCP_ZONE: ${{ env.GCP_ZONE }} | |
| VM_NAME: ${{ steps.extract.outputs.vm_name }} | |
| MACHINE_TYPE: ${{ env.MACHINE_TYPE }} | |
| IMAGE_NAME: ${{ env.IMAGE_NAME }} | |
| OWNER_LABEL: ${{ env.OWNER_LABEL }} | |
| TEAM_LABEL: ${{ env.TEAM_LABEL }} | |
| SSH_USER: ${{ env.SSH_USER }} | |
| SSH_PATH: ${{ env.SSH_PATH }} | |
| TSB_SYNC_PASS: ${{ secrets.TSB_SYNC_PASS }} | |
| TSB_SYNC_USR: ${{ secrets.TSB_SYNC_USR }} | |
| run: | | |
| ansible-playbook -i "localhost ansible_connection=local", ansible/playbook-gcp-vm-create.yaml | |
| - name: Run Ansible playbook on the VM | |
| run: | | |
| ansible-playbook -i /tmp/inventory.ini ansible/playbook.yaml | |
| - name: Always delete VM after failure | |
| if: failure() | |
| env: | |
| GCP_PROJECT_ID: ${{ env.GCP_PROJECT_ID }} | |
| GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }} | |
| GCP_ZONE: ${{ env.GCP_ZONE }} | |
| VM_NAME: ${{ steps.extract.outputs.vm_name }} | |
| run: | | |
| ansible-playbook -i "localhost ansible_connection=local", ansible/playbook-gcp-vm-delete.yaml |