在GitLab上设置phpstan pipeline

81次阅读
没有评论

问题描述

在GitLab上尝试根据指南设置phpstan pipeline,但是遇到了问题,pipeline根本无法工作(Command ‘sh’ is not defined),他不知道如何解决这个问题。
以下是用户的.gitlab-ci.yml文件:

stages:
  - check
checkphpstan:
  stage: check
  image: ghcr.io/phpstan/phpstan
  script:
    - analyse --no-progress --error-format gitlab > phpstan.json

以下是pipeline的输出:

Running with gitlab-runner 15.2.0~beta.17.g34ae4a68 (34ae4a68)  on blue-5.shared.runners-manager.gitlab.com/default -AzERasQ
Preparing the "docker+machine" executor
Using Docker executor with image ghcr.io/phpstan/phpstan ...
Pulling docker image ghcr.io/phpstan/phpstan ...
Using docker image sha256:797d91431d4ecb9c7c570d793db215dec2ae01f942b85e4e6e7cf4e07d07c8f2 for ghcr.io/phpstan/phpstan with digest ghcr.io/phpstan/phpstan@sha256:ac693ee977b314976226205631cd9071364f6c84b3b113f0c9404f9d4747a0b5 ...
Preparing environment
00:01
Running on runner--azerasq-project-37231833-concurrent-0 via runner-azerasq-shared-1658326011-484bb33b...
Getting source from Git repository
00:03
$ eval "$CI_PRE_CLONE_SCRIPT"
Fetching changes with git depth set to 20...
Initialized empty Git repository in /builds/balikobot/BalikobotAdmin/.git/
Created fresh repository.
Checking out e4512b17 as feature/ci-pipeline...
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:01
Using docker image sha256:797d91431d4ecb9c7c570d793db215dec2ae01f942b85e4e6e7cf4e07d07c8f2 for ghcr.io/phpstan/phpstan with digest ghcr.io/phpstan/phpstan@sha256:ac693ee977b314976226205631cd9071364f6c84b3b113f0c9404f9d4747a0b5 ...
Command "sh" is not defined.
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

问题的原因是官方的phpstan Docker镜像使用了ENTRYPOINT [“phpstan”],而GitLab runner使用”sh -c”命令调用镜像。你可以覆盖默认的入口点(https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#overriding-the-entrypoint-of-an-image):

phpstan:
  stage: check
  image:
    name: ghcr.io/phpstan/phpstan
    entrypoint: [""]
  script:
    - phpstan analyse

我建议使用具体版本的phpstan Docker镜像(不要使用latest标签)。

方案2

使用脚本或工具来管理容器的启动顺序可能会增加复杂性,并且需要确保容器A和容器B之间的依赖关系正确设置。
另一种方法是编写脚本或使用工具来控制容器的运行顺序。你可以使用docker run命令来手动控制容器的启动顺序,或者使用一些第三方工具来管理容器的依赖关系。

示例:

以下是一个简单的bash脚本示例,可以在容器A启动后启动容器B:

#!/bin/bash
# 启动容器A
docker run -d --name container_a your_image_a
# 等待容器A完全启动
while ! docker exec container_a echo "Container A is ready"; do
  sleep 1
done
# 启动容器B
docker run -d --name container_b your_image_b

在这个示例中,我们首先使用docker run命令启动容器A,并将其命名为container_a。然后,使用一个循环来等待容器A完全启动(这里是通过在容器内运行echo命令来测试)。一旦容器A就绪,我们再使用docker run命令启动容器B,并将其命名为container_b。

正文完