待更新…
待更新…
version: '3'
services:
gitlab:
container_name: 'gitlab'
image: gitlab/gitlab-ce:14.2.1-ce.0
hostname: 'gitlab'
restart: always
environment:
TZ: 'Asia/Shanghai'
GITLAB_ROOT_PASSWORD: 'gitlab_pwd123' # root 账号的密码
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://localhost:80' # http协议所使用的访问地址,不加端口默认80
gitlab_rails['gitlab_shell_ssh_port'] = 2222 # 此端口是run时22端口映射的2224端口
ports:
- '80:80'
- '4443:443'
- '2222:22'
volumes:
- './config:/etc/gitlab:rw'
- './logs/:/var/log/gitlab:rw'
- './data:/var/opt/gitlab:rw'
networks:
- gitlab
networks:
gitlab:
在当前目录下执行命令
docker-compose up -d
gitlab-rails console -e production
user =User.where(id:1).first
user.password = 'gitlab_pwd123'
user.password_confirmation = 'gitlab_pwd123'
user.save!
version: '3'
services:
gitlab-runner:
container_name: 'gitlab-runner'
image: gitlab/gitlab-runner:ubuntu-v14.2.0
hostname: 'gitlab-runner'
restart: always
volumes:
- ./config:/etc/gitlab-runner
- /var/run/docker.sock:/var/run/docker.sock # 将宿主机的docker挂载在runner,这样就可以在runner里面执行docker命令
- /bin/docker:/bin/docker # 将宿主机的docker挂载在runner,这样就可以在runner里面执行docker命令
- /usr/bin/com.docker.cli:/usr/bin/com.docker.cli # 将宿主机的docker挂载在runner,这样就可以在runner里面执行docker命令
networks:
- gitlab-runner
networks:
gitlab-runner:
在当前目录下执行命令
docker-compose up -d
gitlab-runner register
stages:
- test # 执行单元测试
- build # 执行构建
- deploy # 执行发布
Test:
stage: test
script:
- echo "$(date +"%Y%m%d%H%M%S")" Test Done # 模拟单元测试脚本
Build:
stage: build
script:
- echo "$(date +"%Y%m%d%H%M%S")" Build Done # 模拟构建脚本
Deploy:
stage: deploy
script:
- echo "$(date +"%Y%m%d%H%M%S")" Deploy Done # 模拟发布脚本
sudo chmod 777 /var/run/docker.sock
sudo useradd gitlab-runner
sudo gpasswd -a gitlab-runner docker
cat /etc/group
在 docker 组中能查看到 gitlab-runner 则表示添加成功
version: '3'
services:
registry:
image: registry:2.7.1
container_name: registry
volumes:
- ./var/lib/registry:/var/lib/registry
ports:
- 5000:5000
networks:
- registry
networks:
registry:
在当前目录下执行命令
docker-compose up -d
docker tag registry:2.7.1 localhost:5000/registry:2.7.1
docker push localhost:5000/registry:2.7.1
将runner的执行者修改为 docker,并配置runner.docker,并重启 gitlab-ci runner 容器
concurrent = 1
check_interval = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "hello runner"
url = "http://gitlab.local.com/"
token = "Je87UgYb7HjNjh9Y2xvZ"
executor = "docker"
[runners.custom_build_dir]
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
[runners.cache.azure]
[runners.docker]
tls_verify = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
shm_size = 0
image = "localhost:5000/golang:1.17.0"
privileged = true
volumes = ["/var/run/docker.sock:/var/run/docker.sock","/bin/docker:/bin/docker","/usr/bin/com.docker.cli:/usr/bin/com.docker.cli", "/cache"]
package main
import (
"fmt"
"time"
)
func main() {
for {
fmt.Printf("%v Say Hello!\n", time.Now())
time.Sleep(time.Second)
}
}
func Sum(a, b int) int {
return a + b
}
package main
import "testing"
func TestSum(t *testing.T) {
a := 1
b := 2
exp := 3
act := Sum(a, b)
if exp != act {
t.Errorf("Not Equal. exp:%d,act:%d", exp, act)
}
}
module gitlab.local.com/test/hello
go 1.17
FROM golang:1.17.0
WORKDIR /src
COPY . .
RUN go build -v . && ls
CMD ["./hello"]
stages:
- lint # 执行代码风格检查
- test # 执行单元测试
- build # 执行构建
- deploy # 执行发布
Lint:
image: localhost:5000/golangci/golangci-lint:latest # 依赖的镜像
stage: lint
allow_failure: true # 允许失败继续
script:
- echo "Lint begin"
- go version
- golangci-lint run -v # go 代码风格检查
- echo "Lint end"
Test:
image: localhost:5000/golang:1.17.0 # 依赖的镜像
stage: test
script:
- echo "Test begin"
- go version
- go test -race -mod=vendor -v -coverprofile .testCoverage.out ./... # go 单元测试命令并生成覆盖率文件
- echo "Test end"
Build:
stage: build
script:
- echo "Build begin"
- docker build -t localhost:5000/hello:v0.0.1 -f ./Dockerfile . # 使用dockerfile构建镜像
- docker push localhost:5000/hello:v0.0.1 # 推送镜像到本地镜像仓库
- echo "Build end" # 镜像构建完成
Deploy:
stage: deploy
script:
- echo "$(date +"%Y%m%d%H%M%S")" Deploy Done # 模拟发布脚本
推送代码到仓库后,登录gitlab可以看到已经触发了runner
docker pull localhost:5000/hello:v0.0.1
能正常拉取镜像则说明推送仓库成功
docker run localhost:5000/hello:v0.0.1
容器成功启动,并执行 Say Hello ,则表示镜像是正确的
未完待续…