iac/.forgejo/workflows/tofu-issue-deploy.yml

125 lines
4.5 KiB
YAML
Raw Normal View History

2024-12-20 22:05:03 -05:00
name: Handle VM Deployment Issue
on:
issues:
2024-12-20 22:43:31 -05:00
types: [opened, reopened]
2024-12-20 22:05:03 -05:00
jobs:
process-issue:
2024-12-20 22:13:25 -05:00
if: contains(github.event.issue.labels.*.name, 'opentofu')
2024-12-20 22:05:03 -05:00
runs-on: docker
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Extract Issue Details
id: extract
run: |
echo "Extracting issue details..."
ISSUE_BODY="${{ github.event.issue.body }}"
2024-12-20 23:03:35 -05:00
VM_NAME=$(echo "$ISSUE_BODY" | grep -A 2 'VM Name' | tail -n 1 | xargs)
2024-12-20 23:40:57 -05:00
VM_ID=$(echo "$ISSUE_BODY" | grep -A 2 'VM UID' | tail -n 1 | xargs)
2024-12-20 23:10:34 -05:00
RAM=$(echo "$ISSUE_BODY" | grep -A 2 'RAM' | tail -n 1 | xargs)
CPUS=$(echo "$ISSUE_BODY" | grep -A 2 'CPU Cores' | tail -n 1 | xargs)
SCPU_TYPE=$(echo "$ISSUE_BODY" | grep -A 2 'CPU Type' | tail -n 1 | xargs)
NODE_NAME=$(echo "$ISSUE_BODY" | grep -A 2 'Node Name' | tail -n 1 | xargs)
VLAN_ID=$(echo "$ISSUE_BODY" | grep -A 2 'VLAN ID' | tail -n 1 | xargs)
DATASTORE=$(echo "$ISSUE_BODY" | grep -A 2 'Datastore ID' | tail -n 1 | xargs)
DISK_SIZE=$(echo "$ISSUE_BODY" | grep -A 2 'Disk Size' | tail -n 1 | xargs)
CLONE_VM_ID=$(echo "$ISSUE_BODY" | grep -A 2 'Clone VM ID' | tail -n 1 | xargs)
2024-12-21 00:13:06 -05:00
DESCRIPTION=$(echo "$ISSUE_BODY" | grep -A 2 'Description' | tail -n 1 | xargs)
2024-12-20 22:05:03 -05:00
# Determine user_data_file_id
if [ "$NODE_NAME" == "prox" ]; then
USER_DATA_FILE_ID="proxmox_virtual_environment_file.cloud_config.id"
else
USER_DATA_FILE_ID="proxmox_virtual_environment_file.cloud_config2.id"
fi
echo "VM_NAME=$VM_NAME" >> $GITHUB_ENV
echo "VM_ID=$VM_ID" >> $GITHUB_ENV
echo "RAM=$RAM" >> $GITHUB_ENV
echo "CPUS=$CPUS" >> $GITHUB_ENV
echo "SCPU_TYPE=$SCPU_TYPE" >> $GITHUB_ENV
echo "NODE_NAME=$NODE_NAME" >> $GITHUB_ENV
echo "VLAN_ID=$VLAN_ID" >> $GITHUB_ENV
echo "IP_METHOD=$IP_METHOD" >> $GITHUB_ENV
echo "DATASTORE=$DATASTORE" >> $GITHUB_ENV
echo "DISK_SIZE=$DISK_SIZE" >> $GITHUB_ENV
echo "CLONE_VM_ID=$CLONE_VM_ID" >> $GITHUB_ENV
echo "USER_DATA_FILE_ID=$USER_DATA_FILE_ID" >> $GITHUB_ENV
- name: Generate Terraform File
run: |
2024-12-20 23:10:34 -05:00
cat <<EOF > terraform/proxmox/$VM_NAME.tf
2024-12-20 22:05:03 -05:00
resource "proxmox_virtual_environment_vm" "$VM_NAME" {
node_name = "${{ env.NODE_NAME }}"
vm_id = "${{ env.VM_ID }}"
name = "${{ env.VM_NAME }}"
description = "${{ env.VM_NAME }}"
cpu {
cores = "${{ env.CPUS }}"
type = "${{ env.SCPU_TYPE }}"
2024-12-21 00:13:06 -05:00
architecture = "x86_64"
2024-12-20 22:05:03 -05:00
}
memory {
dedicated = "${{ env.RAM }}"
}
network_device {
bridge = "vmbr0"
vlan_id = "${{ env.VLAN_ID }}"
}
disk {
datastore_id = "${{ env.DATASTORE }}"
size = "${{ env.DISK_SIZE }}"
2024-12-21 00:13:06 -05:00
interface = "scsi0"
2024-12-20 22:05:03 -05:00
}
clone {
vm_id = "${{ env.CLONE_VM_ID }}"
}
initialization {
ip_config {
ipv4 {
address = "dhcp"
2024-12-21 00:32:00 -05:00
vlan_gateway
2024-12-20 22:05:03 -05:00
}
}
user_data_file_id = ${{ env.USER_DATA_FILE_ID }}
}
2024-12-21 00:13:06 -05:00
lifecycle {
ignore_changes = [
initialization[0].user_account[0].keys,
initialization[0].user_account[0].password,
initialization[0].user_account[0].username,
initialization[0].user_data_file_id
]
}
2024-12-20 22:05:03 -05:00
}
EOF
- name: Commit and Push Terraform File
run: |
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
2024-12-20 23:11:19 -05:00
git add terraform/proxmox/$VM_NAME.tf
2024-12-20 22:05:03 -05:00
git commit -m "Add VM $VM_NAME configuration"
git push
2024-12-20 23:26:02 -05:00
- name: Trigger Terraform Apply Workflow
uses: https://github.com/actions/github-script@v6
with:
script: |
github.rest.actions.createWorkflowDispatch({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: "tofu.yml",
ref: "main"
})