Jenkins+Gitlab+Ansible自动化部署(四)

接Jenkins+Gitlab+Ansible自动化部署(三)https://www.cnblogs.com/zd520pyx1314/p/10235394.html

Jenkins应用

Jenkins Linux Shell集成

登录Jenkins web管理页,点击新建任务

添加描述信息

 

添加构建执行shell

在执行shell输入框内输入

#!/bin/sh

user=`whoami`

if [ $user == 'deploy' ]
then
    echo "Hello, my name is $user"
else
    echo "Sorry, I am not $user"
fi

ip addr

cat /etc/system-release

free -m

df -h

py_cmd=`which python`

$py_cmd --version

 

 点击查看输出信息

Started by user admin
Building in workspace /var/lib/jenkins/workspace/shell-freestyle-job
[shell-freestyle-job] $ /bin/sh /tmp/jenkins3318863497197478868.sh
Hello, my name is deploy
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:23:04:a4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.244.131/24 brd 192.168.244.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::6c1f:4afc:8c42:2ef7/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:23:04:ae brd ff:ff:ff:ff:ff:ff
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:d5:9c:dd brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:d5:9c:dd brd ff:ff:ff:ff:ff:ff
CentOS Linux release 7.5.1804 (Core) 
              total        used        free      shared  buff/cache   available
Mem:           1982        1032         377          10         571         707
Swap:          2047           0        2047
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  4.6G   13G  26% /
devtmpfs                 975M     0  975M   0% /dev
tmpfs                    992M     0  992M   0% /dev/shm
tmpfs                    992M   11M  982M   2% /run
tmpfs                    992M     0  992M   0% /sys/fs/cgroup
/dev/sr0                 8.8G  8.8G     0 100% /media/cdrom
/dev/sda1                497M  150M  347M  31% /boot
tmpfs                    199M   12K  199M   1% /run/user/42
tmpfs                    199M     0  199M   0% /run/user/0
Python 2.7.5
Finished: SUCCESS

Jenkins 参数集成

仍然登录到Jenkins web管理页,点击新建任务。

添加描述信息

点击“参数化构建过程”

#!/bin/sh

echo "Current deploy environment is $deploy_env"
echo "The build is $version"
echo "The paasword is $pass"

if $bool
then
    echo "Request is approved"
else
    echo "Request is rejected"
fi

点击“Build with Parameters”后如下图所示

查看输出信息

Started by user admin
Building in workspace /var/lib/jenkins/workspace/parameter-freestyle-job
[parameter-freestyle-job] $ /bin/sh /tmp/jenkins459467524349987986.sh
Current deploy environment is dev
The build is 1.0.1
The paasword is 123456
Request is approved
Finished: SUCCESS

Jenkins Git集成

首先还是登录Jenkins web管理页,点击“New 任务”

添加描述信息

返回到gitlab新建一个ansible-playbook.repo工程(新建过程略),只有一个ansible-playbook.txt空文件

复制ansible-playbook.repo仓库地址,粘贴至下图

点击“立即构建”

查看输出信息

Started by user admin
Building in workspace /var/lib/jenkins/workspace/git-freestyle-job
Cloning the remote Git repository
Cloning repository https://gitlab.example.com/root/ansible-playbook-repo.git
 > git init /var/lib/jenkins/workspace/git-freestyle-job # timeout=10
Fetching upstream changes from https://gitlab.example.com/root/ansible-playbook-repo.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress https://gitlab.example.com/root/ansible-playbook-repo.git +refs/heads/*:refs/remotes/origin/*
 > git config remote.origin.url https://gitlab.example.com/root/ansible-playbook-repo.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://gitlab.example.com/root/ansible-playbook-repo.git # timeout=10
Fetching upstream changes from https://gitlab.example.com/root/ansible-playbook-repo.git
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress https://gitlab.example.com/root/ansible-playbook-repo.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision eb4736cb66dd954b1f45b1d38f789c84aaedb078 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f eb4736cb66dd954b1f45b1d38f789c84aaedb078
Commit message: "First comm"
First time build. Skipping changelog.
Finished: SUCCESS

Jenkins maven集成

