拉取鏡像 啟動容器 暴露出來 9980和9922端口
docker pull gitlab/gitlab-ce:latest
mkdir -P /data/gitlab-test/etc /data/local/gitlab-test/log /data/local/gitlab-test/opt
docker run \
-itd \
-p 9980:80 \
-p 9922:22 \
-v /data/gitlab-test/etc:/etc/gitlab \
-v /data/local/gitlab-test/log:/var/log/gitlab \
-v /data/local/gitlab-test/opt:/var/opt/gitlab \
--restart always \
--privileged=true \
--name gitlab-test \
gitlab/gitlab-ce
進入容器修改基本配置 字面意思看描述修改
docker exec -it gitlab-test /bin/bash
production: &base
gitlab:
## Web server settings (note: host is the FQDN, do not include http://)
host: xx.xx.xx.xx # 你的主機IP
port: 9980
https: false
ssh_host: xx.xx.xx.xx # 你的主機IP
# If your ssh user differs from the system user, you need to specify it here
# Set it to an empty string to omit the username from any ssh url altogether
ssh_user:
gitlab_shell:
# If you use non-standard ssh port you need to specify it
ssh_port: 9922
讓配置生效
gitlab-ctl reconfigure
gitlab-ctl restart
# 同時修改密碼
gitlab-rails console
user = User.where(id: 1).first
或者
user = User.find_by(email: 'admin@example.com')
user.password = '你的密碼'
user.password_confirmation = '你的密碼'
user.save
登錄之后默認會有個Monitoring 項目 可以開始配置gitlab Runners,進行gitlab cicd 的自動構建
首先在設置里面找到注冊runner的url 和 token:如下圖所示
我選擇的是在docker里面啟動runnser服務 官方地址:https://docs.gitlab.com/runner/install/docker.html
可以有2種方式一種是使用本地的存儲卷,一種是使用docker存儲卷 我使用本地存儲
https://docs.gitlab.com/runner/install/docker.html
docker run -d --name gitlab-runner --restart always \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:latest
### 在 macOS, 使用 /Users/Shared 替換 /srv
### 注冊runner https://docs.gitlab.com/runner/register/index.html#docker
docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register
### 注冊的時候會選擇執行器,可以根據需要選擇 我選擇了shell 和 docker,一次注冊只能選擇一個執行器
### 根據選項一步一步注冊就好了 tag 和 描述要寫好,在后面的.git-ci.yam 配置里面會用到
配置 ci/cd 描述文件
default:
tags:
- docker
before_script:
- export PACKAGE_NAME=${CI_PROJECT_NAME}_${CI_COMMIT_SHA:0:8}.zip
stages: # List of stages for jobs, and their order of execution
- build
build-job: # This job runs in the build stage, which runs first.
stage: build
script:
- echo "Compiling the code..."
- echo "Compile complete."
- go build main.go
artifacts:
expire_in: 1 week
when: on_success
paths:
- main
# archive_package:
# stage: package
# script:
# - zip -r ${PACKAGE_NAME} main
# artifacts:
# expire_in: 1 week
# when: on_success
# untracked: true
# paths:
# - ${PACKAGE_NAME}
# tags:
# - docker
# only:
# - tags
在cd/cd Pipeline 菜單里面執行作業:
通過 artifacts 關鍵字可以生成構建物,在倉庫的tag 里面可以進行下載
具體的 .gitlab-ci.yml 文件語法可以參考官方文檔
https://docs.gitlab.com/ee/ci/yaml/
構建結果
注意事項
- gitlab 和 runner 的版本要一致,否則可能出現構建記錄無法出現的錯誤
- 可以通過 docker logs id 查看具體的日志