Skip to content

Commit a29ef65

Browse files
committed
Initial migration from kangasta/jenkins-examples
0 parents  commit a29ef65

35 files changed

Lines changed: 890 additions & 0 deletions

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* text=auto
2+
*.sh text eol=lf

.github/workflows/deploy.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Deploy
2+
on:
3+
push:
4+
branches:
5+
- main
6+
7+
jobs:
8+
deploy-pages:
9+
name: Deploy to pages
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
with:
14+
fetch-depth: 0
15+
- uses: actions/setup-python@v2
16+
with:
17+
python-version: "3.12"
18+
- name: Install dependencies
19+
run: |
20+
pip install -r requirements.txt
21+
- name: Build site
22+
run: |
23+
mkdocs build
24+
touch site/.nojekyll
25+
- name: Deploy to pages
26+
uses: JamesIves/github-pages-deploy-action@3.7.1
27+
with:
28+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29+
BRANCH: gh-pages
30+
FOLDER: site
31+
CLEAN: true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
site/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Toni Kangas
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# CI/CD Examples
2+
3+
Examples on how to develop CI/CD pipelines and how to configure development environment for working with CI/CD pipelines.
4+

examples/docker/docker-on-wsl.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Docker on WSL
2+
3+
If you do not already have Windows Subsystem for Linux (WSL) installed, see [Install Linux on Windows with WSL](https://docs.microsoft.com/en-us/windows/wsl/install) for instructions.
4+
5+
This document describes how to configure WSL 2, install recent version of Ubuntu (24.04 LTS), and install Docker as well as Docker compose.
6+
7+
## Configure WSL settings
8+
9+
Open PowerShell and check current WSL status by running `wsl --status`.
10+
11+
```pwsh
12+
wsl --status
13+
```
14+
15+
If default version is not 2, run `wsl --set-default-version 2`. This enables more recent, virtual machine based WSL, that is required for us to be able to run Docker daemon in the WSL.
16+
17+
```pwsh
18+
wsl --set-default-version 2
19+
```
20+
21+
If output indicates any problems with WSL 2 Kernel, such as `The WSL 2 kernel file is not found.`, run `wsl --update` and `wsl --shutdown`, as suggested. These command might require admin priviledges. To obtain these, find PowerShell, right click it, and select _Run as System Administrator_.
22+
23+
```pwsh
24+
wsl --update
25+
wsl --shutdown
26+
```
27+
28+
Run `wsl --status` again. The output should display 2 as default version, recent last update date, and kernel version.
29+
30+
If you already have recent version of Ubuntu installed, ensure that it uses WSL 2 by running `wsl --list --verbose`. If necessary, use `wsl --set-version` to update your instance to use WSL 2. Skip next section.
31+
32+
```pwsh
33+
wsl --list --verbose
34+
wsl --set-version "YOUR_DISTRO_HERE" 2
35+
```
36+
37+
## Install recent version of Ubuntu
38+
39+
Install recent version of Ubuntu from either Microsoft Store or PowerShell. These instructions use Ubuntu 22.04. Other distributions might also work, if you know what you are doing.
40+
41+
If you want to install distro through the PowerShell, run `wsl --list --online` and follow the instructions.
42+
43+
```pwsh
44+
wsl --list --online
45+
wsl --install -d Ubuntu-24.04
46+
```
47+
48+
If you want to install distro through Microsoft Store: Open Microsoft Store, search for `Ubuntu 22`, select _Ubuntu 22.04 LTS_, and click _Install_.
49+
50+
If not opened automatically, open the installed Ubuntu distro and follow instruction in installation wizard.
51+
52+
## Install Docker
53+
54+
Open Ubuntu you just installed and follow [Install Docker Engine on Ubuntu](https://docs.docker.com/engine/install/ubuntu/) instructions.
55+
56+
__tl;dr:__ Run the convenience script available in `get.docker.com`:
57+
58+
```bash
59+
curl -fsSL https://get.docker.com -o get-docker.sh
60+
sudo sh get-docker.sh
61+
```
62+
63+
Ignore the `WSL DETECTED: We recommend using Docker Desktop for Windows.` warning.
64+
65+
To be able to run docker commands without `sudo`, add current user to `docker` group with `usermod`. Note that you might have to logout and login for the changes to take effect.
66+
67+
```bash
68+
sudo usermod -aG docker $USER
69+
```
70+
71+
## Configure networking
72+
73+
By default, WSL 2 uses local DNS server to reflect network settings, such as VPN, from the host Windows to WSL instances. The IP address of this DNS server might fall into IP range used by the Dockers default bridge network. In that case, containers will not be able to resolve domains.
74+
75+
To configure working DNS inside containers you can either use public DNS or modify IP range used by Dockers default network. If you are working behind a corporate firewall, public DNS might not work.
76+
77+
=== "Use public DNS in WSL 2"
78+
79+
To use public DNS, you must first disable WSL from automatically generating DNS settings to `/etc/resolv.conf`. To do this, add following rows to `/etc/wsl.conf` file in your WSL instance, for example, by editing (or creating) it with `sudo nano /etc/wsl.conf`:
80+
81+
```conf
82+
[network]
83+
generateResolvConf = false
84+
```
85+
86+
For the changes to take effect, restart WSL by running `wsl --shutdown` in powershell and launching WSL instance again.
87+
88+
```pwsh
89+
wsl --shutdown
90+
```
91+
92+
Finally, configure DNS server manually by creating `/etc/resolv.conf` with following content, for example by opening it with `sudo nano /etc/resolv.conf`:
93+
94+
```conf
95+
nameserver 8.8.8.8
96+
```
97+
98+
=== "Configure IP range for Docker"
99+
100+
To configure Docker to use IP ranges that wont overlap with DNS server configured by WSL, add following content to `/etc/docker/daemon.json`, for example, by opening it with `sudo nano /etc/docker/daemon.json`:
101+
102+
```json
103+
{
104+
"bip": "172.21.0.1/16",
105+
"default-address-pools": [
106+
{
107+
"base":"172.22.0.0/16",
108+
"size":24
109+
}
110+
]
111+
}
112+
```
113+
114+
If you already have dockerd running, you have to restart it for the changes to take effect.
115+
116+
```bash
117+
sudo service docker restart
118+
```
119+
120+
If containers cannot resolve domains with above configuration, ensure that the above networks do not overlap with the DNS server configured by WSL by running `ipconfig` in powershell and checking the networks it outputs.
121+
122+
## Start Docker daemon
123+
124+
Start `dockerd` as a background process by using `service` or, alternatively, start `dockerd` directly.
125+
126+
```bash
127+
sudo service docker start
128+
```
129+
130+
Note that this must be done every time WSL is restarted. If `docker` commands print `Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?` error, run `sudo service docker status` command to check if `dockerd` is running and, if necessary, run `sudo service docker start` command again.
131+
132+
If you are working on Windows 11, you can use [boot settings](https://docs.microsoft.com/en-us/windows/wsl/wsl-config#boot-settings) to start Docker daemon automatically on WSL instance startup. To do this, add following rows to `/etc/wsl.conf` file in your WSL instance, for example, by editing (or creating) it with `sudo nano /etc/wsl.conf`.
133+
134+
```conf
135+
[boot]
136+
command = service docker start
137+
```
138+
139+
## Install Docker compose
140+
141+
Follow [Install Docker Compose](https://docs.docker.com/compose/install/) instructions for Linux.
142+
143+
__tl;dr:__ Install Docker Compose with the distros package manager:
144+
145+
```bash
146+
sudo apt-get update
147+
sudo apt-get install docker-compose-plugin
148+
```

examples/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# CI/CD Examples
2+
3+
This website contains examples on how to develop CI/CD pipelines and how to configure development environment for working with CI/CD pipelines.
4+
5+
## Installing Docker
6+
7+
See [Get Docker](https://docs.docker.com/get-docker/) for installation instructions.
8+
9+
If you use Docker a lot on Mac or Windows system, the recommended Docker Desktop is likely a good option for you. Note that, since January 2022, it has required paid subscription if used for commercial development.
10+
11+
Alternatively, Windows users can [install Docker on top of WSL2](./docker/docker-on-wsl/). For Mac users there are alternatives, such as [Colima](https://github.com/abiosoft/colima), available.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM python:3.12
2+
3+
RUN pip install ansible kubernetes && \
4+
apt update && \
5+
apt install jq -y && \
6+
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && \
7+
install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pipeline {
2+
agent any
3+
parameters {
4+
choice(name: 'ANIMAL', choices: ['cat', 'cow', 'dog', 'lion', 'pig'], description: 'Tag to use for deployment image')
5+
}
6+
stages {
7+
stage("deploy") {
8+
agent {
9+
dockerfile {
10+
dir "ansible-kubernetes"
11+
reuseNode true
12+
}
13+
}
14+
environment {
15+
K8S_AUTH_KUBECONFIG = credentials('kubeconfig')
16+
}
17+
steps {
18+
sh """
19+
ansible-playbook ansible-kubernetes/deploy-to-kubernetes.yml --extra-vars "animal=${params.ANIMAL}"
20+
"""
21+
// Do not use Groovy string interpolation for credential
22+
sh '''
23+
export KUBECONFIG=$K8S_AUTH_KUBECONFIG
24+
./ansible-kubernetes/wait-until-service-up.sh
25+
'''
26+
}
27+
}
28+
}
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Deploy application to Kubernetes with Ansible
2+
3+
This example contains a pipeline that deploys an simple hello-world application to a Kubernetes cluster.
4+
5+
## Prerequisites
6+
7+
You will need a running Kubernetes cluster, that supports services with `LoadBalancer` type, and a kubeconfig file that can be used to deploy application (a deployment and a service) into the cluster.
8+
9+
## Preparing the Jenkins instance
10+
11+
The pipeline provided by this example can be added to any Jenkins instance you have administrator access and can run pipeline stages with docker agent. For example, Jenkins configuration from [Docker-in-Docker](../dind-jenkins/) example can be used.
12+
13+
We will use secret file to configure credentials for managing the target Kubernetes cluster. To create the secret file credential, open _Global credentials_ from _Jenkins_ credentials store from [Manage Jenkins > Manage Credentials](http://localhost:8080/credentials/) and click _Add Credentials_ from the left side menu.
14+
15+
In the _New credentials_ form:
16+
17+
1. Select _Secret file_ as the credential _kind_
18+
2. Upload your kubeconfig to the _file_ input
19+
3. Configure _ID_ for the credential. [Jenkinsfile](./Jenkinsfile) uses `kubeconfig` as the ID.
20+
4. (Optionally) add a description.
21+
22+
## Configure the pipeline
23+
24+
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.
25+
26+
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/kangasta/jenkins-examples.git`. Ensure that branch specifier includes `main` branch of the repository and modify the Script Path to be `ansible-kubernetes\Jenkinsfile`.
27+
28+
After you have created the pipeline, try to execute it by clicking _Build Now_. The pipeline should have deployed the example application into the Kubernetes cluster with the default image tag (`cow`) defined in the [deploy-to-kubernetes.yml](./deploy-to-kubernetes.yml) Ansible playbook.
29+
30+
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.
31+
32+
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.

0 commit comments

Comments
 (0)