打开maven官网:https://maven.apache.org/download.cgi

复制最新maven的软件包链接

http://mirrors.shu.edu.cn/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz

安装maven

[root@jenkins ~]# wget http://mirrors.shu.edu.cn/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz
[root@jenkins ~]# tar -xf apache-maven-3.6.0-bin.tar.gz -C /opt/
[root@jenkins ~]# ls /opt/
apache-maven-3.6.0  rh
[root@jenkins ~]# cd /opt/apache-maven-3.6.0/bin/
[root@jenkins bin]# ./mvn --version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
Maven home: /opt/apache-maven-3.6.0
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"

回到Jenkins web管理页,点击“New 任务”

添加描述信息

回到gitlab主页

 新建一个空的Java-war-dev工程,复制其仓库地址;

然后切到windows git bash命令行,上传本地代码到刚才新建的Java-war-dev仓库中:

xueji@xueji MINGW64 ~/Desktop/repo
$ pwd
/c/Users/xueji/Desktop/repo

xueji@xueji MINGW64 ~/Desktop/repo
$ ls
Java-war-dev/
xueji@xueji MINGW64 ~/Desktop/repo
$ git config --global user.name "root"

xueji@xueji MINGW64 ~/Desktop/repo
$ git config --global user.email "root@example.com"

xueji@xueji MINGW64 ~/Desktop/repo
$ git init
Initialized empty Git repository in C:/Users/xueji/Desktop/repo/.git/

xueji@xueji MINGW64 ~/Desktop/repo (master)
$ git remote add origin https://gitlab.example.com/root/java-war-dev.git

xueji@xueji MINGW64 ~/Desktop/repo (master)
$ git add .
warning: LF will be replaced by CRLF in Java-war-dev/pom.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in Java-war-dev/src/main/webapp/WEB-INF/web.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in Java-war-dev/src/main/webapp/index.jsp.
The file will have its original line endings in your working directory

xueji@xueji MINGW64 ~/Desktop/repo (master)
$ git commit -m"first commit"
[master (root-commit) aa3c70c] first commit
 7 files changed, 58 insertions(+)
 create mode 100644 Java-war-dev/.DS_Store
 create mode 100644 Java-war-dev/pom.xml
 create mode 100644 Java-war-dev/src/.DS_Store
 create mode 100644 Java-war-dev/src/main/.DS_Store
 create mode 100644 Java-war-dev/src/main/webapp/.DS_Store
 create mode 100644 Java-war-dev/src/main/webapp/WEB-INF/web.xml
 create mode 100644 Java-war-dev/src/main/webapp/index.jsp

xueji@xueji MINGW64 ~/Desktop/repo (master)
$ git -c http.sslVerify=false push origin master
Enumerating objects: 14, done.
Counting objects: 100% (14/14), done.
Delta compression using up to 4 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (14/14), 2.13 KiB | 726.00 KiB/s, done.
Total 14 (delta 3), reused 0 (delta 0)
To https://gitlab.example.com/root/java-war-dev.git
 * [new branch]      master -> master

查看gitlab上已经上传成功,复制Java-war-dev仓库地址;

 

再次回到Jenkins刚才创建的maven-freestyle-job中,将复制的仓库地址粘贴过来;

 接着添加构建步骤

返回到Jenkins系统管理设置jdk和maven路径

 

点击“Apply”,最后点击“Save”

回到Jenkins mavent-freestyle-job配置页;

试了好多次,中间错误过程及错误信息在本文结尾列出,针对我的环境,我猜测的是路径错误导致的,因为重新设置了路径之后,第16次构建,也就是上述构建成功的截图就是我重新建了一个Java-war-dev工程,直接在工程下面就是src目录,之前的错误因为我的目录结构是Java-war-dev工程下还有一个Java-war-dev文件夹,导致一直报错,在晚上转了一大圈,也没解决,最后我尝试新建了一个工程,重新配置了一下,构建成功!

查看输出信息

