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

问题描述

我有一个 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 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...