gitlab - 用于 CI 作业完成后未执行的所有存储库的 GitLab 全局接收后服务器挂钩
问题描述
语境
在设置了 GitLab 服务器、GitLab 运行器 CI 并创建了一个成功完成 CI 作业的测试存储库之后,我在确保post-receive
在测试存储库提交上成功完成 CI 后完成脚本方面遇到了一些困难。
MWE
MWE 执行完整的部署,并将存储库上传到 GitLab 服务器并在存储库上运行 CI。但是,它还没有很好地推广,因此它(至少)具有以下要求:系统:Ubuntu 20.04,架构:AMD64。
git clone git@github.com:Deployment-Oneliners/Self-host-GitLab-Server-and-Runner-CI.git
cd Self-host-GitLab-Server-and-Runner-CI
git checkout post-receive
rm -r test/libs/*
chmod +x install-bats-libs.sh
./install-bats-libs.sh
./install_gitlab.sh -s -r
./test.sh
./test/libs/bats/bin/bats test/test_post_receive.bats
人们会期望这项post-receive
工作会在/home/<your ubuntu username>/Desktop/helloworld.txt
. (我认为它没有,因为post-receive
脚本没有运行。)
要完全卸载 MWE,可以运行:./uninstall_gitlab.sh -y -h -r
.
问题
- 文件说
- 从源代码安装通常是 /home/git/gitlab-shell/hooks。
- 对于 Omnibus GitLab 安装通常是 /opt/gitlab/embedded/service/gitlab-shell/hooks。
但是,对于使用的部署脚本,这是/home/name/gitlab/data/gitlab-shell
相反的。因此,我假设这/home/name/gitlab/data/gitlab-shell/hooks
是默认的钩子目录。
我通过创建一个post-receive/post-receive
目录和脚本来测试这个假设,确保它们归gitlab-runner
用户所有,并且可以运行,并进行新的提交以使 CI 再次完成。post-receive
GitLab 未执行该脚本。
接下来,我登录到 docker 并gitlab-shell
使用以下命令找到了实际文件夹:
sudo docker ps -a
docker exec -t -i 0f17db17c212 /bin/bash
cd /opt/gitlab/embedded/service/gitlab-shell/
mkdir hooks
cd hooks
mkdir post-receive.d
cd post-receive.d
echo '#!/bin/bash' | tee post-receive
echo 'touch server_example_output.txt' | tee -a tee post-receive
chmod +x post-receive
并验证post-receive
脚本创建了一个文件,名为:example_output.txt
在与脚本相同的文件夹中(因为我无法从 docker 内部联系到 linux 用户)。我不知道如何使post-receive
所有者拥有,gitlab-runner
因为该chown
命令不起作用,因为它说gitlab-runner
找不到用户。我认为这是因为 docker 内的 root 帐户实际上是该gitlab-runner
帐户。post-receive
docker里面的脚本内容是:
#!/bin/bash
touch server_example_output.txt
并且它是使用手动验证的:./post-receive
它创建了example_output.txt
文件。但是,在新提交上运行 CI 不会导致创建该输出文件。
此外,我尝试post-receive
在 docker 内为特定存储库创建一个脚本。这很成功,我了解到脚本是从目录的根目录运行的,因此如果您编写touch output.txt
文件,则不会在 .h 中创建文件/hooks/output.txt
,而是在散列<long hash code>.git/
目录中创建。
问题
post-receive
在 GitLab 服务器中执行存储库提交后,如何确保执行 GitLab脚本?(GitLab 服务器中是否有一些 gui/按钮允许测试该post-receive
脚本是否被 GitLab 找到并可执行)?
解决方案
问题是我是双重的:
- 我在
post-receive
linux 用户内部创建了脚本,而不是在 docker 内部。 - 我在错误的位置寻找
post-receive
脚本的输出。我假设它会在与脚本所在的目录相同的目录中产生输出。但是post-receive
脚本是从目录的根目录调用的.git
,因此这是创建输出的地方。
最初我试图通过使用绝对路径来克服问题 2,但问题 1 暗示我选择的绝对路径实际上并不存在于 Docker 容器中。因此,没有创建输出,(我在 Linux 设备上寻找错误的输出位置,而不是在 Docker 容器中)。
我找到的手动解决方案是:首先使用 : 获取 Docker 容器 id sudo docker ps -a
,返回... ca3ba74eb832 ...
. 然后使用以下命令打开终端内的泊坞窗:
sudo docker exec -t -i ca3ba74eb832 /bin/bash
cd /opt/gitlab/embedded/service/gitlab-shell/
mkdir hooks
cd hooks
mkdir post-receive.d
cd post-receive.d
nano post-receive
然后输入:
#!/bin/bash
touch touch general_server_output.txt
并保存post-receive
文件。然后使其可运行:
chmod +x post-receive
不需要让用户post-receive
拥有gitlab-runner
,因为root
Docker 容器内的用户实际上是该用户,所以它已经被拥有gitlab-runner
(即使在 Docker 容器内,该用户被称为root
)。
推荐阅读
- django - 如何使用 django-query 以更动态的方式获取我的 api
- python-3.x - 读取文件目录,然后在 pygame 表面上显示它们
- typescript - 错误 - node_modules\@types\jasmine\index.d.ts(138,230) TS1005: ')' 预期
- php - 改造中经常超时
- tensorflow - 加速 rcnn 的量化感知训练异常
- python - 如何定期清除 celery rabbitmq 中的队列?
- swift - 如何在 Watchkit 应用程序 swift 上拥有交互式地图?
- python - 使用 matplotlib 在 x 轴上绘制时间
- sapui5 - SAPUI5 BusyIndicator 周围的边界线错误
- jenkins - jenkins 无法根据来自 bitbucket 云的拉取请求触发构建