Python 如何在gitlab中为一个简单的hello world程序配置gitlab-ci.yml

Python 如何在gitlab中为一个简单的hello world程序配置gitlab-ci.yml,python,gitlab,gitlab-ci,Python,Gitlab,Gitlab Ci,我想知道如何配置正确的.gitlab ci.yml文件,以便它能够自动检测我提交到项目中的代码中的错误 例如,我将创建一个新的python文件helloworld.py: print("hello world"" 上面的代码中有一个明显的错误,我希望我的.gitlab ci.yml能够测试该代码并确保它不会通过 我该怎么做?我非常感谢您在这方面提供的帮助。请尝试以过梁方式执行脚本: .gitlab ci.yml: image: ubuntu hello-test: script:

我想知道如何配置正确的
.gitlab ci.yml
文件,以便它能够自动检测我提交到项目中的代码中的错误

例如,我将创建一个新的python文件
helloworld.py

print("hello world""
上面的代码中有一个明显的错误,我希望我的
.gitlab ci.yml
能够测试该代码并确保它不会通过


我该怎么做?我非常感谢您在这方面提供的帮助。

请尝试以过梁方式执行脚本:

.gitlab ci.yml

image: ubuntu

hello-test:
    script: 
        - apt-get update && apt-get install pylint3
        - pylint3 helloworld.py
image: ubuntu

hello-test:
    script: 
        - apt-get update && apt-get install python3
        - python3 helloworld.py
或者直接在解释器中执行:

.gitlab ci.yml

image: ubuntu

hello-test:
    script: 
        - apt-get update && apt-get install pylint3
        - pylint3 helloworld.py
image: ubuntu

hello-test:
    script: 
        - apt-get update && apt-get install python3
        - python3 helloworld.py

请尝试使用以下代码:

stages:
  - build

PythonBuild:
  stage: build
  script:
    - python helloworld.py

顺便说一句,如果您想检查所有python文件,您可以添加一个shell脚本来帮助您完成这项工作

bash.sh

#! bin/sh
for n in `find . -name "*.py"`
do
  python $n
done
然后编辑.gitlab ci.yml,如下所示:

stages:
  - build

PythonBuild:
  stage: build
  script:
    - bash build.sh

:记住使用.gitlab ci.yml

build.bash推送到gitlab存储库的根路径你好,Jan,谢谢你的回复。我已经将上面的代码复制到我的gitlab-ci.yml中。我在我的项目中创建了一个名为helloworld.py的新文件,并在其中放置了print(“helloworld”),代码很好,但测试仍然失败:-(有什么帮助吗?GitLab runner应该给你一些输出并告诉你失败的原因。linter不仅会检查代码的语法是否正确,还会检查代码是否符合Python编码风格和实践。有多少
pylint
测试是可配置的。我只使用解释器添加了一个版本。理想情况下,你可以运行一个真正的coverage test.Hi Jan,我已经修改了我的yml文件,将您的代码复制粘贴到该文件中。但是正确的打印(“hello world”)代码将无法通过测试。它只显示错误:作业失败:退出代码1。任何其他建议?可能会失败,因为&&apt get install python3请求输入(Y或N)。如果您修改为“apt-get-update&&apt-get-install python3-y",这运行平稳OP可能想编写Python3代码,所以他应该使用
Python3
解释器。它可以工作!!!!测试失败了我的代码,并显示了错误。谢谢你现在如何将此yml代码用于我提交到项目中的多个python文件:例如helloworld.py和test。py@JanChristophTerasa我认为这取决于gitlab runner环境中的ds,如果您想在.gitlab-ci.yml中使用任何东西,您应该将它们安装到gitlab runner的服务器上。在.gitlab-ci.yml中使用命令进行安装。我认为这不是最好的尝试。@EndisSapuandi我已经编辑了我的建议,请检查:)