如何通过无服务器的Azure管道部署python应用?

时间:2020-05-19 01:58:46

标签: azure-devops serverless

我尝试通过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'

1 个答案:

答案 0 :(得分: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