如何在 google 云构建中访问 git 标签?

How do I access a git tag in a google cloud build?

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

在第二次触发期间,我想验证我的版本号是否与 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 documentation 中指定的 TAG_NAME 变量似乎不包含 git 标记。

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

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