Blog
Wie Sie Terraform Cloud VCS ausgelöste Läufe mit Ihrer CI/CD-Pipeline synchronisieren

Wie synchronisieren Sie von Terraform Cloud VCS ausgelöste Läufe mit Ihrer CI/CD-Pipeline? In diesem Blog stelle ich Ihnen ein kleines Dienstprogramm vor, mit dem Sie alle mit einem Commit verbundenen Läufe finden und auf deren Abschluss warten können.
Terraform Cloud-Läufe können entweder durch einen expliziten API-Aufruf oder durch einen Push an Ihr Git-Repository ausgelöst werden. Letzteres wird als VCS-ausgelöster Lauf bezeichnet. VCS-ausgelöste Läufe haben zwei Vorteile: Sie können mehrere Workspace-Läufe bei einem einzigen Commit auslösen und zweitens den Zugriff auf den Terraform Cloud Workspace zur gleichen Zeit einschränken.
Der Nachteil ist, dass der von VCS ausgelöste Lauf parallel zur CI/CD-Pipeline startet. Wenn Sie also
Für diesen speziellen Zweck haben wir die Dienstprogramme tfe-run-wait und tfe-run-apply erstellt. Das Dienstprogramm
Warten Sie auf die von Terraform Cloud VCS ausgelöste Ausführung
Um das Dienstprogramm zu demonstrieren, haben wir das Repository tfe-run-wait-demo erstellt, das immer planen und anwenden wird. Nachdem Sie also eine Änderung übertragen und gepusht haben, geben Sie ein:
tfe-run-wait --organization binx-io
--clone-url https://github.com/binxio/tfe-run-wait-demo.git
--branch main
--commit-sha $(git show --format=%H --no-patch)
Dies sucht nach allen Läufen, die mit dem Commit verbunden sind, und gibt die folgende Ausgabe aus:
INFO: waiting for run in binx-io:tfe-run-wait-demo for commit 304826b in repository https://github.com/binxio/tfe-run-wait-demo.git
INFO: found run https://app.terraform.io/app/binx-io/workspaces/tfe-run-wait-demo/runs/run-LqoFvBGZS6WVMLAr for commit 304826b
INFO: run-LqoFvBGZS6WVMLAr in workspace tfe-run-wait-demo in status planning, waited 1s
INFO: run-LqoFvBGZS6WVMLAr in workspace tfe-run-wait-demo has reached state cost_estimated
https://app.terraform.io/app/binx-io/workspaces/tfe-run-wait-demo/runs/run-LqoFvBGZS6WVMLAr
--------------------------------------------------------------------------------
Terraform v1.1.8
on linux_amd64
Configuring remote state backend...
Initializing Terraform configuration...
Terraform 1.1.8
local_file.timestamp: Plan to create
Plan: 1 to add, 0 to change, 0 to destroy.
--------------------------------------------------------------------------------
Wie Sie sehen können, wird auch der Plan angezeigt. Wären diesem Repository-Zweig mehrere Arbeitsbereiche zugeordnet, würde jeder der Läufe angezeigt werden.
Einen Terraform Cloud VCS ausgelösten Lauf anwenden
Wenn Sie den Plan überprüfen möchten, bevor Sie die Änderung anwenden, können Sie tfe-run-wait zusammen mit
und tfe-run-apply mit der Option Bestätigen verwenden. Nach einem Push geben Sie ein:
tfe-run-wait --organization binx-io
--clone-url https://github.com/binxio/tfe-run-wait-demo.git
--branch main
--commit-sha $(git show --format=%H --no-patch)
--wait-for-status cost_estimated
Dies sucht nach den Läufen, die mit dem Commit verbunden sind, und gibt die folgende Ausgabe aus:
INFO: found run https://app.terraform.io/app/binx-io/workspaces/tfe-run-wait-demo/runs/run-Tniodf33JHKMPrq6 for commit a7a94e8
INFO: apply run in binx-io:tfe-run-wait-demo for commit a7a94e8 in repository https://github.com/binxio/tfe-run-wait-demo.git
https://app.terraform.io/app/binx-io/workspaces/tfe-run-wait-demo/runs/run-Tniodf33JHKMPrq6
--------------------------------------------------------------------------------
Terraform v1.1.8
on linux_amd64
Configuring remote state backend...
Initializing Terraform configuration...
Terraform 1.1.8
local_file.timestamp: Plan to create
Plan: 1 to add, 0 to change, 0 to destroy.
--------------------------------------------------------------------------------
Es zeigt den Plan an. Wenn Sie mit dem Plan zufrieden sind, tippen Sie:
tfe-run-apply --organization binx-io
--clone-url https://github.com/binxio/tfe-run-wait-demo.git
--branch main
--commit-sha $(git show --format=%H --no-patch)
--comment 'it is all good'
--confirm
Das Ergebnis ist die folgende Ausgabe:
INFO: found run https://app.terraform.io/app/binx-io/workspaces/tfe-run-wait-demo/runs/run-LqoFvBGZS6WVMLAr for commit 304826b
INFO: apply run in binx-io:tfe-run-wait-demo for commit 304826b in repository https://github.com/binxio/tfe-run-wait-demo.git
https://app.terraform.io/app/binx-io/workspaces/tfe-run-wait-demo/runs/run-LqoFvBGZS6WVMLAr
--------------------------------------------------------------------------------
Terraform v1.1.8
on linux_amd64
Configuring remote state backend...
Initializing Terraform configuration...
Terraform 1.1.8
local_file.timestamp: Plan to create
Plan: 1 to add, 0 to change, 0 to destroy.
--------------------------------------------------------------------------------
want to apply this plan?
Wie Sie sehen können, werden Sie um Ihre Eingabe gebeten. Wenn Sie bestätigen, wird die folgende Ausgabe angezeigt:
INFO: apply run run-LqoFvBGZS6WVMLAr in workspace tfe-run-wait-demo from status cost_estimated
INFO: found run https://app.terraform.io/app/binx-io/workspaces/tfe-run-wait-demo/runs/run-LqoFvBGZS6WVMLAr for commit 304826b
INFO: run-LqoFvBGZS6WVMLAr in workspace tfe-run-wait-demo in status applying, waited 1s
INFO: run-LqoFvBGZS6WVMLAr in workspace tfe-run-wait-demo has reached state applied
--------------------------------------------------------------------------------
Terraform v1.1.8
on linux_amd64
Initializing plugins and modules...
Terraform 1.1.8
local_file.timestamp: Plan to create
local_file.timestamp: Creating...
local_file.timestamp: Creation complete after 0s [id=ba962809cc884a9cda2a28a6a0f39ba538100067]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs: 0
--------------------------------------------------------------------------------
INFO: apply of commit 304826bd000cf6755cf4b950d3f22d4be63e780c from repository https://github.com/binxio/tfe-run-wait-demo.git affected 1 workspaces, 0 failed
Das Dienstprogramm wird Sie weiterhin auffordern, alle betroffenen Arbeitsbereiche anzuwenden. Wenn Sie --prompt weglassen, wendet es automatisch alle Läufe an.
Installation
Sie installieren das Dienstprogramm, indem Sie eingeben:
pip install tfe-run-wait
Alternativ können Sie auch das Docker-Container-Image ghcr.io/binxio/tfe-run-wait:0.7.0 oder die Github-Aktion binxio/tfe-run-wait-action verwenden.
Eine kurze Beschreibung aller Kommandozeilenoptionen finden Sie unter tfe-run-wait.
Fazit
Mit den Dienstprogrammen tfe-run-wait und tfe-run-apply können Sie CI/CD-Pipelines mit von Terraform Cloud VCS ausgelösten Läufen synchronisieren.
Foto von Angela Loria auf Unsplash
Verfasst von

Mark van Holsteijn
Mark van Holsteijn is a senior software systems architect at Xebia Cloud-native solutions. He is passionate about removing waste in the software delivery process and keeping things clear and simple.
Unsere Ideen
Weitere Blogs
Contact



