我是Google Cloud Artifact Registry的新手,我设法在那里发布了一个模块。然而,现在我想通过将其放入git管道来实现自动化,但不幸的是,我无法实现这一点。
我认为问题是我必须使用两个不同的图像(python和google/cloud-sdk(,但你不能在一个脚本中使用它。我尝试过两种不同的解决方案,但都不起作用。
我知道这不是有效的代码,但我想让它首先工作
版本1:
stages:
- test
- install
- authenticate
- deploy
python-installs:
image: python:latest
stage: install
script:
- pip install build twine
- pip install build
- python -m pip install keyring
- python -m pip install keyrings.google-artifactregistry-auth
login-to-gcp:
image: google/cloud-sdk
stage: authenticate
tags:
- script
script:
- echo $GOOGLE_SERVICE_ACCOUNT_JSON > deployment_service_account.json
- gcloud auth activate-service-account --key-file deployment_service_account.json
- rm deployment_service_account.json
unit-test-job:
image: python:latest
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
cache:
paths:
- .cache/pip
- venv/
before_script:
- python3 -V # Print out python version for debugging
- pip3 install virtualenv
- virtualenv venv
- source venv/bin/activate
- pip3 install requests
- pip3 install python-dotenv
script:
- python3 -m unittest discover
deploy-package:
stage: deploy
image: python:latest
tags:
- script
only:
- master
script:
- pip install build
- pip install build twine
- python -m pip install keyring
- python -m pip install keyrings.google-artifactregistry-auth
- python -m build
- python -m twine upload --repository-url https://xxxxxxxx-python.pkg.dev/devote-staging/xxxxxxxx/ dist/*
错误:/bin/bash:行127:gcloud:命令未找到,但如果我删除带有gcloud auth-activate service account的行--密钥文件deployment_service_account.json,我会收到未通过身份验证的错误。
版本2:
stages:
- test
- build
- authenticate
- deploy
.python-installs: &installs
image: python:latest
before_script:
- pip install build twine
- pip install build
- python -m pip install keyring
- python -m pip install keyrings.google-artifactregistry-auth
.login-to-gcp: &logintogcp
image: google/cloud-sdk
tags:
- script
before_script:
- echo $GOOGLE_SERVICE_ACCOUNT_JSON > deployment_service_account.json
- gcloud auth activate-service-account --key-file deployment_service_account.json
- rm deployment_service_account.json
unit-test-job:
image: python:latest
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
cache:
paths:
- .cache/pip
- venv/
before_script:
- python3 -V # Print out python version for debugging
- pip3 install virtualenv
- virtualenv venv
- source venv/bin/activate
- pip3 install requests
- pip3 install python-dotenv
script:
- python3 -m unittest discover
deploy-package:
stage: deploy
<<: *installs
<<: *logintogcp
image: python:latest
tags:
- script
only:
- master
script:
- python -m build
- python -m twine upload --repository-url https://xxxxxxxx-python.pkg.dev/devote-staging/xxxxxxxx/ dist/*
错误:/bin/bash:行119:gcloud:找不到命令
正如@destination所评论的,Error: gcloud: command not found
表示您的机器上没有安装云sdk。
尝试使用包含cloud-cli最新映像的google/cloud-sdk:latest
标记。
在此之前,请确保已安装Google Cloud CLI Docker image
。
要从docker安装Cloud cli映像,请执行以下步骤:
- 要使用此映像,请从Docker Hub中提取,运行以下命令:
docker pull google/cloud-sdk:latest
- 验证安装
docker run -ti google/cloud-sdk:latest gcloud version
Google Cloud SDK 159.0.0
- 或使用特定的版本号:
docker run -ti google/cloud-sdk:160.0.0 gcloud version
或者,如果您想在Google Container Registry上安装托管的Cloud cli映像。请参阅本文档。