docke+gitlab+jenkins+springboot

时间:2022-12-15 10:56:03

安装Gitlab

开放防火墙端口80和配置映射文件夹

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
mkdir -p /docker_data/gitlab/{data,logs,config}

启动Gitlab容器(启动容器之前确保80,443端口没用被占用,被占用就改成其它的。这个启动比较费时,我差点认为它没跑起来!

docker run -d --hostname 192.168.1.11 -p 80:80 -p 443:443 --privileged=true --restart=always -v /docker_data/gitlab/config:/etc/gitlab -v /docker_data/gitlab/data:/var/opt/gitlab -v /docker_data/gitlab/logs:/var/log/gitlab --shm-size 256m --name gitlab-ce-15.6.2 gitlab/gitlab-ce:15.6.2-ce.0

获取Gitlab密码(gitlab-ce-15.6.2是容器名称,我个人习惯用容器名称 - 版本号命名

docker exec -it gitlab-ce-15.6.2 grep 'Password:' /etc/gitlab/initial_root_password

ip:80访问,账号:root,密码就是上面那行命令输出的。
太长了分开写《docker安装Gitlab》

安装jenkins

宿主机安装jdk

《Linux之Centos7安装JDK17》想装jdk8就下载jdk,操作也是一样的。

宿主机安装maven

wget -P /docker_data/jenkins/maven/ https://archive.apache.org/dist/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz

解压到指定文件夹

tar -zxvf /docker_data/jenkins/maven/apache-maven-3.8.1-bin.tar.gz -C /docker_data/jenkins/maven/

设置环境变量

vim /etc/profile

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL追加内容

# maven
export MAVEN_HOME=/docker_data/jenkins/maven/apache-maven-3.8.1
export PATH=$PATH:$MAVEN_HOME/bin

保存,使配置文件生效并查看mavn是否配置成功

source /etc/profile
mvn -version

配置镜像

vim /docker_data/jenkins/maven/apache-maven-3.8.1/conf/settings.xml

替换掉整个mirrors

<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>aliyunmaven</id>
            <mirrorOf>*</mirrorOf>
            <name>阿里云公共仓库</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </mirror>
        <!--        spring-->
        <mirror>
            <id>aliyunmaven</id>
            <mirrorOf>*</mirrorOf>
            <name>阿里云公共仓库</name>
            <url>https://maven.aliyun.com/repository/spring</url>
        </mirror>
        <!--        spring-plugin-->
        <mirror>
            <id>aliyunmaven</id>
            <mirrorOf>*</mirrorOf>
            <name>阿里云公共仓库</name>
            <url>https://maven.aliyun.com/repository/spring-plugin</url>
        </mirror>
        <!--        google-->
        <mirror>
            <id>aliyunmaven</id>
            <mirrorOf>*</mirrorOf>
            <name>阿里云公共仓库</name>
            <url>https://maven.aliyun.com/repository/google</url>
        </mirror>
        <!--        grails-core-->
        <mirror>
            <id>aliyunmaven</id>
            <mirrorOf>*</mirrorOf>
            <name>阿里云公共仓库</name>
            <url>https://maven.aliyun.com/repository/grails-core</url>
        </mirror>
        <!--        apache snapshots-->
        <mirror>
            <id>aliyunmaven</id>
            <mirrorOf>*</mirrorOf>
            <name>阿里云公共仓库</name>
            <url>https://maven.aliyun.com/repository/apache-snapshots</url>
        </mirror>
  </mirrors>

宿主机安装git

《Git压缩包tar.gz下载》
docke+gitlab+jenkins+springboot
git相关依赖

yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
yum -y install gcc-c++ perl-ExtUtils-MakeMaker

上传到linux服务器的usr/loacl/目录下解压

tar -zxvf /usr/local/v2.39.0.tar.gz -C /usr/local/

安装git

cd /usr/local/git-2.39.0/
make prefix=/usr/local/git-2.39 all
make prefix=/usr/local/git-2.39 install

配置环境变量

vim /etc/profile

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL追加内容

# git
export GIT_HOME=/usr/local/git-2.39
export PATH=$GIT_HOME/bin:$PATH

保存,使配置文件生效并查看git是否安装成功

source /etc/profile
git -v

安装jenkins

开放防护墙端口和创建宿主机文件夹

firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload
mkdir -p /docker_data/jenkins/home

-u 0表示使用超级管理员启动

docker run -u 0 -d -p 8080:8080 -p 50000:50000 --privileged=true --restart=always -v /usr/local/java/jdk1.8.0_341:/usr/local/jdk -v /docker_data/jenkins/maven/apache-maven-3.8.1:/usr/local/maven -v /usr/local/git-2.39/bin/git:/usr/local/git -v /docker_data/jenkins/home:/var/jenkins_home -v /etc/localtime:/etc/localtime -e JAVA_OPTS="-server -Xms2048m -Xmx2048m" --name jenkins-jdk17 jenkins/jenkins:jdk17

查看jenkins初始密码

[root@node1 ~]# docker exec -it jenkins-jdk17 bash
root@10203976ba44:/# cat /var/jenkins_home/secrets/initialAdminPassword
e7944399aba54dcdb43488fc2feb5ad2

ip:8080访问jenkins可视化界面,输入密码登录,选择安装推荐的插件。
账号:root
密码:123456
docke+gitlab+jenkins+springboot
Jenkins URL:http://192.168.1.11:8080/(默认即可,一般jenkins会专门用一台服务器
docke+gitlab+jenkins+springboot

全局工具配置

配置jdk
docke+gitlab+jenkins+springboot
配置Git
docke+gitlab+jenkins+springboot
配置Maven
docke+gitlab+jenkins+springboot

管理插件

docke+gitlab+jenkins+springboot
安装Localization: Chinese (Simplified)、Maven Integration plugin、Publish Over SSH三个插件
docke+gitlab+jenkins+springboot

系统配置

docke+gitlab+jenkins+springboot
docke+gitlab+jenkins+springboot
拉到最底下的 Publish over SSH配置部署服务器,就是运行项目的服务器
docke+gitlab+jenkins+springboot
部署服务器创建部署文件夹

mkdir /jenkins

构建一个maven项目

docke+gitlab+jenkins+springboot
docke+gitlab+jenkins+springboot
去Gitlab拷贝代码地址
docke+gitlab+jenkins+springboot
回到jenkins粘贴复制的地址
docke+gitlab+jenkins+springboot
解决 remote: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2FA enabled and you must use a personal access token instead of a password. 错误
docke+gitlab+jenkins+springboot
docke+gitlab+jenkins+springboot
docke+gitlab+jenkins+springboot
查看Gitlab的主分支名称,有的是master有的是main
docke+gitlab+jenkins+springboot

最后保存。
docke+gitlab+jenkins+springboot
报错就看控制台输出的错误信息去百度
docke+gitlab+jenkins+springboot

部署服务器手动启动jar包(防火墙记得开放相应端口,避免无法访问

[root@node2 /]# cd /docker_data/jenkins/home/workspace/first/target/
[root@node2 target]# java -jar gitlabdemo-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.6)

2022-12-14 08:12:22.723  INFO 41645 --- [           main] com.fu.gitlabdemo.GitlabdemoApplication  : Starting GitlabdemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_341 on node2 with PID 41645 (/docker_data/jenkins/home/workspace/first/target/gitlabdemo-0.0.1-SNAPSHOT.jar started by root in /docker_data/jenkins/home/workspace/first/target)
2022-12-14 08:12:22.726  INFO 41645 --- [           main] com.fu.gitlabdemo.GitlabdemoApplication  : No active profile set, falling back to 1 default profile: "default"
2022-12-14 08:12:23.926  INFO 41645 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9090 (http)
2022-12-14 08:12:23.951  INFO 41645 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-12-14 08:12:23.951  INFO 41645 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.69]
2022-12-14 08:12:24.239  INFO 41645 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-12-14 08:12:24.239  INFO 41645 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1448 ms
2022-12-14 08:12:24.772  INFO 41645 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9090 (http) with context path ''
2022-12-14 08:12:24.786  INFO 41645 --- [           main] com.fu.gitlabdemo.GitlabdemoApplication  : Started GitlabdemoApplication in 2.517 seconds (JVM running for 2.991)

springboot测试代码
启动类

@RestController
@SpringBootApplication
public class GitlabdemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(GitlabdemoApplication.class, args);
    }

    @GetMapping("hello")
    public String hello(){
        return "hello world!";
    }
}

yml配置server.port: 9090
pom.xml

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

浏览器访问192.168.56.12:9090/hello
docke+gitlab+jenkins+springboot