東川印記

一本東川,笑看爭龍斗虎;寰茫兦者,度橫佰昧人生。

使用gitlab + maven nexus + Jenkins基于docker的自动化部署

2026年12月2日星期三



老方案了,新瓶装旧酒。。。。

1,下载docker compose

地址 https://github.com/docker/compose/releases

[root@SENRSL01 docker]# chmod +x docker-compose-linux-x86_64
[root@SENRSL01 docker]# ./docker-compose-linux-x86_64 --version
Docker Compose version v2.10.2
[root@SENRSL01 docker]#

2,修改ssh登录端口

gitlab使用的22与这个冲突

如果安装docker版gitlab,会跟这个22冲突

如果直接安装gitlab到当前系统,找了一遭资料看起来不冲突

这样的话看起来直接安装gitlab到系统更合适,并且gitlab docker版没有邮件功能。。。。

不过暂时还是继续走docker吧。。。。

修改系统ssh登录端口。。。。

[root@SENRSL01 senrsl]# vi /etc/ssh/sshd_config  //注意,是sshd_config,不是ssh_config

修改    Port 2221

#Port 22
Port 2221
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

重启sshd服务

[root@SENRSL01 senrsl]# systemctl restart sshd

然后配置防火墙。

重启sshd服务报错 error: Bind to port 2221 on 0.0.0.0 failed: Permission denied.

还需要修改selinux中sshd端口,在selinux中ssh端口增加2221

[root@SENRSL01 senrsl]# yum -y install policycoreutils-python

[root@SENRSL01 senrsl]# semanage port -l | grep ssh
ssh_port_t                     tcp      22
[root@SENRSL01 senrsl]# semanage port -a -t ssh_port_t -p tcp 2221

[root@SENRSL01 senrsl]# systemctl restart sshd
[root@SENRSL01 senrsl]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since 二 2022-09-06 15:57:51 CST; 6s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 17329 (sshd)
    Tasks: 1
   Memory: 1.1M
   CGroup: /system.slice/sshd.service
           └─17329 /usr/sbin/sshd -D

9月 06 15:57:51 SENRSL01 systemd[1]: Starting OpenSSH server daemon...
9月 06 15:57:51 SENRSL01 sshd[17329]: Server listening on 0.0.0.0 port 2221.
9月 06 15:57:51 SENRSL01 sshd[17329]: Server listening on :: port 2221.
9月 06 15:57:51 SENRSL01 systemd[1]: Started OpenSSH server daemon.
[root@SENRSL01 senrsl]#


3,安装gitlab

官方文档 https://docs.gitlab.com/ee/install/docker.html

dockerhub https://hub.docker.com/u/gitlab

有ce社区版和ee企业版两个版本。

[root@SENRSL01 senrsl]# docker pull gitlab/gitlab-ce:latest

继续docker安装

[root@SENRSL01 docker]# cat docker-compose.yml
version: '3.6'
services:
  web:
    image: 'gitlab/gitlab-ce:latest'
    container_name: "gitlab"
    restart: always
    hostname: '192.168.0.103'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://192.168.0.103:8900'
        # Add any other gitlab.rb configuration here, each on its own line
    ports:
      - '8900:8900'
      - '22:22'
    volumes:
      - '/home/senrsl/gitlab/config:/etc/gitlab'
      - '/home/senrsl/gitlab/logs:/var/log/gitlab'
      - '/home/senrsl/gitlab/data:/var/opt/gitlab'
    shm_size: '256m'

[root@SENRSL01 docker]#

[root@SENRSL01 docker]# ./docker-compose-linux-x86_64 compose up -d
[+] Running 2/2
 ⠿ Network docker_default  Created                                                                                                             0.0s
 ⠿ Container gitlab        Started                                                                                                             0.4s
[root@SENRSL01 docker]# docker ps
CONTAINER ID   IMAGE                        COMMAND                  CREATED         STATUS                   PORTS                                                                                           NAMES
1d31ae642be3   gitlab/gitlab-ce:latest      "/assets/wrapper"        3 minutes ago   Up 3 minutes (healthy)   80/tcp, 0.0.0.0:22->22/tcp, :::22->22/tcp, 0.0.0.0:8900->8900/tcp, :::8900->8900/tcp, 443/tcp   gitlab

等了一会,访问 http://192.168.0.103:8900/  就通了

默认密码

[root@SENRSL01 docker]# docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password
Password: Zh7XeRhaJOUnp96N4XL3XxH5e5VABS089fBk6ck5Ko=
[root@SENRSL01 docker]# 

登录后设置时区及语言。。。。


补充,也可以指定域名

先配置域名指向或拦截,然后修改docker compose文件重新生成

[root@SENRSL01 docker]# cat docker-compose.yml
version: '3.6'
services:
  web:
    image: 'gitlab/gitlab-ce:latest'
    container_name: "gitlab"
    restart: always
    hostname: 'gitlab.test.com'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://gitlab.test.com:8900'
        # Add any other gitlab.rb configuration here, each on its own line
    ports:
      - '8900:8900'
      - '22:22'
    volumes:
      - '/home/senrsl/gitlab/config:/etc/gitlab'
      - '/home/senrsl/gitlab/logs:/var/log/gitlab'
      - '/home/senrsl/gitlab/data:/var/opt/gitlab'
    shm_size: '256m'

[root@SENRSL01 docker]#

这样就不用暴露地址了。

然后配置ssh密钥

解决多地址密钥冲突

.ssh/config文件添加

# Private GitLab instance
Host gitlab.test.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_ed25519_103

之前那台mac应该有同样情况。


a)安装gitlab runner 版

runner 是gitlab用来cicd的方案,安好了作用应该跟Jenkins系列一样。

也有docker版,但是为什么都弄得这么大。。。。下载那么费劲。。。。

