首页 > 解决方案 > CodeBuild 仍在通过测试阶段

问题描述

我在部署我的应用程序时遇到问题,因为我的测试没有运行。它是一个简单的脚本,但代码构建仍然绕过了我的测试。我已经指定了 unittest 并将我的 unittest-buildspec 的路径放在我的应用程序的控制台中,如下所示:

-圣杯

--.圣杯

-- 构建规范

---- 构建.sh

---- unittest-buildspec.ym

-- 测试

---- test_app.py

---- 测试数据库.py

-- 应用程序.py

version: 0.2

phases:
install:
runtime-versions:
  python: 3.7
commands:
  - pip install -r requirements_test.txt

build:
  commands:
    - echo Build started on `date` ---
    - pip install -r requirements_test.txt
    - ./build.sh
    - pytest --pep8 --flakes  

artifacts:
  files: 
    - '**/*'
  base-directory: 'my-build*'
  discard-paths: yes

我的 build.sh 也在同一个文件夹中

#!/bin/bash
pip install --upgrade awscli
aws --version
cd ..
pip install virtualenv
virtualenv /tmp/venv
. /tmp/venv/bin/activate
export PYTHONPATH=.
py.test tests/ || exit 1

标签: pythondevopsaws-codebuild

解决方案


您共享的构建规范中有一些问题:

  1. “安装”和“构建”阶段的缩进不正确。它们应该属于“阶段”。

  2. 在运行之前在 build.sh 上设置“+x”。

修复 buildspec.yml:

version: 0.2 

phases: 
    install: 
        runtime-versions: 
          python: 3.7 
        commands: 
            - pip install -r requirements_test.txt 

    build: 
        commands: 
            - echo Build started on `date` --- 
            - pip install -r requirements_test.txt 
            - chmod +x ./build.sh 
            - ./build.sh 
            - pytest --pep8 --flakes   

artifacts: 
    files:  
        - '**/*' 
    base-directory: 'my-build*' 
    discard-paths: yes 


另请注意,您的“build.sh”使用“/bin/bash”解释器,虽然脚本可以工作,但从技术上讲,shell 不是“bash”,因此任何 bash 特定功能都不起作用。CodeBuild 的外壳很神秘,它会运行通常的脚本,只是不是 bash。


推荐阅读