Skip to content

Commit 123edf3

Browse files
authored
Add pipeline for deploying app to Kubernetes with OpenTofu (#36)
1 parent ed19850 commit 123edf3

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

docs/tutorials/jenkins/ansible-kubernetes/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ In the _New credentials_ form:
2525

2626
## Configure the pipeline
2727

28-
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.
28+
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.
2929

3030
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`.
3131

@@ -47,6 +47,6 @@ After the Ansible playbook has been executed, the pipeline runs [wait-until-serv
4747
---8<--- "docs/tutorials/jenkins/ansible-kubernetes/wait-until-service-up.sh"
4848
```
4949

50-
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.
50+
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.
5151

5252
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.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
String d = "docs/tutorials/jenkins/opentofu-kubernetes"
2+
def status = 0
3+
4+
pipeline {
5+
agent any
6+
environment {
7+
KUBE_CONFIG_PATH = credentials('kubeconfig')
8+
}
9+
stages {
10+
stage('Checkout') {
11+
steps {
12+
git branch: 'main', url: 'https://github.com/cicd-tutorials/feedback.git'
13+
}
14+
}
15+
stage('Plan') {
16+
agent { docker {
17+
image 'ghcr.io/opentofu/opentofu:latest'
18+
args '--entrypoint=""'
19+
reuseNode true
20+
} }
21+
steps {
22+
dir("iac/tf") {
23+
sh "tofu init -no-color"
24+
script {
25+
status = sh returnStatus: true, script: "tofu plan -no-color -detailed-exitcode -out ${env.BUILD_TAG}.plan"
26+
}
27+
stash includes: "${env.BUILD_TAG}.plan", name: 'plan'
28+
}
29+
}
30+
}
31+
stage('Apply') {
32+
agent { docker {
33+
image 'ghcr.io/opentofu/opentofu:latest'
34+
args '--entrypoint=""'
35+
reuseNode true
36+
} }
37+
when {
38+
beforeInput true
39+
equals expected: 2, actual: status
40+
}
41+
input {
42+
message 'Apply the plan?'
43+
ok 'Apply'
44+
}
45+
steps {
46+
dir("iac/tf") {
47+
unstash 'plan'
48+
sh """
49+
tofu init -no-color
50+
tofu apply -no-color ${env.BUILD_TAG}.plan
51+
"""
52+
}
53+
}
54+
}
55+
}
56+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
description: Tutorial on how to configure a Jenkins pipeline that deploys an feedback application to a Kubernetes cluster.
3+
---
4+
5+
# Deploy application to Kubernetes with OpenTofu
6+
7+
This tutorial contains a pipeline that deploys an feedback application to a Kubernetes cluster.
8+
9+
## Prerequisites and preparing the Jenkins instance
10+
11+
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.
12+
13+
## Configure the pipeline
14+
15+
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.
16+
17+
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.
18+
19+
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.
20+
21+
```groovy title="Jenkinsfile"
22+
---8<--- "docs/tutorials/jenkins/opentofu-kubernetes/Jenkinsfile"
23+
```
24+
25+
After you have created the pipeline, try to execute it by clicking _Build Now_. The pipeline should have deployed the example application.
26+
27+
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.

mkdocs.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ nav:
2020
- Jenkins:
2121
- ./tutorials/jenkins/jenkins-host-docker/README.md
2222
- ./tutorials/jenkins/ansible-kubernetes/README.md
23+
- ./tutorials/jenkins/opentofu-kubernetes/README.md
2324
- ./tutorials/jenkins/build-status-pipelines/README.md
2425
- ./tutorials/jenkins/parallel-robot-pipeline/README.md
2526
- ./tutorials/jenkins/sonarqube-jenkins/README.md

0 commit comments

Comments
 (0)