【问题标题】:How do I access a git tag in a google cloud build?如何在谷歌云构建中访问 git 标签?
【发布时间】:2021-08-09 02:06:26
【问题描述】:

我有一个云源存储库,用于维护我的 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 标签进行验证?

【问题讨论】:

    标签: python git google-cloud-platform google-cloud-build cloudbuild.yaml


    【解决方案1】:

    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-18
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      • 2020-04-11
      • 2013-12-19
      • 2023-04-06
      • 2019-08-24
      • 2018-03-01
      相关资源
      最近更新 更多