在GitLab CI/CD中无法执行脚本

后端开发 2026-07-09

因为有些脚本比较复杂,我更愿意创建外部Bash脚本,然后在 gitlab-ci.yml 中使用它。

我有一个小脚本,可以列出所有已编译的脚本,并用它们来打包一个发布版本。 只是,当我尝试执行它(使用 ./release_bin 时),我得到了这个错误:

2026-04-26T08:22:56.104620Z 01O $ ./release_bin
2026-04-26T08:22:56.104946Z 01E /bin/sh: eval: line 180: ./release_bin: not found

我也不明白的是:

  • 当我运行 ls . 时,文件看起来是正确的:

none $ ls . LICENSE README.md bin build_all build_only cmd flags go.mod go.sum internal main.go release_bin * 当我修改它的属性(使用 chmod +x release_bin 时),没有任何错误。 * 在另一个作业中,一切正常:

none $ chmod +x build_all $ ./build_all Building to ./bin/zen-addons_aix_ppc64 with GCO...

这是我的 gitlab-ci.yml

# Copyright (C) 2026 Eliott Takvorian
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

stages:
    - build
    - release

compile:
    image: golang:latest
    stage: build
    script:
        - chmod +x build_all
        - chmod +x build_only
        - ./build_all
        - ls ./bin
    artifacts:
        paths:
            - bin/
    rules:
        - if: $CI_COMMIT_TAG

release:
    stage: release
    image: registry.gitlab.com/gitlab-org/cli:latest
    rules:
        - if: $CI_COMMIT_TAG
    script:
        - ls ./bin
        - ls .
        - chmod +x release_bin
        - ./release_bin

以及 release_bin

#!/bin/bash

for f in "./bin/*"
do
    BINFILES="$BINFILES $f"
done

echo $BINFILES

glab auth login --hostname "$CI_SERVER_HOST" --token "$GLAB_TOKEN"

glab release create "$CI_COMMIT_TAG" --name "Release $CI_COMMIT_TAG" \
    -N "$CI_COMMIT_TAG_MESSAGE" $BINFILES

如果你认为我应该再添加一个文件,请在评论中说明,我会修改问题。

解决方案

我最终把

#!/bin/bash

替换为

#!/bin/sh

并且它工作正常。

我也不知道为什么,镜像 golang:latest 支持Bash,而 registry.gitlab.com/gitlab-org/cli:latest 不支持。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章