首页 > 解决方案 > 用于 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.

问题

  1. 文件说

但是,对于使用的部署脚本,这是/home/name/gitlab/data/gitlab-shell相反的。因此,我假设这/home/name/gitlab/data/gitlab-shell/hooks是默认的钩子目录。

我通过创建一个post-receive/post-receive目录和脚本来测试这个假设,确保它们归gitlab-runner用户所有,并且可以运行,并进行新的提交以使 CI 再次完成。post-receiveGitLab 未执行该脚本。

接下来,我登录到 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-receivedocker里面的脚本内容是:

#!/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 找到并可执行)?

标签: gitlabgitlab-ci-runnergit-post-receive

解决方案


问题是我是双重的:

  1. 我在post-receivelinux 用户内部创建了脚本,而不是在 docker 内部。
  2. 我在错误的位置寻找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,因为rootDocker 容器内的用户实际上是该用户,所以它已经被拥有gitlab-runner(即使在 Docker 容器内,该用户被称为root)。


推荐阅读