3.Jenkins & Git

Jenkins & Git

GitLab是基于Git的集成软件开发平台。本地搭建GitLab服务器作为源码管理系统。
官网提供的最低硬件要求:4核 + 4GB RAM,能够支持大约500用户

查看虚拟机资源

cat /proc/cpuinfo | grep cores

free -h

Docker

Docker是一个容器工具,提供了轻量级的隔离兼容机制。我们会在之后的章节详细讲解Docker的原理,目前来说可以先将docker当作一个轻量级的虚拟机。

安装启动docker
# install docker by get-docker.sh
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh ./get-docker.sh

# enable docker service
sudo systemctl enable docker.service
sudo systemctl enable containerd.service

# manager docker as non-root user
docker run hello-world

sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker

docker run hello-world

安装运行GitLab 服务器

安装完成docker之后,我们使用docker 安装运行GitLab 服务器

export GITLAB_HOME=$HOME/gitlab

# Replace it with your own IP
export IP=10.0.0.215

sudo ufw disable

docker run --detach \
  --hostname gitlab.example.com \
  --publish ${IP}:443:443 \
  --publish ${IP}:80:80 \
  --name gitlab \
  --restart always \
  --volume $GITLAB_HOME/config:/etc/gitlab \
  --volume $GITLAB_HOME/logs:/var/log/gitlab \
  --volume $GITLAB_HOME/data:/var/opt/gitlab \
  --shm-size 256m \
  gitlab/gitlab-ce:latest

docker logs -f /gitlab
# docker rm -f /gitlab

获取root密码,登录

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

修改密码 User settings > Password

创建Git仓库

Group

View all Groups > New Group > Create group

  • 创建Jenkins group
  • Visibility Level 选择Private
  • 创建
Project

View all projects > New project > Create blank project

  • Project name: maven
  • project URL 选择jenkins
  • Visibility Level 选择Private
  • 取消 Initialize repository with a README
  • 创建
User

Admin > Users > New user

  • Name: ubuntu
  • Username: ubuntu
  • email: [email protected]
  • Password: 12345678
  • Access level: Regular
  • 创建

Admin > Project > jenkins / maven > maven project members > manage access

  • Invite members
    • username: ubuntu
    • role: maintainer
  • 点击invite

回到Jenkins / maven 项目,查看 Project information > members

退出,尝试登录ubuntu账户

将Java App上传到Git repo

依然使用之前的simple-java-maven-app.git

将代码下载到本地

mkdir workspace
cd workspace
git clone https://github.com/jenkins-docs/simple-java-maven-app.git

同样将Jenkins / maven 项目clone到本地,使用新建的ubuntu用户

git clone http://ubuntu:[email protected]/jenkins/maven.git

cp -r ../simple-java-maven-app/* maven/

git status
git add .
git commit -m "Add maven project"
git push origin main

查看Jenkins / maven 项目

Jenkins 集成 Git

登入Jenkins,选择项目maven-job,修改配置
填入Git repo

添加credential
Dashboard > Manage Jenkins > Manage Credentials > System > Global credentials (unrestricted) > Add credentials

  • Kind: Username with password
  • Username: ubuntu
  • Password: 12345678
  • ID: git_user
    创建

Source Code Management > Git

  • 输入
    • Repository URL: http://10.0.0.215/jenkins/maven.git
    • Credentials: ubuntu
    • Branch: */main
  • 保存,点击Build
  • 成功

你可能感兴趣的:(jenkins,git,运维)