【问题标题】:How to deploy python app with serverless azure pipeline?如何使用无服务器 Azure 管道部署 python 应用程序?
【发布时间】:2020-05-19 01:58:46
【问题描述】:

我尝试通过 azure devops 管道部署无服务器 python 应用程序。

serverless.yml

service: ws-serverless
provider:
  name: aws
  runtime: python3.7
  websocketApiName: ws-serverless-api
  websocketApiRouteSelectionExpression: $request.body.action

  stage: dev
  region: ap-northeast-2

  iamRoleStatements:

    - Effect: "Allow"
      Action:
        - "execute-api:ManageConnections"
      Resource: 
        - "arn:aws:execute-api:*:*:**/@connections/*"

    - Effect: "Allow"
      Action:
        - "dynamodb:PutItem"
        - "dynamodb:GetItem"
        - "dynamodb:UpdateItem"
        - "dynamodb:DeleteItem"
        - "dynamodb:BatchGetItem"
        - "dynamodb:BatchWriteItem"
        - "dynamodb:Scan"
        - "dynamodb:Query"
      Resource: 
        - "arn:aws:dynamodb:ap-northeast-2:*:*"

functions:
  connectionManager:
    handler: handler.connection_manager
    events:
        - websocket:
            route: $connect
        - websocket:
            route: $disconnect
  defaultMessage:
    handler: handler.default_message
    events:
        - websocket:
            route: $default
  getRecentMessages:
    handler: handler.get_recent_messages
    events:
        - websocket:
            route: getRecentMessages
  sendMessage:
    handler: handler.send_message
    events:
        - websocket:
            route: sendMessage
  ping:
    handler: handler.ping
    events:
        - http:
            path: ping
            method: get

plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
      dockerizePip: true
      noDeploy: []

azure-pipelines.yml

trigger:
- test

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.7'
    addToPath: true
  displayName: Install Python

- task: NodeTool@0
  inputs:
    versionSpec: '10.x'
  displayName: 'Install Node.js'

- script: |
    npm install --no-cache
  displayName: 'npm install'

- task: AWSShellScript@1
  inputs:
    awsCredentials: aws
    regionName: 'ap-northeast-2'
    scriptType: 'inline'
    inlineScript: |
      ./node_modules/.bin/serverless package --stage dev --region ap-northeast-2 --package /tmp/dev_artifacts/
  displayName: Package for Dev Environment

- task: PublishBuildArtifacts@1
  inputs:
    pathToPublish: /tmp/dev_artifacts
    artifactName: dev_artifacts
  displayName: Export Dev Artifacts

Package for Dev Environment 上失败

我收到此错误消息

错误:ENOENT:没有这样的文件或目录,打开 '/home/vsts/work/1/s/venv/bin/python'

所以我尝试使用 Virtualenv 在该路径中安装 Python,但它也失败了。

FileExistsError: [Errno 17] File exists: '/opt/hostedtoolcache/Python/3.7.7/x64/bin/python' -> '/home/vsts/work/1/s/venv/bin/python'

【问题讨论】:

    标签: azure-devops serverless


    【解决方案1】:

    我以完全不同的方式做到了。我使用了全局安装的serverless 包。但一切顺利。 (几乎所有工作目录都不适合我,所以我不得不直接导航到文件夹)。希望对你有帮助:

    trigger:
    - test
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '3.7'
        addToPath: true
      displayName: Install Python
    
    - task: NodeTool@0
      inputs:
        versionSpec: '10.x'
      displayName: 'Install Node.js'
    
    - script: |
        npm install
      displayName: 'npm install dependency'
    
    - script: |
        npm install serverless -g
      displayName: 'npm install serverless'
    
    - task: AWSShellScript@1
      inputs:
        awsCredentials: aws
        workingDirectory: $(Build.SourcesDirectory)/stackoverflow/31-serverless-aws
        regionName: 'ap-northeast-2'
        scriptType: 'inline'
        inlineScript: |
          cd $(Build.SourcesDirectory)/stackoverflow/31-serverless-aws
          ls
          serverless package --stage dev --region ap-northeast-2 --package $(Build.ArtifactStagingDirectory)
      displayName: Package for Dev Environment
    
    - task: PublishBuildArtifacts@1
      inputs:
        pathToPublish: $(Build.ArtifactStagingDirectory)
        artifactName: dev_artifacts
      displayName: Export Dev Artifacts
    

    【讨论】:

    • 请正确阅读问题。我已经用过 UsePythonVersion@0。
    • 完美运行
    猜你喜欢
    • 2018-08-03
    • 1970-01-01
    • 2020-11-20
    • 2020-09-30
    • 1970-01-01
    • 2019-09-28
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多