From this lesson you will learn about
1,How to install and configure a docker based gitlab server
2,How to install and configure a docker based seperated gitlab-runner
3,The basic usage of expect utility
4,How to trigger a script task when committing files to gitlab server
5,How to map a local folder to a container folder
#从本课程你将学会
1,怎样安装和配置一个基于docker的gitlab server 2,怎样安装和配置一个基于docker的单独的gitlab-runner 3,expect 工具的基本使用 4,怎样在提交文件时触发gitlab server的脚本任务 5,怎样映射本地文件夹到容器文件夹 The configuration directory of gitlab server(in container)(gitlab server在容器中的配置目录) /etc/gitlab #gitlab configuration directory(配置文件目录) /var/log/gitlab #gitlab log directory (日志目录 ) /var/opt/gitlab #gitlab data directory(数据目录) The gitlab server related ports:443,80,22
环境:CentOS7 ,Docker CE
1,Pull the gitlab image(拉取gitlab镜像)
sudo docker pull gitlab/gitlab-ce
2,
Prepare gitlab local foldders (准备gitlab本地文件夹)
sudo mkdir /home/gitlab
sudo chmod -Rf /home/gitlab
sudo cd /home/gitlab
sudo mkdir etcgitlab
sudo mkdir loggitlab
sudo mkdir optgitlab
3,Create a docker network (创建一个docker网络)
sudo docker network create --attachable --driver overlay my-net-git
#--attachable ,can be manually attached (可被手动附加)
#--overlay , can support Docker Swarm network (可支持Docker Swarm集群网络)
4,Run the gitlab server (运行gitlab server镜像)
sudo docker run -td -p : -p : -p : --name gitlab --restart always \ --network my-net-git -v /home/gitlab/etcgitlab:/etc/gitlab -v /home/gitlab/loggitlab:/var/log/gitlab -v /home/gitlab/optgitlab:/var/opt/gitlab gitlab/gitlab-ce
#--network my-net-git let the gitlab container join the network my-net-git(让容器加入到my-net-git网络)
#-p ,host port to container port mapping (主机到容器的端口映射)
5,Prepare gitlab-runner Dockerfile
sudo mkdir docker
sudo chmod docker
sudo vi docker/Dockerfile
Copy below content to the file
FROM centos:6.10 MAINTAINER Liping RUN bash -c "curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | bash" RUN bash -c "yum install -y expect.x86_64 expect-devel.x86_64" RUN bash -c "yum install -y gitlab-runner" ADD ./docker/my-init.sh /home/sh/my-init.sh RUN chmod /home/sh/my-init.sh CMD ["/home/sh/my-init.sh"]
6,Open your gitlab server url ,the first access will ask you to set a new password.
Navigate to http://localhost:9080/admin/runners
Find the Server URL and to Token for Runner Registration.
打开gitlab server url,首次访问时会要求设置一个新密码。导航到http://localhost:9080/admin/runners
查找图所示 的url 和token 用于Runner 注册.
7,Prepare the Runner Registration script (准备Runner的注册脚本)
sudo vi docker/my-init.sh
Copy content below to the file replace the red font portion to your own data(复制如下内容至文件,使用你自己第7步中查到的数据,替换红色字体部分)
#!/usr/bin/expect #the gitlab runner init command(gitlab runner 的初始化命令) spawn gitlab-runner register --locked=false expect "*gitlab.com/):" #input the gitlab server url ,replace with your real gitlab server url(输入gitlab 服务>器地址,使用你实际的gitlab server地址代替) send "http://localhost:9080/\r" expect "*token for this runner:" #replace with your real token send "FSrjiy8_9oDbst2-xX-S\r" expect "*description for this runner:*" send "myrunner\r" expect "*tags for this runner (comma separated):*" send " \r" expect "*shell,*" send "shell\r" exec bash -c "service gitlab-runner restart" exec bash -c "read"
8,
sudo docker build -t lipingsg/mygitrunner -f docker/Dockerfile .
9,Start the gitlab-runner container (启动gitlab-runner容器)
sudo docker run -td --name my_gitrunner --network my-net-git lipingsg/mygitrunner
10,Go to the gitlab server url http://localhost:9080/admin/runners , verify the registration was done.
导航至http://localhost:9080/admin/runners 验证注册是否已完成。
11,Go to http://localhost:9080/admin/application_settings/ci_cd
Uncheck “Default to Auto DevOps pipeline for all projects”and save.
进入ci_cd 页面 取消“Default to Auto DevOps pipeline for all projects”并保存
12,Create a new Project in gitlab ,and create a .gitlab-ci.yml to test the trigger fuction for a script task.(创建一个新工程,在工程中创建一个 .gitlab-ci.yml 文件 用来测试自动触发脚本任务是否完成) .gitlab-ci.yml的内容如下
stages:
- test
- build
- deploy
job 1:
stage: test
script:
- echo "$(date +"%Y%m%d%H%M%S")" job 1 done
job 2:
stage: build
script:
- echo "$(date +"%Y%m%d%H%M%S")" job 2 done
job 3:
stage: deploy
script:
- echo "$(date +"%Y%m%d%H%M%S")" job 3 done
13,The gitlab server DEVOPS environment is working.
Gitlab 服务器的DEVOPS功能已经完成,如果有任何疑问请在下面提出~
原创内容,版权归属千分网络科技,转载需经许可,并附带原始链接,谢谢!