我正在尝试一步一步地复制本视频"云简历挑战冲刺(2022年9月)-第4周">从youtube, https://youtu.be/wiyI0Ngn31o,关于如何设置GitHub Actions与CD/CI管道的后端测试与Python在AWS中的SAM部署。
我已经按照视频的说明一步一步地做了;然而,当我推送邮件时。我从GitHub Actions中得到以下错误消息:
Run pytest
============================= test session starts ==============================
platform linux -- Python 3.8.15, pytest-7.2.0, pluggy-1.0.0
rootdir: /home/runner/work/cloud-resume-challenge/cloud-resume-challenge
plugins: mock-3.10.0
collected 0 items / 1 error
==================================== ERRORS ====================================
_ ERROR collecting serverless-architecture-with-SAM/hello_world/tests/test_handler.py _
serverless-architecture-with-SAM/hello_world/tests/test_handler.py:6: in <module>
from hello_world import app
serverless-architecture-with-SAM/hello_world/app.py:4: in <module>
dynamodb = boto3.resource('dynamodb')
/opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/boto3/__init__.py:101: in resource
return _get_default_session().resource(*args, **kwargs)
/opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/boto3/session.py:446: in resource
client = self.client(
/opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/boto3/session.py:299: in client
return self._session.create_client(
/opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/botocore/session.py:976: in create_client
client = client_creator.create_client(
/opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/botocore/client.py:155: in create_client
client_args = self._get_client_args(
/opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/botocore/client.py:485: in _get_client_args
return args_creator.get_client_args(
/opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/botocore/args.py:92: in get_client_args
final_args = self.compute_client_args(
/opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/botocore/args.py:205: in compute_client_args
endpoint_config = self._compute_endpoint_config(
/opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/botocore/args.py:313: in _compute_endpoint_config
return self._resolve_endpoint(**resolve_endpoint_kwargs)
/opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/botocore/args.py:418: in _resolve_endpoint
return endpoint_bridge.resolve(
/opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/botocore/client.py:590: in resolve
resolved = self.endpoint_resolver.construct_endpoint(
/opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/botocore/regions.py:229: in construct_endpoint
result = self._endpoint_for_partition(
/opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/botocore/regions.py:277: in _endpoint_for_partition
raise NoRegionError()
E botocore.exceptions.NoRegionError: You must specify a region.
=========================== short test summary info ============================
ERROR serverless-architecture-with-SAM/hello_world/tests/test_handler.py - botocore.exceptions.NoRegionError: You must specify a region.
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.40s ===============================
Error: Process completed with exit code 2.
这是我的邮件副本。yml文件:name: main
on: push
jobs:
test-infra:
runs-on: ubuntu-latest
timeout-minutes: 2
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Install dependencies
run: |
cd serverless-architecture-with-SAM/hello_world/tests
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests with pytest
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
run: pytest
build-and-deploy-infra:
needs: test-infra
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: aws-actions/setup-sam@v1
- uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- run: sam build
working-directory: serverless-architecture-with-SAM
- run: sam deploy --no-confirm-changeset --no-fail-on-empty-changeset
working-directory: serverless-architecture-with-SAM
deploy-site:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: jakejarvis/s3-sync-action@master
with:
args: --delete
env:
AWS_S3_BUCKET: justinhenson-cloud-resume-website
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
SOURCE_DIR: serverless-architecture-with-SAM/resume-site
下面是我的GitHub Repo中的文件结构:https://github.com/justin-henson/cloud-resume-challenge
account/cloud-resume-challenge
.github/
workflows/
main.yml
serverless-architecture-with-SAM/
hello_world/
tests/
__init__.py
requirements.txt
test_handler.py
__init__.py
app.py
requirements.txt
resume-site/
css/
img/
js/
index.html
Makefile
README.md
samconfig.toml
template.yaml
.gitignore
和test_handler.py文件的内容:
import os
import re
import json
from unittest import mock
from hello_world import app
with open('serverless-architecture-with-SAM/template.yaml', 'r') as f:
TABLENAME = re.search(r'TableName: (.*)?', f.read()).group(1)
@mock.patch.dict(os.environ, {"TABLENAME": TABLENAME})
def test_lambda_handler():
# Check AWS creds
assert "AWS_ACCESS_KEY_ID" in os.environ
assert "AWS_SECRET_ACCESS_KEY" in os.environ
ret = app.lambda_handler("", "")
# Assert return keys
assert "statusCode" in ret
assert "headers" in ret
assert "body" in ret
# Check for CORS in Headers
assert "Access-Control-Allow-Origin" in ret["headers"]
assert "Access-Control-Allow-Methods" in ret["headers"]
assert "Access-Control-Allow-Headers" in ret["headers"]
# Check status code
if ret["statusCode"] == 200:
assert "visit_count" in ret["body"]
assert json.loads(ret["body"])["visit_count"].isnumeric()
else:
assert json.loads(ret["body"])["visit_count"] == -1
return
我尝试将以下内容添加到我的test_handler.py文件,但没有成功:
os.environ['AWS_DEFAULT_REGION'] = 'your_region_name'
在test-infra
作业的环境变量部分添加AWS_DEFAULT_REGION: us-east-1
您定义了aws-region
,但AWS_DEFAULT_REGION
区分大小写。