Started by user admin
Building in workspace /var/lib/jenkins/workspace/maven-freestyle-job
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://gitlab.example.com/root/Java-war-dev.git # timeout=10
Fetching upstream changes from https://gitlab.example.com/root/Java-war-dev.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress https://gitlab.example.com/root/Java-war-dev.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 87d659a9d8706b67e70a89fe7c9d5359d8fd72f5 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 87d659a9d8706b67e70a89fe7c9d5359d8fd72f5
Commit message: "master-1.0.0"
First time build. Skipping changelog.
[maven-freestyle-job] $ /opt/apache-maven-3.6.0/bin/mvn package
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.jenkins.demo:Java-war-dev >--------------------
[INFO] Building Java-war-dev Maven Webapp 1.0.15-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Java-war-dev ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/maven-freestyle-job/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Java-war-dev ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Java-war-dev ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/maven-freestyle-job/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ Java-war-dev ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ Java-war-dev ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ Java-war-dev ---
[INFO] Packaging webapp
[INFO] Assembling webapp [Java-war-dev] in [/var/lib/jenkins/workspace/maven-freestyle-job/target/Java-war-dev]
[INFO] Processing war project
[INFO] Copying webapp resources [/var/lib/jenkins/workspace/maven-freestyle-job/src/main/webapp]
[INFO] Webapp assembled in [21 msecs]
[INFO] Building war: /var/lib/jenkins/workspace/maven-freestyle-job/target/Java-war-dev.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.341 s
[INFO] Finished at: 2019-01-09T19:22:35+08:00
[INFO] ------------------------------------------------------------------------
Finished: SUCCESS

Jenkins Ansible集成演示

需要同anible主机上的配置ansible2.5+python 3.6虚拟环境一致,

配置Ansible2.5+python3.6

[root@jenkins Python-3.6.8]# yum install -y openssl* gcc zlib*
[root@jenkins ~]# wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz
[root@jenkins ~]# tar -xf Python-3.6.8.tar.xz -C /usr/local/src/
[root@jenkins ~]# cd /usr/local/src/Python-3.6.8/
[root@jenkins Python-3.6.8]# ./configure --prefix=/usr/local/ --with-ensurepip=install --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
[root@jenkins Python-3.6.8]# make && echo $? && sleep 3 && make altinstall && echo $?
[root@jenkins Python-3.6.8]# whereis pip3.6
pip3: /usr/local/bin/pip3.6
[root@jenkins Python-3.6.8]# whereis pip
pip: /usr/local/bin/pip3.6
[root@jenkins Python-3.6.8]# ln -s /usr/local/bin/pip3.6 /usr/local/bin/pip
[root@jenkins ~]# pip install virtualenv
[root@jenkins ~]# su - deploy
[deploy@jenkins ~]$ virtualenv -p /usr/local/bin/python3.6 .py3-a2.5-env
Already using interpreter /usr/local/bin/python3.6
Using base prefix '/usr/local'
New python executable in /home/deploy/.py3-a2.5-env/bin/python3.6
Also creating executable in /home/deploy/.py3-a2.5-env/bin/python
Installing setuptools, pip, wheel...
done.
[deploy@jenkins ~]$ cd /home/deploy/.py3-a2.5-env/
[deploy@jenkins .py3-a2.5-env]$ source /home/deploy/.py3-a2.5-env/bin/activate
(.py3-a2.5-env) [deploy@jenkins .py3-a2.5-env]$ pip install paramiko PyYAML jinja2
 (.py3-a2.5-env) [deploy@jenkins .py3-a2.5-env]$git clone https://github.com/ansible/ansible.git
