From 52d3874e18d4eb7f9cd2ccb9474ea8dab69889f3 Mon Sep 17 00:00:00 2001 From: Toni Kangas Date: Tue, 17 Jun 2025 01:37:35 +0300 Subject: [PATCH] Add pipeline for deploying app to Kubernetes with OpenTofu --- .../jenkins/ansible-kubernetes/README.md | 4 +- .../jenkins/opentofu-kubernetes/Jenkinsfile | 56 +++++++++++++++++++ .../jenkins/opentofu-kubernetes/README.md | 27 +++++++++ mkdocs.yaml | 1 + 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 docs/tutorials/jenkins/opentofu-kubernetes/Jenkinsfile create mode 100644 docs/tutorials/jenkins/opentofu-kubernetes/README.md diff --git a/docs/tutorials/jenkins/ansible-kubernetes/README.md b/docs/tutorials/jenkins/ansible-kubernetes/README.md index 39b0652..3b5b067 100644 --- a/docs/tutorials/jenkins/ansible-kubernetes/README.md +++ b/docs/tutorials/jenkins/ansible-kubernetes/README.md @@ -25,7 +25,7 @@ In the _New credentials_ form: ## Configure the pipeline -First, create a new pipeline via [New Item](http://localhost:8080/view/all/newJob) button in the rigth side menu of the Jenkins dashboard. The name of the pipeline could be for example `Animals` and it should be an pipeline. +First, create a new pipeline via [New Item](http://localhost:8080/view/all/newJob) button in the right side menu of the Jenkins dashboard. The name of the pipeline could be for example `Animals` and it should be an pipeline. In the configure pipeline view, scroll to the bottom and under Pipeline sub-header select `Pipeline script from SCM`. SCM type should be `Git` and Repository URL the url of this repository: `https://github.com/cicd-tutorials/cicd-tutorials.net.git`. Ensure that branch specifier includes `main` branch of the repository and modify the Script Path to be `docs/tutorials/jenkins/ansible-kubernetes/Jenkinsfile`. @@ -47,6 +47,6 @@ After the Ansible playbook has been executed, the pipeline runs [wait-until-serv ---8<--- "docs/tutorials/jenkins/ansible-kubernetes/wait-until-service-up.sh" ``` -You can find the URL of the created load-balancer from the console output of the build. Open the application with your browser or user curl to see the application response. +You can find the URL of the created load-balancer from the console output of the build. Open the application with your browser or use curl to see the application response. In addition, after the first execution Jenkins should have updated the project configuration to contain parameters defined in the pipeline and we can configure the image tag in _Build with Parameters_ menu. diff --git a/docs/tutorials/jenkins/opentofu-kubernetes/Jenkinsfile b/docs/tutorials/jenkins/opentofu-kubernetes/Jenkinsfile new file mode 100644 index 0000000..37cd85e --- /dev/null +++ b/docs/tutorials/jenkins/opentofu-kubernetes/Jenkinsfile @@ -0,0 +1,56 @@ +String d = "docs/tutorials/jenkins/opentofu-kubernetes" +def status = 0 + +pipeline { + agent any + environment { + KUBE_CONFIG_PATH = credentials('kubeconfig') + } + stages { + stage('Checkout') { + steps { + git branch: 'main', url: 'https://github.com/cicd-tutorials/feedback.git' + } + } + stage('Plan') { + agent { docker { + image 'ghcr.io/opentofu/opentofu:latest' + args '--entrypoint=""' + reuseNode true + } } + steps { + dir("iac/tf") { + sh "tofu init -no-color" + script { + status = sh returnStatus: true, script: "tofu plan -no-color -detailed-exitcode -out ${env.BUILD_TAG}.plan" + } + stash includes: "${env.BUILD_TAG}.plan", name: 'plan' + } + } + } + stage('Apply') { + agent { docker { + image 'ghcr.io/opentofu/opentofu:latest' + args '--entrypoint=""' + reuseNode true + } } + when { + beforeInput true + equals expected: 2, actual: status + } + input { + message 'Apply the plan?' + ok 'Apply' + } + steps { + dir("iac/tf") { + unstash 'plan' + sh """ + tofu init -no-color + tofu apply -no-color ${env.BUILD_TAG}.plan + """ + } + } + } + } +} diff --git a/docs/tutorials/jenkins/opentofu-kubernetes/README.md b/docs/tutorials/jenkins/opentofu-kubernetes/README.md new file mode 100644 index 0000000..716a23e --- /dev/null +++ b/docs/tutorials/jenkins/opentofu-kubernetes/README.md @@ -0,0 +1,27 @@ +--- +description: Tutorial on how to configure a Jenkins pipeline that deploys an feedback application to a Kubernetes cluster. +--- + +# Deploy application to Kubernetes with OpenTofu + +This tutorial contains a pipeline that deploys an feedback application to a Kubernetes cluster. + +## Prerequisites and preparing the Jenkins instance + +See [Deploy application to Kubernetes with Ansible](../ansible-kubernetes/) tutorial for prerequisites and instruction for configuring access from the Jenkins instance to the Kubernetes cluster. + +## Configure the pipeline + +First, create a new pipeline via [New Item](http://localhost:8080/view/all/newJob) button in the right side menu of the Jenkins dashboard. The name of the pipeline could be for example `Feedback-Deploy` and it should be an pipeline. + +In the configure pipeline view, scroll to the bottom and under Pipeline sub-header select `Pipeline script`. Copy the script from below to the _Script_ textarea. + +The pipeline deploys an example application to a Kubernetes cluster using OpenTofu configuration. The pipeline will first plan the deployment and then ask approval from an user before applying the plan to the target cluster. + +```groovy title="Jenkinsfile" +---8<--- "docs/tutorials/jenkins/opentofu-kubernetes/Jenkinsfile" +``` + +After you have created the pipeline, try to execute it by clicking _Build Now_. The pipeline should have deployed the example application. + +You can find the URL of the created application from the console output of the build. Open the application with your browser or use curl to see the application response. diff --git a/mkdocs.yaml b/mkdocs.yaml index 3d45414..0689090 100644 --- a/mkdocs.yaml +++ b/mkdocs.yaml @@ -20,6 +20,7 @@ nav: - Jenkins: - ./tutorials/jenkins/jenkins-host-docker/README.md - ./tutorials/jenkins/ansible-kubernetes/README.md + - ./tutorials/jenkins/opentofu-kubernetes/README.md - ./tutorials/jenkins/build-status-pipelines/README.md - ./tutorials/jenkins/parallel-robot-pipeline/README.md - ./tutorials/jenkins/sonarqube-jenkins/README.md