对于我的一个项目,我尝试在 gitlab 上设置 CI/CD 管道,使用以下管道脚本:
# This file is a template, and might need editing before it works on your project.
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Python.gitlab-ci.yml
# Official language image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/python/tags/
image: python:3.11.8
# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/topics/caching/
#
# If you want to also cache the installed packages, you have to install
# them in a virtualenv and cache it as well.
cache:
paths:
- .cache/pip
- venv/
before_script:
- apt-get update && apt-get install ffmpeg libsm6 libxext6 libegl-dev -y
- python --version # For debugging
- pip install poetry
- cd ./project_dir
- poetry install --with dev
- source `poetry env info --path`/bin/activate
stages:
- test
- wheel_build
- doc_build
- release
test_job:
image: "python:$VERSION"
stage: test
script:
- ls -R
- poetry run pytest ./tests/ --junitxml=report.xml
- ls -R
parallel:
matrix:
- VERSION: ["3.9", "3.11.8"] #["3.9", "3.10", "3.11.8", "3.12"]
artifacts:
when: always
reports:
junit: /builds/report.xml
wheel_build_job:
stage: wheel_build
needs: [test_job]
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- ls -R
- poetry build
- ls -R
- pwd
artifacts:
paths:
- /builds/dist/*.whl
doc_build_job:
stage: doc_build
needs: [test_job]
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- cd docs
- poetry run sphinx-apidoc -o ./source/ ../test_code/
- poetry run sphinx-build -M html ./source/ ./build/
- mv build/html/ ../public/
artifacts:
paths:
- /builds/public
release_job:
stage: release
needs: [wheel_build_job]
image: registry.gitlab.com/gitlab-org/release-cli:latest
rules:
- if: $CI_COMMIT_TAG # Run this job when a tag is created
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- echo "running release_job"
release: # See https://docs.gitlab.com/ee/ci/yaml/#release for available properties
tag_name: "$CI_COMMIT_TAG"
description: "$CI_COMMIT_TAG"
最初一切正常,每次我推送时都会执行两个测试作业,但即使在主程序上合并时(即当
$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
为 true 时)也不会执行进一步的步骤。我认为这是由于在合并之前执行了作业,但是,如果我在 master 上手动启动作业,它仍然只执行测试作业,没有其他。
我到底应该在此处更改什么才能在合并到 master 后运行所有作业?
我认为它不起作用的原因是因为您使用的变量之一($CI_COMIT_BRANCH)未在合并管道上定义,因此不等于默认分支。 $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
来自 gitlabs 文档:
提交分支名称。可用于分支管道,包括 默认分支的管道。在合并请求中不可用 管道或标记管道。
我认为您已经确定了正确的问题,我会尝试使用一些不同的规则和其他一些预定义变量。
或者,您可以仅当您要在那里运行的作业的提交分支为 == master 且为要在合并之前运行的作业的 != master 时才运行作业。
您可以尝试删除规则并建立规则,直到获得正确的顺序