先简单介绍一下gitlab-runner
GitLab Runner 是一个与 GitLab CI/CD 一起使用以在管道中运行作业的应用程序。
您可以选择在您拥有或管理的基础设施上安装GitLab Runner 应用程序。如果这样做,出于安全和性能原因,您应该将 GitLab Runner 安装在与托管 GitLab 实例的机器不同的机器上。当您使用不同的机器时,您可以在每台机器上拥有不同的操作系统和工具,例如 Kubernetes 或 Docker。
GitLab Runner 是开源的,用Go编写。它可以作为单个二进制文件运行;不需要特定语言的要求。
您可以在几个不同的受支持操作系统上安装 GitLab Runner。其他操作系统也可以工作,只要您可以在它们上编译 Go 二进制文件。
GitLab Runner 也可以在 Docker 容器内运行或部署到 Kubernetes 集群中
这里可以docker安装也可以裸机部署,为了方便,我这里选择docker部署
第一步,安装gitlab,这里有三种方式可以安装,我这里选择docker engine
docker run --detach --hostname localhost --publish 443:443 --publish 80:80 --publish 222:22 --name gitlab --restart always --volume /giblab/config:/etc/gitlab --volume /giblab/logs:/var/log/gitlab --volume /giblab/data:/var/opt/gitlab --shm-size 256m gitlab/gitlab-ee:latest
确保gitlab正常启动
查看gitlab默认密码
[root@node1 config]# docker exec -it gitlab cat /etc/gitlab/initial_root_password # WARNING: This value is valid only in the following conditions # 1. If provided manually (either via `GITLAB_ROOT_PASSWORD` environment variable or via `gitlab_rails['initial_root_password']` setting in `gitlab.rb`, it was provided before database was seeded for the first time (usually, the first reconfigure run). # 2. Password hasn't been changed manually, either via UI or via command line. # # If the password shown here doesn't work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password. Password: 7NMXACjqBTUQBJtFuVHnrGDeddAQJ9hbC5zUAbIcSjY= # NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.
登录浏览器登录gitlab
默认用户名:root
第二步,新建gitlab项目
第三步,查看注册gitlab-runner的token,看你是想注册项目的runner,组的runner还是共享的runner,如果是项目runner或者是组的runner,那么权限有限,只能按照项目或者组去运行,如果share共享类型的runnner,那么可以执行任何项目,我这里注册一个share类型的runner
第四步,安装gitlab-runner,这里安装gitlab-runner也可以裸机安装,也可以docker安装,为了更好实现cicd,我使用的是裸机安装,点击下面的按钮可以查看如何裸机安装gitlab-runner
一通操作之后查看gitlab-runner是否正常启动
第五步,注册runner
[root@node1 ~]# gitlab-runner register --url http://localhost/ --registration-token ivvoCJfBTZ56BFuiMMtC Runtime platform arch=amd64 os=linux pid=14276 revision=c6bb62f6 version=14.10.0 Running in system-mode. Enter the GitLab instance URL (for example, https://gitlab.com/): [http://localhost/]: Enter the registration token: [ivvoCJfBTZ56BFuiMMtC]: Enter a description for the runner: [node1]: share-runner Enter tags for the runner (comma-separated): tag Enter optional maintenance note for the runner: shell Registering runner... succeeded runner=ivvoCJfB Enter an executor: custom, ssh, docker+machine, docker-ssh+machine, kubernetes, docker, docker-ssh, parallels, shell, virtualbox: shell Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
查看gitlab,是否有注册成功的runner,可以看到,已经有runner了,并且是share类型的
把下面这个勾上,否则执行不了
第六步,编写.gitlab-ci.yml文件,这个是默认的文件名字,不要修改文件名
内容格式如下:
stages: # List of stages for jobs, and their order of execution - build - test - deploy build-job: # This job runs in the build stage, which runs first. stage: build script: - echo "Compiling the code..." - echo "Compile complete." unit-test-job: # This job runs in the test stage. stage: test # It only starts when the job in the build stage completes successfully. script: - echo "Running unit tests... This will take about 60 seconds." - sleep 60 - echo "Code coverage is 90%" lint-test-job: # This job also runs in the test stage. stage: test # It can run at the same time as unit-test-job (in parallel). script: - echo "Linting code... This will take about 10 seconds." - sleep 10 - echo "No lint issues found." deploy-job: # This job runs in the deploy stage. stage: deploy # It only runs when *both* jobs in the test stage complete successfully. script: - echo "Deploying application..." - echo "Application successfully deployed."
执行后结果如下: