hgraca/ci-cd-composer-update

一个CI/CD脚本的库,用于运行`composer update`,如果管道为绿色,则打开一个MR并将其合并。

v1.2.0 2023-04-25 19:18 UTC

This package is auto-updated.

Last update: 2024-09-25 23:03:58 UTC


README

Author Software License GitLab tag (latest by SemVer)

一个CI/CD脚本的库,用于运行composer update,如果管道为绿色,则打开一个MR并将其合并。

目前,仅支持GitLab,但如有需要,将添加更多CI/CD提供程序。

如果您需要其他CI/CD提供程序,请随时打开一个MR。

为什么需要这个?

有两个非常好的依赖更新工具

不幸的是,它们与PHP配合得不好,它们提供了很多功能(例如,每个需要更新的包都有一个MR)并且需要很多信息,因此它们试图复制composer做的所有事情,但并没有完全成功。

例如,RenovateBot只有在您使用最新版本的PHP运行项目时才能正常工作,一旦发布新版本并且包更改以符合该版本,它将安装适用于新版本的PHP但不在您较旧版本中工作的包。

这是因为它没有考虑所有依赖项的PHP版本要求来确定要安装的包版本。

如何使用

首先将此库导入到您的项目中

composer require --dev hgraca/ci-cd-composer-update

GitLab

1. 在您的GitLab配置中包含此包

此包定义了一个GitLab yaml文件,您需要将其包含在项目GitLab yaml配置中,例如

# .gitlab-ci.yml
include:
  - remote: 'https://gitlab.com/hgraca/ci-cd-composer-update/raw/v1.0.0/src/GitLab/update_PHP_deps.yml'

包含类型必须是remote,因为当您的项目管道开始时,composer install尚未运行,因此将无法找到本地文件以包含在内。

此文件提供了一个禁用(称为“抽象”)作业,名为.update_PHP_deps

2. 创建您的更新作业

然后您可以创建您的更新作业,扩展此包提供的作业(.update_PHP_deps),提供一些变量,它应该在哪个阶段运行,并可能提供before_script来安装依赖项(gitjqcurl)。

GITLAB_ACCESS_TOKEN中使用的访问令牌必须是个人访问令牌或组访问令牌。项目访问令牌将不起作用,因为它将缺少API和Docker注册表的权限。

例如

update_PHP_deps:
  extends: .update_PHP_deps
  variables:
    UPDATE_BRANCH_PREFIX: "update_PHP_deps_" # Used for the update branch name, it will be followed by the datetime
    GIT_USER: "Maintainer Bot" # Used for the update commit
    GIT_EMAIL: "maintainer.bot@gmail.com" # Used for the update commit
    GITLAB_USERNAME: "my_username" # Used for pushing the new branch and opening the MR
    GITLAB_ACCESS_TOKEN: "personal access token with 'api' and 'write repository' permissions" # Used for pushing the new branch and opening the MR
    MERGE_IF_SUCCESSFUL: "true" # Merge automatically if the pipeline succeeds
    SECONDS_BETWEEN_POOLING: 10 # Nbr of seconds between checking if the MR pipeline is successful, so then it will merge
  stage: update_dependencies
  before_script: # `git`, `jq` and `curl` are needed, so if they are not already installed, they should be installed here
    - !reference [ default, before_script ] # Include any other `before_script` that might be defined
    - apk add --no-cache --update git jq curl && rm -rf /var/cache/apk/*
    - git config --global user.name "${GIT_USER}"
    - git config --global user.email "${GIT_EMAIL}"

通过查看src/GitLab/update_PHP_deps.yml,检查您可以覆盖或忽略的所有变量。

此作业不会在您的管道中运行,因为它具有仅当存在名为SCHEDULED_PIPELINE的变量且其值为作业名称时才运行的规则。这将确保作业仅在计划管道上运行。

2.1 自定义GIT、CURL和Composer标志

如果您需要为git或curl操作设置自定义标志,则可以使用变量JOB_GIT_FLAGSJOB_CURL_FLAGS

例如,如果您在自托管的gitlab实例上,您可能会遇到自签名证书的问题。

同样,您可能想告诉composer忽略特定的平台要求。

为了解决这个问题,您可以这样做

update_PHP_deps:
  extends: .update_PHP_deps
  variables:
    # ...
    JOB_GIT_FLAGS: "-c http.sslVerify=false"
    JOB_CURL_FLAGS: "--insecure"
    JOB_COMPOSER_FLAGS: "--ignore-platform-req=ext-bcmath --ignore-platform-req=ext-intl --ignore-platform-req=ext-soap"
  # ...

3. 在GitLab UI中安排管道

现在您可以创建管道调度以定期运行更新作业。您可以按任何方式设置它,只需确保您配置了一个名为SCHEDULED_PIPELINE的变量,其值为作业名称。

此外,请注意在“编辑流水线计划”界面中选择的分支,将是更新的目标分支。带有更新的分支将从该分支创建,并会将合并请求(MR)合并到该分支。

按照上述示例任务,您需要设置一个键为SCHEDULED_PIPELINE、值为update_PHP_deps的变量。

例如

Schedule the update pipeline