Jenkins Tutorial for Beginners

0. Foreword

1. Installing Jenkins

  • To use the latest LTS: docker pull jenkins/jenkins:lts-jdk11

Create Volume Command:

1
docker volume create jenkins_home

Run Command:

1
docker run -d -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 --restart=on-failure jenkins/jenkins:lts-jdk11

Note: To start a container in detached mode, you use -d=true or just -d option. Detached mode, shown by the option –detach or -d , means that a Docker container runs in the background of your terminal.

2. View Default Password

2.1 View log

1
docker logs <your_jenkins_container_id_or_name>

2.2 View file

1
docker exec <your_jenkins_container_id_or_name> cat /var/jenkins_home/secrets/initialAdminPassword

Switch to Domestic source

/var/jenkins_home/updates/default.json

1
2
sed -i 's#https://updates.jenkins.io/download#https://mirrors.tuna.tsinghua.edu.cn/jenkins#g' default.json
sed -i 's#http://www.google.com#https://www.baidu.com#g' default.json

3. Other

Nginx config

If you want to route to Jenkins using Nginx, you can refer to the following Nginx configuration:

1
2
3
4
5
6
7
location / {
proxy_pass http://jenkins_server_ip:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

Generic Webhook Plugin

https://plugins.jenkins.io/generic-webhook-trigger/

1
nohup sh /usr/local/hexo-server/automatic-deployment.sh > /dev/null 2>&1 &

Docker Compose manage jenkins

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
version: '3'

services:
jenkins:
image: jenkins/jenkins:lts
container_name: jenkins
restart: unless-stopped
privileged: true
user: root
ports:
- "8080:8080"
- "50000:50000"
volumes:
- /root/jenkins/jenkins_deploy:/jenkins_deploy # Jenkins deployment directory
- /root/jenkins/jenkins_home:/var/jenkins_home # Data volume
- /root/jenkins/.ssh:/root/.ssh # SSH key
- /var/run/docker.sock:/var/run/docker.sock # Docker socket
- /usr/bin/docker:/usr/bin/docker # Docker binary
- /usr/bin/docker-compose:/usr/local/bin/docker-compose # Docker Compose binary
environment:
- TZ=Asia/Shanghai
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
pipeline {
agent any

stages {
stage('Checkout') {
agent any
steps {
sh "rm -rf blog"
// Clone repository
sh "git clone {YOUR_PROJECT}"
}
}

stage('Build') {
agent {
docker {
image 'node:20'
}
}
steps {
dir('blog') {
sh 'rm -rf .git'
// Configure npm and pnpm registry
sh 'npm config set registry https://registry.npmmirror.com/'
sh 'npm install -g pnpm'
sh 'pnpm config set registry https://registry.npmmirror.com/'

// Install dependencies and hexo-cli
sh 'pnpm install hexo-cli'
sh 'pnpm install'

// Build the project
sh 'pnpm run build'

// Archive the public directory for the deployment stage
stash includes: 'public/**', name: 'blog-public'

// Create Nginx configuration file
writeFile file: 'default.conf', text: '''
server {
listen 80;
server_name _;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
# try_files $uri /index.html;

# 禁用缓存的指令
add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
add_header Pragma "no-cache";
add_header Expires "0";
etag off;
if_modified_since off;
}
}'''
// Stash Nginx configuration for deployment
stash includes: 'default.conf', name: 'nginx-config'
}
}
}

stage('Deploy') {
agent any
steps {
// Unstash the build artifacts
unstash 'blog-public'
unstash 'nginx-config'

// Create a directory to store the build artifacts temporarily
sh "rm -rf /jenkins_deploy/blog"
sh "mkdir -p /jenkins_deploy/blog"
sh "cp -r public /jenkins_deploy/blog"
sh "cp default.conf /jenkins_deploy/blog"

// Deploy using Docker on the host machine
sh '''
# Stop and remove existing container if it exists
docker stop blog-nginx || true
docker rm blog-nginx || true

# Start a new container with the updated content
docker run -d --name blog-nginx \
-p 4000:80 \
-v /root/jenkins/jenkins_deploy/blog/public:/usr/share/nginx/html \
-v /root/jenkins/jenkins_deploy/blog/default.conf:/etc/nginx/conf.d/default.conf:ro \
--restart unless-stopped \
nginx:alpine
'''
}
}
}
}

Jenkins Tutorial for Beginners
https://www.hardyhu.cn/2022/02/08/Jenkins-Tutorial-for-Beginners/
Author
John Doe
Posted on
February 8, 2022
Licensed under