python在Azure Pipelines的'.py'中找不到'__main__'模块

问题描述

我的目标是通过Azure Pipeline从GitHub部署并运行我的python脚本到我的虚拟机。我的azure-pipelines.yml看起来像这样:

jobs: 
- deployment: VMDeploy
  displayName: Test_script
  environment:
    name: deploymentenvironment
    resourceType: VirtualMachine
  strategy:
      rolling:
        maxParallel: 2  #for percentages,mention as x%
        preDeploy:
          steps:
          - download: current
          - script: echo initialize,cleanup,backup,install certs
        deploy:
          steps:
          - task: Bash@3
            inputs:
              targettype: 'inline'
              script: python3 $(Agent.BuildDirectory)/test_file.py
        routeTraffic:
          steps:
          - script: echo routing traffic
        postRouteTraffic:
          steps:
          - script: echo health check post-route traffic
        on:
          failure:
            steps:
            - script: echo Restore from backup! This is on failure
          success:
            steps:
            - script: echo Notify! This is on success

这将返回错误

/usr/bin/python3: can't find '__main__' module in '/home/ubuntu/azagent/_work/1/test_file.py'

##[error]Bash exited with code '1'.

如果我将test_file.py放置到/home/ubuntu上,并用以下内容替换部署脚本:script: python3 /home/ubuntu/test_file.py脚本运行正常。

如果我用test_file.pymv /home/ubuntu/azagent/_work/1/test_file.py /home/ubuntu移到另一个目录,则可以找到一个名为.py的空文件夹,而不是test_file.py文件

编辑

乔布斯的截图:

enter image description here

解决方法

无法获取源代码的原因是因为您使用了download: currentdownload artifacts,这是由当前管道运行产生的,但是您没有在当前管道中发布任何工件。

由于部署作业不会自动签出源代码,因此您在部署作业中需要checkout源代码,

       - checkout: self

publish下载工件之前的来源。

      - publish: $(Build.SourcesDirectory)
        artifact: Artifact_Deploy