Update README.md #26
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: Python | |
on: | |
push: | |
branches: | |
- main | |
- feature/* | |
- review/* | |
- fix/* | |
pull_request: | |
types: [opened, reopened] | |
jobs: | |
build-terraform-file: | |
runs-on: ghr-proxmox-vm-sthings-cicd | |
environment: k8s | |
container: | |
image: eu.gcr.io/stuttgart-things/machineshop:v1.7.2 | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Install dependencies | |
id: pip | |
run: | | |
pip install github-action-utils PyYAML Jinja2 | |
- name: Create VM config | |
id: renderConfig | |
uses: jannekem/[email protected] | |
with: | |
script: | | |
import yaml as yaml | |
import random | |
import string | |
from jinja2 import Environment, FileSystemLoader | |
import github_action_utils as gha_utils | |
def random_string_generation(length): | |
# choose random lowercase letters for unique name | |
letters = string.ascii_lowercase | |
result_str = ''.join(random.choice(letters) for i in range(length)) | |
return result_str | |
def write_file(testVars, output_file_name): | |
environment = Environment(loader=FileSystemLoader("tests/templates/")) | |
template = environment.get_template("module.tpl") | |
filename = "main.tf" | |
content = template.render( | |
name = output_file_name, | |
vm_count = random.choice(testVars['vm_count']), | |
vm_num_cpus = random.choice(testVars['vm_num_cpus']), | |
pve_datastore = random.choice(testVars['pve_datastore']), | |
pve_network = random.choice(testVars['pve_network']), | |
vm_disk_size = random.choice(testVars['vm_disk_size']), | |
vm_memory = random.choice(testVars['vm_memory']), | |
) | |
# Save template | |
with open(filename, mode="w", encoding="utf-8") as message: | |
message.write(content) | |
print(f"... wrote {filename}") | |
def main(): | |
### Generate Random String for VM name | |
str_tfvarName = "pipeline-" + random_string_generation(length = 5) | |
gha_utils.append_job_summary("Unique Name for VM's: " + str_tfvarName) | |
### Import Yaml file with all possible test values | |
with open('tests/test_values.yaml', 'r') as file: | |
testVars = yaml.safe_load(file) | |
print(testVars) | |
write_file(testVars, str_tfvarName) | |
if __name__ == '__main__': | |
main() | |
- name: Upload main file for job 2 | |
uses: actions/upload-artifact@v4 | |
with: | |
name: terraform_main | |
path: main.tf | |
test-terraform-apply: | |
needs: build-terraform-file | |
runs-on: ghr-proxmox-vm-sthings-cicd | |
environment: k8s | |
container: | |
image: hashicorp/terraform:1.6 | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
path: "./tests" | |
- name: Download main | |
uses: actions/download-artifact@v4 | |
with: | |
name: terraform_main | |
- name: Run teraform init, plan and apply | |
run: | | |
terraform init | |
terraform plan -var="pve_api_url=${{ secrets.PVE_API_URL }}" -var="pve_api_user=${{ secrets.PVE_API_USER }}" -var="pve_api_password=${{ secrets.PVE_API_PASSWORD }}" -var="vm_ssh_user=${{ secrets.VM_SSH_USER }}" -var="vm_ssh_password=${{ secrets.VM_SSH_PASSWORD }}" -var="pve_api_tls_verify=${{ vars.PVE_API_TLS_VERIFY }}" | |
terraform apply --auto-approve -var="pve_api_url=${{ secrets.PVE_API_URL }}" -var="pve_api_user=${{ secrets.PVE_API_USER }}" -var="pve_api_password=${{ secrets.PVE_API_PASSWORD }}" -var="vm_ssh_user=${{ secrets.VM_SSH_USER }}" -var="vm_ssh_password=${{ secrets.VM_SSH_PASSWORD }}" -var="pve_api_tls_verify=${{ vars.PVE_API_TLS_VERIFY }}" | |
- name: Upload tfstate file for cleanup | |
if: always() | |
uses: actions/upload-artifact@v4 | |
with: | |
name: terraform_state | |
path: terraform.tfstate | |
- name: Run Terraform Destroy | |
run: | | |
terraform destroy --auto-approve -var="pve_api_url=${{ secrets.PVE_API_URL }}" -var="pve_api_user=${{ secrets.PVE_API_USER }}" -var="pve_api_password=${{ secrets.PVE_API_PASSWORD }}" -var="vm_ssh_user=${{ secrets.VM_SSH_USER }}" -var="vm_ssh_password=${{ secrets.VM_SSH_PASSWORD }}" -var="pve_api_tls_verify=${{ vars.PVE_API_TLS_VERIFY }}" | |
cleanup: | |
if: ${{ always() }} | |
needs: test-terraform-apply | |
runs-on: ghr-proxmox-vm-sthings-cicd | |
environment: k8s | |
container: | |
image: hashicorp/terraform:1.6 | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
path: "./tests" | |
- name: Download tfstate | |
uses: actions/download-artifact@v4 | |
with: | |
name: terraform_state | |
- name: Download main | |
uses: actions/download-artifact@v4 | |
with: | |
name: terraform_main | |
- name: Run Terraform Destroy | |
run: | | |
terraform init | |
terraform destroy --auto-approve -var="pve_api_url=${{ secrets.PVE_API_URL }}" -var="pve_api_user=${{ secrets.PVE_API_USER }}" -var="pve_api_password=${{ secrets.PVE_API_PASSWORD }}" -var="vm_ssh_user=${{ secrets.VM_SSH_USER }}" -var="vm_ssh_password=${{ secrets.VM_SSH_PASSWORD }}" -var="pve_api_tls_verify=${{ vars.PVE_API_TLS_VERIFY }}" | |