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

128 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 22:43:14 -05:00
echo "ISSUE_BODY=$ISSUE_BODY"
2024-12-20 22:50:10 -05:00
# Check if 'VM Name' is in the body
echo "$ISSUE_BODY" | grep 'VM Name' # This should print the line with 'VM Name'
# Extract the value for VM_NAME
2024-12-20 22:48:15 -05:00
VM_NAME=$(echo "$ISSUE_BODY" | grep -A 1 'VM Name' | tail -n 1 | xargs)
2024-12-20 22:50:10 -05:00
echo "Extracted VM_NAME: '$VM_NAME'" # Debugging extracted value
if [ -z "$VM_NAME" ]; then
echo "Error: VM_NAME is empty. Check the formatting of the input."
else
echo "VM_NAME=$VM_NAME" # Display the value if extracted correctly
fi
2024-12-20 22:46:37 -05:00
2024-12-20 22:05:03 -05:00
VM_ID=$(echo "$ISSUE_BODY" | grep 'VM ID:' | cut -d':' -f2 | xargs)
RAM=$(echo "$ISSUE_BODY" | grep 'RAM:' | cut -d':' -f2 | xargs)
CPUS=$(echo "$ISSUE_BODY" | grep 'CPU Cores:' | cut -d':' -f2 | xargs)
SCPU_TYPE=$(echo "$ISSUE_BODY" | grep 'CPU Type:' | cut -d':' -f2 | xargs)
NODE_NAME=$(echo "$ISSUE_BODY" | grep 'Node Name:' | cut -d':' -f2 | xargs)
VLAN_ID=$(echo "$ISSUE_BODY" | grep 'VLAN ID:' | cut -d':' -f2 | xargs)
DATASTORE=$(echo "$ISSUE_BODY" | grep 'Datastore ID:' | cut -d':' -f2 | xargs)
DISK_SIZE=$(echo "$ISSUE_BODY" | grep 'Disk Size:' | cut -d':' -f2 | xargs)
CLONE_VM_ID=$(echo "$ISSUE_BODY" | grep 'Clone VM ID:' | cut -d':' -f2 | xargs)
2024-12-20 22:40:52 -05:00
echo "NODE_NAME=$NODE_NAME"
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: |
cat <<EOF > terraform/proxmox/$VM_ID.tf
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 }}"
}
memory {
dedicated = "${{ env.RAM }}"
}
network_device {
bridge = "vmbr0"
vlan_id = "${{ env.VLAN_ID }}"
}
disk {
datastore_id = "${{ env.DATASTORE }}"
size = "${{ env.DISK_SIZE }}"
}
clone {
vm_id = "${{ env.CLONE_VM_ID }}"
}
initialization {
ip_config {
ipv4 {
address = "dhcp"
}
}
user_data_file_id = ${{ env.USER_DATA_FILE_ID }}
}
}
EOF
- name: Commit and Push Terraform File
run: |
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
git add terraform/proxmox/$VM_ID.tf
git commit -m "Add VM $VM_NAME configuration"
git push
- name: Trigger Terraform Apply Workflow
2024-12-20 22:23:40 -05:00
uses: https://github.com/actions/github-script@v6
2024-12-20 22:05:03 -05:00
with:
script: |
github.rest.actions.createWorkflowDispatch({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: "tofu.yml",
ref: "main"
})