1

我有一个 Cloud Source Repository,我在其中维护我的 python 包的代码。我设置了两个触发器:

  • 在每个分支上的每次提交上运行的触发器(这个安装我的 python 包并测试代码。
  • 在推送的 git 标签上运行的触发器(安装包、测试、构建工件,并将它们部署到我的私有 pypi 存储库)。

在第二次触发期间,我想验证我的版本号是否与 git 标签匹配。在 setup.py 文件中,我添加了代码:

#!/usr/bin/env python
import sys
import os
from setuptools import setup
from setuptools.command.install import install

VERSION = "v0.1.5"


class VerifyVersionCommand(install):
    """Custom command to verify that the git tag matches our version"""
    description = 'verify that the git tag matches our version'

    def run(self):
        tag = os.getenv('TAG_NAME')

        if tag != VERSION:
            info = "Git tag: {0} does not match the version of this app: {1}".format(
                tag, VERSION
            )
            sys.exit(info)


setup(
    name="name",
    version=VERSION,
    classifiers=["Programming Language :: Python :: 3 :: Only"],
    py_modules=["name"],
    install_requires=[
        [...]
    ],
    packages=["name"],
    cmdclass={
        'verify': VerifyVersionCommand,
    }
)

我的 cloudbuild.yaml 的开头如下所示:

steps:

  - name: 'docker.io/library/python:3.8.6'
    id: Install
    entrypoint: /bin/sh
    args:
      - -c
      - |
        python3 -m venv /workspace/venv &&
        . /workspace/venv/bin/activate &&
        pip install -e .

  - name: 'docker.io/library/python:3.8.6'
    id: Verify
    entrypoint: /bin/sh
    args:
      - -c
      - |
        . /workspace/venv/bin/activate &&
        python setup.py verify

这在 CircleCi 上完美运行,但在 Cloud Build 上我收到错误消息:

Finished Step #0 - "Install"
Starting Step #1 - "Verify"
Step #1 - "Verify": Already have image: docker.io/library/python:3.8.6
Step #1 - "Verify": running verify
Step #1 - "Verify": /workspace/venv/lib/python3.8/site-packages/setuptools/dist.py:458: UserWarning: Normalizing 'v0.1.5' to '0.1.5'
Step #1 - "Verify":   warnings.warn(tmpl.format(**locals()))
Step #1 - "Verify": Git tag: None does not match the version of this app: v0.1.5
Finished Step #1 - "Verify"
ERROR
ERROR: build step 1 "docker.io/library/python:3.8.6" failed: step exited with non-zero status: 1

因此,Cloud Build 文档TAG_NAME中指定的变量似乎不包含 git 标签。

如何访问 git 标签来验证它?

4

1 回答 1

0

TAG_NAME设置为替换变量,但不设置为环境变量

你可以这样做

  - name: 'docker.io/library/python:3.8.6'
    id: Verify
    entrypoint: /bin/sh
    env:
    - "TAG_NAME=$TAG_NAME"
    args:
      - -c
      - |
        . /workspace/venv/bin/activate &&
        python setup.py verify
于 2021-05-20T07:06:34.500 回答