12 月 252020
 

Jenkins版本

Jenkins 2.263.1

Jenkins操作

SSH Server --> Test Configuration

错误提示

Failed to connect or change directory
jenkins.plugins.publish_over.BapPublisherException: Failed to connect and initialize SSH connection.

默认输出key格式

[root@iZwz92yjivclsut0awv6bjZ ~]# ssh-keygen -t ecdsa -b 256
Generating public/private ecdsa key pair.
Enter file in which to save the key (/root/.ssh/id_ecdsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_ecdsa.
Your public key has been saved in /root/.ssh/id_ecdsa.pub.
The key fingerprint is:
SHA256:bG0w07gnsQlE68UytVRe+ZlD21B9EeKN+OD4wmAf7cg root@iZwz92yjivclsut0awv6bjZ
The key's randomart image is:
+---[ECDSA 256]---+
| .o o.. .o +=|
| . = = .+ * o|
| = X oo = B.|
| . * X+ o * .|
| .oSo+o . . |
| ..=+= |
| E o |
| . |
| |
+----[SHA256]-----+
[root@iZwz92yjivclsut0awv6bjZ ~]# cat .ssh/id_ecdsa
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAaAAAABNlY2RzYS
1zaGEyLW5pc3RwMjU2AAAACG5pc3RwMjU2AAAAQQRs8Cw4Ydm7zowb4ZYpuqwqSfeaOXqw
byz0iL1KAsWJyA8swh/AG2eHW3HJd1QWYExeQBKGe/9kPBpdKP+JpVcmAAAAuO37TNHt+0
zRAAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBGzwLDhh2bvOjBvh
lim6rCpJ95o5erBvLPSIvUoCxYnIDyzCH8AbZ4dbccl3VBZgTF5AEoZ7/2Q8Gl0o/4mlVy
YAAAAhAOEdqVnSzZwrdb0QpuT6taMnF3LrIF+Wbo4TMMVnDcJiAAAAHHJvb3RAaVp3ejky
eWppdmNsc3V0MGF3djZialoBAgM=
-----END OPENSSH PRIVATE KEY-----
[root@iZwz92yjivclsut0awv6bjZ ~]#

指定输出key格式

[root@iZwz92yjivclsut0awv6bjZ ~]# ssh-keygen -t ecdsa -b 256 -m PEM
Generating public/private ecdsa key pair.
Enter file in which to save the key (/root/.ssh/id_ecdsa): 
/root/.ssh/id_ecdsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_ecdsa.
Your public key has been saved in /root/.ssh/id_ecdsa.pub.
The key fingerprint is:
SHA256:BsVkzYf82bO5QtF1gs85DN90JvyqGFSaMD9D2+xV+fI root@iZwz92yjivclsut0awv6bjZ
The key's randomart image is:
+---[ECDSA 256]---+
| o++ . o .|
| o+ * = +.B|
| . = X X @+|
| . O * & +|
| S. + o O |
| . . o + E|
| + . . |
| . o . |
| . |
+----[SHA256]-----+
[root@iZwz92yjivclsut0awv6bjZ ~]# cat .ssh/id_ecdsa
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIIPtHTiOHFbg0ib+xqpT/Ppu15gIVnnh1UGJjjZoiDr9oAoGCCqGSM49
AwEHoUQDQgAEp/PU4drzb49XzMmrnIa55Veb9K5coVX/67B6aNF3owQX3tCBSzFp
Y3BwI+m5jRZ8CDRwQgItF6jW6h8snr9xeg==
-----END EC PRIVATE KEY-----
[root@iZwz92yjivclsut0awv6bjZ ~]#

参数描述

-m key_format
Specify a key format for the -i (import) or -e (export) conversion options. The supported key formats are: “RFC4716” (RFC 4716/SSH2 public or private key), “PKCS8” (PEM PKCS8 public key) or “PEM” (PEM public key). The default conversion format is “RFC4716”.

 

2 月 232020
 

查看系统版本和当前默认的Ansible安装版本信息

[root@ip-172-31-40-41 ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@ip-172-31-40-41 ~]# yum info ansible
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: d36uatko69830t.cloudfront.net
 * epel: mirrors.aliyun.com
 * extras: d36uatko69830t.cloudfront.net
 * updates: d36uatko69830t.cloudfront.net
Available Packages
Name        : ansible
Arch        : noarch
Version     : 2.9.3
Release     : 1.el7
Size        : 17 M
Repo        : epel/x86_64
Summary     : SSH-based configuration management, deployment, and task execution system
URL         : http://ansible.com
License     : GPLv3+
Description : Ansible is a radically simple model-driven configuration management,
            : multi-node deployment, and remote task execution system. Ansible works
            : over SSH and does not require any software or daemons to be installed
            : on remote nodes. Extension modules can be written in any language and
            : are transferred to managed machines automatically.

[root@ip-172-31-40-41 ~]#

Redhat Ansible 官方企业级扩展源RMP包

https://releases.ansible.com/ansible/rpm/release/epel-7-x86_64/

使用YUM安装当前最新版本2.9.5版本

[root@ip-172-31-40-41 ~]# yum install https://releases.ansible.com/ansible/rpm/release/epel-7-x86_64/ansible-2.9.5-1.el7.ans.noarch.rpm
Loaded plugins: fastestmirror
ansible-2.9.5-1.el7.ans.noarch.rpm                                |  17 MB  00:00:03     
Examining /var/tmp/yum-root-6qPFbn/ansible-2.9.5-1.el7.ans.noarch.rpm: ansible-2.9.5-1.el7.ans.noarch
Marking /var/tmp/yum-root-6qPFbn/ansible-2.9.5-1.el7.ans.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package ansible.noarch 0:2.9.5-1.el7.ans will be installed
--> Processing Dependency: python-paramiko for package: ansible-2.9.5-1.el7.ans.noarch
Loading mirror speeds from cached hostfile
 * base: d36uatko69830t.cloudfront.net
 * epel: fedora.cs.nctu.edu.tw
 * extras: d36uatko69830t.cloudfront.net
 * updates: d36uatko69830t.cloudfront.net
--> Processing Dependency: sshpass for package: ansible-2.9.5-1.el7.ans.noarch
--> Running transaction check
---> Package python-paramiko.noarch 0:2.1.1-9.el7 will be installed
---> Package sshpass.x86_64 0:1.06-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================
 Package           Arch     Version              Repository                         Size
=========================================================================================
Installing:
 ansible           noarch   2.9.5-1.el7.ans      /ansible-2.9.5-1.el7.ans.noarch   103 M
Installing for dependencies:
 python-paramiko   noarch   2.1.1-9.el7          base                              269 k
 sshpass           x86_64   1.06-2.el7           extras                             21 k

Transaction Summary
=========================================================================================
Install  1 Package (+2 Dependent packages)

Total size: 103 M
Total download size: 290 k
Installed size: 104 M
Is this ok [y/d/N]:

查看已安装的Ansible版本信息

[root@ip-172-31-40-41 ~]# ansible --version
ansible 2.9.5
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
[root@ip-172-31-40-41 ~]#
6 月 082019
 

主机清单

Jenkins (18.163.103.73/Hong Kong)
Gitlab (45.32.95.186/Los Angeles)
www (45.77.85.36/Los Angeles)
client (136.244.67.202/London)

为已完成的Jenkins安装必要插件

Locale
This plugin lets you control the language of Jenkins.

Publish Over SSH
Send build artifacts over SSH

修改语言设置(强制显示英语)

配置git路径

Jenkins > Global Tool Configuration > Git
Path to Git executable /usr/sbin/git

为PHP项目Wordpress准备LAMP运行环境

查看系统原生PHP及MySQL版本

[root@www ~]# yum info php
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: repos-lax.psychz.net
 * extras: repos-lax.psychz.net
 * updates: repos-lax.psychz.net
Available Packages
Name        : php
Arch        : x86_64
Version     : 5.4.16
Release     : 46.1.el7_7
Size        : 1.4 M
Repo        : updates/7/x86_64
Summary     : PHP scripting language for creating dynamic web sites
URL         : http://www.php.net/
License     : PHP and Zend and BSD
Description : PHP is an HTML-embedded scripting language. PHP attempts to make
            : it easy for developers to write dynamically generated web pages.
            : PHP also offers built-in database integration for several
            : commercial and non-commercial database management systems, so
            : writing a database-enabled webpage with PHP is fairly simple. The
            : most common use of PHP coding is probably as a replacement for CGI
            : scripts.
            :
            : The php package contains the module (often referred to as mod_php)
            : which adds support for the PHP language to Apache HTTP Server.

[root@www ~]# yum info mariadb-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: repos-lax.psychz.net
 * extras: repos-lax.psychz.net
 * updates: repos-lax.psychz.net
Available Packages
Name        : mariadb-server
Arch        : x86_64
Epoch       : 1
Version     : 5.5.64
Release     : 1.el7
Size        : 11 M
Repo        : base/7/x86_64
Summary     : The MariaDB server and related files
URL         : http://mariadb.org
License     : GPLv2 with exceptions and LGPLv2 and BSD
Description : MariaDB is a multi-user, multi-threaded SQL database server. It is
            : a client/server implementation consisting of a server daemon
            : (mysqld) and many different client programs and libraries. This
            : package contains the MariaDB server and some accompanying files
            : and directories. MariaDB is a community developed branch of MySQL.

[root@www ~]#

配置PHP7.4 / MySQL 5.6环境

[root@www ~]# yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@www ~]# yum makecache

[root@www ~]# yum -y install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
[root@www ~]# yum makecache

修改MySQL YUM仓库配置文件,启用MySQL5.6版本

[root@www ~]# vi /etc/yum.repos.d/mysql-community.repo
# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[root@www ~]# yum makecache

执行环境安装

[root@www ~]# yum install httpd php74-php php74-php-pdo php74-php-pecl-mysql php74-php-gd mysql-community-server mysql-community-client

查看php版本信息

[root@www ~]# php74 -v
PHP 7.4.3 (cli) (built: Feb 18 2020 11:53:05) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
[root@www ~]#

启动Apache服务

[root@www ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@www ~]# systemctl start httpd
[root@www ~]#

准备测试文件

[root@www ~]# echo "<?php phpinfo(); ?>" > /var/www/html/hello.php
[root@www ~]# cat /var/www/html/hello.php 
<?php phpinfo(); ?>
[root@www ~]#

使用浏览器访问测试文件

为Jenkins服务器指向域名

C:\Users\harveymei>ping gitlab.bcoc.site

正在 Ping gitlab.bcoc.site [45.32.95.186] 具有 32 字节的数据:
来自 45.32.95.186 的回复: 字节=32 时间=335ms TTL=44
来自 45.32.95.186 的回复: 字节=32 时间=327ms TTL=44
来自 45.32.95.186 的回复: 字节=32 时间=335ms TTL=44
来自 45.32.95.186 的回复: 字节=32 时间=336ms TTL=44

45.32.95.186 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 327ms,最长 = 336ms,平均 = 333ms

C:\Users\harveymei>

添加YUM仓库

[root@gitlab ~]# curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash

执行安装

[root@gitlab ~]# sudo EXTERNAL_URL="https://gitlab.bcoc.site" yum install -y gitlab-ce

提示安装成功

Thank you for installing GitLab!
GitLab should be available at https://gitlab.bcoc.site

使用浏览器访问

配置代码仓库

新建仓库

获取基于https的仓库地址

https://gitlab.bcoc.site/root/wordpress.git

在客户端主机生成密钥对

[root@client ~]# ssh-keygen -t ed25519 -C "harvey.mei@linuxcache.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/root/.ssh/id_ed25519):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_ed25519.
Your public key has been saved in /root/.ssh/id_ed25519.pub.
The key fingerprint is:
SHA256:pBZks0rE/4PCRggJjT+QoBjE2k3fK+kh0mc98/HgOvA harvey.mei@linuxcache.com
The key's randomart image is:
+--[ED25519 256]--+
|O* .. +          |
|Oo..oo o         |
|+= +.oo..        |
|. =.o.o+.        |
|   =. o=S.       |
|  . *.O B o      |
|   o * = * +     |
|      . E o .    |
|        .o       |
+----[SHA256]-----+
[root@client ~]# cat .ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPqeQy7qZvEEcpf+R35DRMB8nXR2nmET049/QJ+z0Qm/ harvey.mei@linuxcache.com
[root@client ~]#

在gitlab用户账户设置中配置密钥

导入公钥详情

安装git命令行工具

[root@client ~]# yum -y install git

克隆远程仓库到本地

[root@client ~]# git config --global user.name "Harvey"
[root@client ~]# git config --global user.email "harvey.mei@linuxcache.com"
[root@client ~]# git clone git@gitlab.bcoc.site:root/wordpress.git
Cloning into 'wordpress'...
The authenticity of host 'gitlab.bcoc.site (45.32.95.186)' can't be established.
ECDSA key fingerprint is SHA256:vhmKGmm/onEV47Xe7T4QOr8t8hRfqoJS1sj3WN/T8oc.
ECDSA key fingerprint is MD5:6e:d7:f6:eb:1d:ff:f4:cf:c6:e7:70:da:36:32:ee:26.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.bcoc.site,45.32.95.186' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
[root@client ~]#

[root@client ~]# ls wordpress/
README.md
[root@client ~]#

下载Wordpress并解压至本地仓库目录

[root@client ~]# curl -O https://wordpress.org/latest.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11.8M  100 11.8M    0     0  8362k      0  0:00:01  0:00:01 --:--:-- 8361k
[root@client ~]# tar xzf latest.tar.gz
[root@client ~]# ls wordpress/
index.php        wp-admin              wp-cron.php        wp-mail.php
license.txt      wp-blog-header.php    wp-includes        wp-settings.php
readme.html      wp-comments-post.php  wp-links-opml.php  wp-signup.php
README.md        wp-config-sample.php  wp-load.php        wp-trackback.php
wp-activate.php  wp-content            wp-login.php       xmlrpc.php
[root@client ~]#

推送源代码至gitlab仓库

[root@client ~]# cd wordpress/
[root@client wordpress]# git add .
[root@client wordpress]# git commit -m "Wordpress 5.3.2"
[root@client wordpress]# git push -u origin master
Counting objects: 2124, done.
Compressing objects: 100% (2085/2085), done.
Writing objects: 100% (2123/2123), 12.10 MiB | 4.41 MiB/s, done.
Total 2123 (delta 213), reused 0 (delta 0)
remote: Resolving deltas: 100% (213/213), done.
To git@gitlab.bcoc.site:root/wordpress.git
   73d3e3f..c52a7e6  master -> master
Branch master set up to track remote branch master from origin.
[root@client wordpress]#

在Gitlab查看已推送的代码

配置Jenkins自动化任务

添加目标服务器及配置信息
Jenkins > Manage Jenkins > Configure System > SSH Servers > Add

配置Jenkins访问Gitlab服务器的账户鉴权
Jenkins > Credentials > System > Global credentials(unrestricted) > Add Credentials

新建任务(自由风格项目)

配置Gitlab仓库信息
Jenkisn > PHP > Source Code Management > Git

配置构建信息
Jenkins > PHP > Build > Send files or execute commands over SSH

执行构建任务
Jenkins > PHP > Build Now

查看任务的控制台输出

控制台输出文本信息

Started by user admin
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/PHP
using credential 13e0e649-fa87-4a60-8021-85276d986544
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url git@gitlab.bcoc.site:root/wordpress.git # timeout=10
Fetching upstream changes from git@gitlab.bcoc.site:root/wordpress.git
> git --version # timeout=10
using GIT_SSH to set credentials Gitlab Account
> git fetch --tags --progress git@gitlab.bcoc.site:root/wordpress.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision c52a7e62f3676005177293d706c9d902ee807d99 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f c52a7e62f3676005177293d706c9d902ee807d99 # timeout=10
Commit message: "Wordpress 5.3.2"
First time build. Skipping changelog.
SSH: Connecting from host [ip-172-31-32-243.ap-east-1.compute.internal]
SSH: Connecting with configuration [www] ...
SSH: EXEC: STDOUT/STDERR from command [exit 0] ...
SSH: EXEC: completed after 400 ms
SSH: Disconnecting configuration [www] ...
SSH: Transferred 1931 file(s)
Build step 'Send files or execute commands over SSH' changed build result to SUCCESS
Finished: SUCCESS

查看www服务器/var/www/html目录下部署的项目代码

使用浏览器访问