https://hub.docker.com/r/gitlab/gitlab-runner/tags

[root@SENRSL01 senrsl]# docker pull gitlab/gitlab-runner

[root@SENRSL01 gitlab]# docker run -d --name gitlab-runner --restart always \
>   -v /home/senrsl/gitlab/runner:/etc/gitlab-runner \
>   -v /var/run/docker.sock:/var/run/docker.sock \
>   gitlab/gitlab-runner:latest

[root@SENRSL01 gitlab]# docker run --rm -it -v /home/senrsl/gitlab/runner:/etc/gitlab-runner gitlab/gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=7 revision=bbcb5aba version=15.3.0
Running in system-mode.                            
                                                   
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://gitlab.test.com/
Enter the registration token:
xxKDD716dVozNb-nbnbK
Enter a description for the runner:
[b0e6e60a9946]: runner4gitlab
Enter tags for the runner (comma-separated):
think1
Enter optional maintenance note for the runner:

Registering runner... succeeded                     runner=xxKDD716
Enter an executor: parallels, shell, ssh, docker+machine, docker-ssh+machine, custom, docker, docker-ssh, virtualbox, kubernetes:
docker
Enter the default Docker image (for example, ruby:2.7):
dcjz/jdk:1.8u202
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
 
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"
[root@SENRSL01 gitlab]#

然后在gitlab里就能看到runner了

此处配图1


然后运行一下,发现一直阻塞,找到管理中心-runner里面,修改runner设置,勾选 运行未标记的作业。

应该是默认必须要标签一直才可以。之前设置了think1的标签。勾选后不是think1标签的也运行。

然后报错 Using Docker executor with image docker pull dcjz/jdk:1.8u202 ...
Pulling docker image docker pull dcjz/jdk:1.8u202 ...
WARNING: Failed to pull image with policy "always": invalid reference format (manager.go:235:0s)
ERROR: Job failed: failed to pull image "docker pull dcjz/jdk:1.8u202" with specified policies [always]: invalid reference format (manager.go:235:0s)

cat /etc/gitlab-runner/config.toml 看了下,镜像复制多了,直接填 dcjz/jdk:1.8u202就好。

竟然没有vi,只能删了重建。。。。

然后再去运行一个流水就可以了。。。。

此处配图2。

多建几个runner,就可以多个任务同时跑了。

然后看了下这个 .gitlab-ci.yml,啥都没有。。。。

# This file is a template, and might need editing before it works on your project.
# This is a sample GitLab CI/CD configuration file that should run without any modifications.
# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
# it uses echo commands to simulate the pipeline execution.
#
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
#
# You can copy and paste this template into a new `.gitlab-ci.yml` file.
# You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword.
#
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# This specific template is located at:
stages: # List of stages for jobs, and their order of execution
- build
- test
- deploy
build-job: # This job runs in the build stage, which runs first.
stage: build
script:
- echo "Compiling the code..."
- echo "Compile complete."
unit-test-job: # This job runs in the test stage.
stage: test # It only starts when the job in the build stage completes successfully.
script:
- echo "Running unit tests... This will take about 60 seconds."
- sleep 60
- echo "Code coverage is 90%"
lint-test-job: # This job also runs in the test stage.
stage: test # It can run at the same time as unit-test-job (in parallel).
script:
- echo "Linting code... This will take about 10 seconds."
- sleep 10
- echo "No lint issues found."
deploy-job: # This job runs in the deploy stage.
stage: deploy # It only runs when *both* jobs in the test stage complete successfully.
environment: production
script:
- echo "Deploying application..."
- echo "Application successfully deployed."

b)补充

看起来gitlab可以直接关联k8s和terraform.

4,安装maven nexus

地址为 https://hub.docker.com/r/sonatype/nexus3/

[root@SENRSL01 nexus3]# docker run -d --restart=always -p 8081:8081 -v /home/senrsl/nexus3/data:/var/nexus-data --name nexus3 sonatype/nexus3:latest

注意挂在的目录,不能直接挂载/nexus-data,不然会没权限。

查看默认密码

[root@SENRSL01 nexus3]# docker exec -it nexus3 cat /opt/sonatype/sonatype-work/nexus3/admin.password
8579d545-9abd-4fe4-8708-81f8d740b1f2[root@SENRSL01 nexus3]#

a)创建repo hosted类型

用于公司内部快照及release发包

创建maven2(hosted)  对应创建 expand-releases、expand-snapshots 一般不建用默认的maven-releases那一套就行。

选择 allow redeploy.

b)创建repo proxy类型

创建maven2(hosted),代理google

c)创建docker仓库hosted

harbor只能作为私有仓库,这个可以作为私有或公有

先新建一个Blob Store名字叫做docker-hub,File Type

配图3

然后创建docker(hosted)类型repo docker-local

设置HTTP 端口8083,用于发包到仓库

启用Enable Docker V1 API,选择Allow redeploy.

配图4

也可以创建docker(proxy)用于代理外部仓库。

d)创建docker仓库group

跟maven group一样,代理其他多个库

配图5

e)发镜像到nexus3

http: server gave HTTP response to HTTPS client

docker自1.3后与docker register交互默认使用https,使用http就会报这个错误。

SENRSL:docker senrsl$ docker login -u admin   nexus.test.com:8082
Password:
Error response from daemon: Get "https://nexus.test.com:8082/v2/": http: server gave HTTP response to HTTPS client
SENRSL:docker senrsl$



d)使用

把新建的库添加到maven-public这个group中,直接

依赖 http://nexus.test.com/repository/maven-public/

就行。


5,安装jekins


6,配置


一晃四年了。。。。 

2026年12月02日15:37:12


--
senRsl
2022年09月06日14:36:56

没有评论 :

发表评论