gitlab-ci
来源:原创
时间:2022-06-12
作者:脚本小站
分类:Linux
添加runner
gitlab-runner下载地址:
http://gitlab-runner-downloads.s3.amazonaws.com/latest/index.html
gitlab没权限运行docker的解决方法:
usermod -aG docker gitlab-runner
在k8s上跑runner的示例:
https://help.aliyun.com/document_detail/106968.html
安装runner:
# Download the binary for your system sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64 # Give it permission to execute sudo chmod +x /usr/local/bin/gitlab-runner # Create a GitLab Runner user sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash # Install and run as a service sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner sudo gitlab-runner start
注册runner:
]# gitlab-runner register --url http://192.168.199.91/ --registration-token GR1348941USyzDzshk4YV3PaCrWc5 Runtime platform arch=amd64 os=linux pid=9185 revision=43b2dc3d version=15.4.0 Running in system-mode. Enter the GitLab instance URL (for example, https://gitlab.com/): [http://192.168.199.91/]: Enter the registration token: [GR1348941USyzDzshk4YV3PaCrWc5]: Enter a description for the runner: # 这个runner是干什么的 [test-runner]: Enter tags for the runner (comma-separated): # 标签,用来项目选择runner Enter optional maintenance note for the runner: Registering runner... succeeded runner=GR1348941USyzDzsh Enter an executor: docker, ssh, virtualbox, docker+machine, kubernetes, custom, docker-ssh, parallels, shell, docker-ssh+machine: shell Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"
runner配置文件:修改后需重启。
]# vim /etc/gitlab-runner/config.toml concurrent = 1 check_interval = 0 [session_server] session_timeout = 1800 [[runners]] name = "test-runner" url = "http://192.168.199.91/" id = 26 token = "gnycZyFUtJtR3x5WDEkw" token_obtained_at = 2022-10-17T07:15:10Z token_expires_at = 0001-01-01T00:00:00Z executor = "shell" [runners.custom_build_dir] [runners.cache] [runners.cache.s3] [runners.cache.gcs] [runners.cache.azure]
将gitlab-runner用户添加到docker组:
gpasswd -a gitlab-runner docker
gitlab-ci中的预定义变量:
docs.gitlab.cn/jh/ci/variables/predefined_variables.html
cache:
使用:定义的目录或文件会被保存下来,后面的job中可以引用这个文件或目录。
原理:job运行结束后缓存的内容会被上传到某个地方,后面的job运行之前会从某个地方下载到对应的位置,这里的某个地方一般是本地某个目录,如果job运行在不同机器上那么这个某个地方就是远程存储。
cache: # 全局缓存, paths: - my/files # 文件或目录会被保存,相对地址 rspec: script: test cache: # 局部缓存,会覆盖全局 paths: - binaries/
gitlab-ci.yml中的cache的使用:
zhuanlan.zhihu.com/p/106971627
错误合集:
1、Reinitialized existing Git repository
解决:
variables: GIT_STRATEGY: clone # 改成这个策略即可
参考:
stackoverflow.com/questions/64255647/how-to-skip-reinitialized-existing-git-repository-on-gitlab-cicd-stage
语法
官方文档:
docs.gitlab.cn/jh/ci/yaml
go语言示例:shell模式
variables: GIT_STRATEGY: clone # 使用clone模式 stages: - golangbuild - dockerbuild - deploytok8s cache: paths: # 定义全局缓存,要保留下面这两个文件 - src/videoweb - IMAGE.txt build: stage: golangbuild script: - echo "start build ..." - cd src - go build main.go - mv main videoweb - ls -al - pwd dockerbuild: stage: dockerbuild script: - echo "start docker build ..." - whoami - docker version - pwd - ls -al src - docker build -t zhuqiyang/videoweb:0.1-$CI_PIPELINE_ID . - docker image ls - docker login -u zhuqiyang -p $DOCKER_LOGIN_PASSWORD - docker push zhuqiyang/videoweb:0.1-$CI_PIPELINE_ID - echo "zhuqiyang/videoweb:0.1-$CI_PIPELINE_ID" > IMAGE.txt - cat IMAGE.txt deploytok8s: stage: deploytok8s script: - cat IMAGE.txt - export IMAGE_URL=`cat IMAGE.txt` - echo $IMAGE_URL - bash -x videoweb.sh $IMAGE_URL - cat videoweb.yaml - kubectl apply -f videoweb.yaml - kubectl get pods -l app=videoweb
java语言示例:docker模式
before_script: - echo "start pipline" image: docker:stable stages: - package - docker_build - deploy_k8s variables: KUBECONFIG: /etc/deploy/config MAVEN_OPTS: "-Dmaven.repo.local=/opt/cache/.m2/repository" REGISTRY_USERNAME: "hixxxxxxxxxx@aliyun.com" REGISTRY_PASSWORD: "" mvn_build_job: image: maven:3.6.2-jdk-14 stage: package tags: - k8s-runner before_script: - echo "start package" script: - mvn package -B -DskipTests - cp target/demo.war /opt/cache - ls -al /opt/cache - pwd after_script: - echo "package end" docker_build_job: image: docker:latest stage: docker_build tags: - k8s-runner script: - env - ls -al /opt/cache - docker login -u $REGISTRY_USERNAME -p $REGISTRY_PASSWORD registry.cn-beijing.aliyuncs.com - mkdir target - cp /opt/cache/demo.war target/demo.war - docker build -t registry.cn-beijing.aliyuncs.com/scriptjc/gitlabci-java-demo:$CI_PIPELINE_ID . - docker push registry.cn-beijing.aliyuncs.com/scriptjc/gitlabci-java-demo:$CI_PIPELINE_ID deploy_k8s_job: image: registry.cn-hangzhou.aliyuncs.com/haoshuwei24/kubectl:1.16.6 stage: deploy_k8s tags: - k8s-runner script: - mkdir -p /etc/deploy - echo $kube_config |base64 -d > $KUBECONFIG - sed -i "s/IMAGE_TAG/$CI_PIPELINE_ID/g" deployment.yaml - cat deployment.yaml - kubectl apply -f deployment.yaml
制品下载:
build: stage: buildgetiplocation script: - echo "start build." - ls -al - go build main.go - ./main artifacts: paths: - main # 要下载的制品