(.py3-a2.5-env) [deploy@jenkins .py3-a2.5-env]$ cd ansible/
(.py3-a2.5-env) [deploy@jenkins ansible]$ pwd
/home/deploy/.py3-a2.5-env/ansible
(.py3-a2.5-env) [deploy@jenkins ansible]$ git checkout stable-2.5
Branch stable-2.5 set up to track remote branch stable-2.5 from origin.
Switched to a new branch 'stable-2.5'
(.py3-a2.5-env) [deploy@jenkins ansible]$ source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
(.py3-a2.5-env) [deploy@jenkins ansible]$ ansible --version
ansible 2.5.14 (stable-2.5 c748512c4c) last updated 2019/01/09 20:03:39 (GMT +800)
  config file = None
  configured module search path = ['/home/deploy/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/deploy/.py3-a2.5-env/ansible/lib/ansible
  executable location = /home/deploy/.py3-a2.5-env/ansible/bin/ansible
  python version = 3.6.8 (default, Jan  9 2019, 19:44:42) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

配置jenkins.example.com主机到test.example.com主机的ssh免秘钥认证

(.py3-a2.5-env) [deploy@jenkins ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/deploy/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/deploy/.ssh/id_rsa.
Your public key has been saved in /home/deploy/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:cXo/fOrw7FhywXZvsRmHUBkMPvuY2C3wdMIFTgeaZ1Q deploy@jenkins.example.com
The key's randomart image is:
+---[RSA 2048]----+
|            *=Eo |
|           B +o  |
|        . + B .  |
|         + = = . |
|        S o O +o.|
|         . X X .*|
|          + % +oo|
|           O = . |
|          .o*    |
+----[SHA256]-----+
(.py3-a2.5-env) [deploy@jenkins ~]$ ssh-copy-id root@test.example.com
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/deploy/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@test.example.com's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@test.example.com'"
and check to make sure that only the key(s) you wanted were added.

(.py3-a2.5-env) [deploy@jenkins ~]$ ssh root@test.example.com
Last login: Wed Jan  9 20:20:16 2019 from 192.168.244.131
[root@test ~]# hostname
test.example.com

配置ansible

[deploy@jenkins ~]$ pwd
/home/deploy
[deploy@jenkins ~]$ cat testservers
[testserver]
test.example.com  ansible_user=root

 

登录到jenkins web管理页,点击“New 任务”

 

添加描述信息

#!/bin/sh

set +x
source /home/deploy/.py3-a2.5-env/bin/activate
source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q

cd /home/deploy
ansible --version
ansible-playbook --version

cat testservers

ansible -i testservers testserver -m command -a "ip addr"
set -x

查看输出信息

Started by user admin
Building in workspace /var/lib/jenkins/workspace/ansible-freestyle-job
[ansible-freestyle-job] $ /bin/sh /tmp/jenkins7988516678097340995.sh
ansible 2.5.14 (stable-2.5 c748512c4c) last updated 2019/01/09 20:03:39 (GMT +800)
  config file = None
  configured module search path = ['/home/deploy/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/deploy/.py3-a2.5-env/ansible/lib/ansible
  executable location = /home/deploy/.py3-a2.5-env/ansible/bin/ansible
  python version = 3.6.8 (default, Jan  9 2019, 19:44:42) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
ansible-playbook 2.5.14 (stable-2.5 c748512c4c) last updated 2019/01/09 20:03:39 (GMT +800)
  config file = None
  configured module search path = ['/home/deploy/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/deploy/.py3-a2.5-env/ansible/lib/ansible
  executable location = /home/deploy/.py3-a2.5-env/ansible/bin/ansible-playbook
  python version = 3.6.8 (default, Jan  9 2019, 19:44:42) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
[testserver]
test.example.com  ansible_user=root

test.example.com | SUCCESS | rc=0 >>
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:61:aa:73 brd ff:ff:ff:ff:ff:ff
    inet 192.168.244.133/24 brd 192.168.244.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::7395:7e45:e2e6:ec36/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:61:aa:7d brd ff:ff:ff:ff:ff:ff
    inet 192.168.182.134/24 brd 192.168.182.255 scope global noprefixroute dynamic ens34
       valid_lft 1477sec preferred_lft 1477sec
    inet6 fe80::196:adf0:e929:d612/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
Finished: SUCCESS

 

 

下面错误信息就是上述构建jenkins maven集成时报错信息(可以随便看看): 

 

构建失败,查看错误输出信息

Started by user admin
Building in workspace /var/lib/jenkins/workspace/maven-freestyle-job
Cloning the remote Git repository
Cloning repository https://gitlab.example.com/root/java-war-dev.git
 > git init /var/lib/jenkins/workspace/maven-freestyle-job # timeout=10
Fetching upstream changes from https://gitlab.example.com/root/java-war-dev.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress https://gitlab.example.com/root/java-war-dev.git +refs/heads/*:refs/remotes/origin/*
 > git config remote.origin.url https://gitlab.example.com/root/java-war-dev.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://gitlab.example.com/root/java-war-dev.git # timeout=10
Fetching upstream changes from https://gitlab.example.com/root/java-war-dev.git
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress https://gitlab.example.com/root/java-war-dev.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision aa3c70c49a68bfc91b4fe65c38be0b4b15461a63 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f aa3c70c49a68bfc91b4fe65c38be0b4b15461a63
Commit message: "first commit"
First time build. Skipping changelog.
[maven-freestyle-job] $ /opt/apache-maven-3.6.0/bin/mvn package
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE

由报错信息可以看出,jdk环境设置有问题

以下报错信息目前没有解决掉,如果有热心同学知道解决办法的欢迎评论区指教^_^ ~,针对以上问题,我是删掉重新构建了一遍,包括全局凭据也是删掉重新创建了一个一样的,然后再次执行的时候就没有报错。

但是经反复确认,jdk路径没有错,接着有重新构建,提示以下内容

Started by user admin
Building in workspace /var/lib/jenkins/workspace/maven-freestyle-job
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://gitlab.example.com/root/java-war-dev.git # timeout=10
Fetching upstream changes from https://gitlab.example.com/root/java-war-dev.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress https://gitlab.example.com/root/java-war-dev.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision aa3c70c49a68bfc91b4fe65c38be0b4b15461a63 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f aa3c70c49a68bfc91b4fe65c38be0b4b15461a63
Commit message: "first commit"
 > git rev-list --no-walk aa3c70c49a68bfc91b4fe65c38be0b4b15461a63 # timeout=10
[maven-freestyle-job] $ /opt/apache-maven-3.6.0/bin/mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.112 s
[INFO] Finished at: 2019-01-09T17:46:04+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/var/lib/jenkins/workspace/maven-freestyle-job). Please verify you invoked Maven from the correct directory. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE

然后进行如下操作

[root@jenkins ~]# cd /var/lib/jenkins/workspace/maven-freestyle-job
[root@jenkins maven-freestyle-job]# ls
Java-war-dev
[root@jenkins maven-freestyle-job]# ls Java-war-dev/
pom.xml  src
[root@jenkins maven-freestyle-job]# mv Java-war-dev/pom.xml .
[root@jenkins maven-freestyle-job]# ls
Java-war-dev  pom.xml

再次执行构建操作,还是报错

........
[INFO] Building war: /var/lib/jenkins/workspace/maven-freestyle-job/target/Java-war-dev.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:36 min
[INFO] Finished at: 2019-01-09T18:04:52+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal 
org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project Java-war-dev: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE

 

改了pom.xml文件之后

            <artifactId>maven-release-plugin</artifactId>
            <version>3.6</version>
            <configuration>
                <webXml>WebRoot\WEB-INF\web.xml</webXml>
                <warSourceDirectory>WebRoot</warSourceDirectory>
                <!--autoVersionSubmodules>true</autoVersionSubmodules-->
            </configuration>

仍然报错

[INFO] Building war: /var/lib/jenkins/workspace/maven-freestyle-job/target/Java-war-dev.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.995 s
[INFO] Finished at: 2019-01-09T18:23:24+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project Java-war-dev: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE

再次更改pom.xml

 <version>3.6</version>
            <configuration>
                <webResources>
                    <resource>
                        <directory>web</directory>
                    </resource>
                </webResources>
                <!--webXml>WebRoot\WEB-INF\web.xml</webXml>
                <warSourceDirectory>WebRoot</warSourceDirectory-->
                <!--autoVersionSubmodules>true</autoVersionSubmodules-->
            </configuration>

仍然报错

[INFO] Building war: /var/lib/jenkins/workspace/maven-freestyle-job/target/Java-war-dev.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.982 s
[INFO] Finished at: 2019-01-09T18:26:38+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project Java-war-dev: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE

然而网友说的这些并不能解决问题。

posted on 2019-01-09 15:12  Lucky_7  阅读(1437)  评论(1编辑  收藏  举报

导航