一、拉取镜像
docker pull jenkinsci/blueocean
二、启动jenkins(可忽略)
docker run -d --name jk jenkinsci/blueocean
三、将配置文件复制出来(可忽略)
docker container cp jk:/var/jenkins_home /usr/jenkins_home
然后停掉容器
docker stop 容器id
docker rm 容器id
四、启动jenkins
docker run \
--name jk \
--restart always \
-u root \
-p 8080:8080 \
-v /usr/jenkins_home:/var/jenkins_home \
-d jenkinsci/blueocean
初始密码在/usr/jenkins_home/secrets
文件夹下,打开initialAdminPassword
,将密码复制出来,使用admin账号登录
cat /usr/jenkins_home/secrets/initialAdminPassword
等一会之后将密码复制进去:
推荐安装之后创建一个用户:
五、进行项目配置
进入系统配置-》全局工具配置
5.1 配置jdk
首先进入容器:
docker exec -it jk /bin/bash
把jdk地址填今jenkins中
或者勾选自动安装:
5.2配置maven
选择自动安装:
在插件管理中搜索maven,并进行安装
配置maven
阿里云镜像,安装之后进入jenkins
docker exec -it fed086226b57 /bin/bash
进入 /root/.m2
cd /root/.m2
新建settings.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
| This is the configuration file for Maven. It can be specified at two levels:
|
| 1. User Level. This settings.xml file provides configuration for a single user,
| and is normally provided in ${user.home}/.m2/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -s /path/to/user/settings.xml
|
| 2. Global Level. This settings.xml file provides configuration for all Maven
| users on a machine (assuming they're all using the same Maven
| installation). It's normally provided in
| ${maven.conf}/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -gs /path/to/global/settings.xml
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
|
|-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>/root/.m2/repository</localRepository>
<!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true
<interactiveMode>true</interactiveMode>
-->
<!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
-->
<!-- pluginGroups
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
|-->
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups>
<!-- proxies
| This is a list of proxies which can be used on this machine to connect to the network.
| Unless otherwise specified (by system property or command-line switch), the first proxy
| specification in this list marked as active will be used.
|-->
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
-->
</proxies>
<servers>
</servers>
<!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
|-->
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>XaNexus</id>
<repositories>
<repository>
<id>XaNexus</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>XaNexus</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>jdk1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>XaNexus</activeProfile>
</activeProfiles>
</settings>
六、 安装gitlab
首先docker拉取下来:
docker pull gitlab/gitlab-ce
运行gitlab:
docker run -d \
-p 443:443 -p 80:80 -p 222:22 \
--name gitlab \
--restart always \
-v /home/gitlab/config:/etc/gitlab \
-v /home/gitlab/logs:/var/log/gitlab \
-v /home/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce
运行之后首次登录不知道密码是多少,第一种比较麻烦:
进入容器中后进入gitlab控制台:
cd /opt/gitlab/bin/
gitlab-rails console -e production
此 user 则表示 root 用户
user = User.where(id: 1).first
修改密码
user.password = 'secret_pass’
确认密码
user.password_confirmation = ‘secret_pass’
user.save
exit
还有一种方法是查看initial_root_password
:
cat /home/gitlab/config/initial_root_password
这里gitlab就装好了
如果遇到git地址错乱,修改gitlab.rb
vim /home/gitlab/config/gitlab.rb
添加下面的代码,然后重启:
# 配置http协议所使用的访问地址,不加端口号默认为80
external_url 'http://192.168.18.93'
# 配置ssh协议所使用的访问地址和端口
gitlab_rails['gitlab_ssh_host'] = '192.168.18.93'
# 此端口是run时22端口映射的222端口
gitlab_rails['gitlab_shell_ssh_port'] = 222
创建一个库之后回到jenkins安装下面三个插件
Git Parameter
( 用于参数化构建中动态获取项目分支 )
Generic Webhook Trigger
( 用于解析 Webhook 传过来的参数 )
GitLab
( 用于推送构建结果给 GitLab )
生成一个token:
然后回到gitlab添加sshkey:
如果配置的是局域网,还要使用admin权限的账号进入admin控制台,设置自允许web hooks and services
配置webhook
:
添加一个 Webhook url是jenkins构建中提供的url http://ip/project/xxxx
,凭证是jenkins生成的secret
, 触发器选择 标签推送事件
在下方的edit中可以看到最近的请求情况,只有status是200的时候才是正常的:
七、jenkins使用
首先在设置中添加一个凭证:
将私钥以及用户名填进去:
新建一个任务:
选择源码管理:
构建触发器:
配置build:
然后就可以开始构建了,可以在控制台输出看到构建日志:
当gitlab有代码push的时候,将自动进行构建: