1、在Jenkins容器中生成SSH密钥:
ssh-keygen -t rsa -C"$your_email"
cat ~/.ssh/id_rsa.pub2、用户浏览器登录gitlab,将id_rsa.pub的内容添加到Profile Settings –> SSH Keys –> Add SSH Keys中
3、在gitlab console中创建新的项目,记录访问URL
4、用jenkins用户进入Jenkins容器,clone gitlab项目到本地存储,并merge本地代码到git lab:
cd /var/jenkins_home/code/ms
git config --global user.name "Administrator"
git config --global user.email "admin@example.com"
git clone git@172.17.0.2:microService/ms.git
cd /var/jenkins_home/code/ms
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
docker cp spring-cloud-event-sourcing-example-master jenkins:/var/jenkins_home/code/ms --在宿主机root用户
git add spring-cloud-event-sourcing-example-master
git commit -m "spring-cloud-event-sourcing-example-master"
git push -u origin master
注意:无论是安装maven还是从gitlab clone代码,只能用jenkins用户,不可用root
注意:Jenkins窗口的主目录,应映射至宿主机的共享目录:
mkdir -p /home/linyu/jenkins/data
chmod /home/linyu/jenkins/data
docker run --name jenkins -d -p 8080:8080 -p 50000:50000 -v /home/linyu/jenkins/data:/var/jenkins_home jenkins
注意:如jenkins容器中没有maven或不能用,自己安装:
docker cp apache-maven-3.5.0-bin.tar.gz jenkins:/var/jenkins_home --在宿主机root用户
以下为jenkins用户:
cd /var/jenkins_home
tar -zvxf apache-maven-3.5.0-bin.tar.gz
export M2_HOME=/var/jenkins_home/apache-maven-3.5.0
export PATH=$PATH:$M2_HOME/bin
echo "export M2_HOME=/var/jenkins_home/maven" >> ~/.bashrc
echo "export M2_HOME=/var/jenkins_home/maven" >> ~/.mavenrc
echo "export M2_HOME=/var/jenkins_home/maven" >> /etc/environment
echo "export M2_HOME=/var/jenkins_home/maven" >> /etc/profile
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64