Azure DevOps 管道没有日志记录

7

更新:

在Azure DevOps上添加或更改执行管道的命令是否可行?


Visual Studio Code本地运行程序时,可以得到输出。

然而,在Azure DevOps上运行我的GitHub源分支没有任何输出。

我遵循了Stack Overflow答案,其中引用了这个GitHub问题的解决方案。

我已经实施了以下内容,但Azure的原始日志(Raw Logs)在我的Python logging中返回空白。

test_logging.py:

import logging

filename = "my.log"

global logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
open(filename, "w").close()  # empty logs
fileHandler = logging.FileHandler(filename)
fileHandler.setFormatter(formatter)
fileHandler.setLevel(logging.INFO)
logger.addHandler(fileHandler)

logger.error('TEST')

# fetch logs
with open(filename, "r") as fileHandler:
    logs = [log.rstrip() for log in fileHandler.readlines()]
open(filename, "w").close()  # empty logs
print('logs = ', logs)

>>> logs = []

host.json:

{
  "version":  "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "default": "Debug"
    }
  } 
}

我接着尝试了这个来自帖子的替代 host.json

"logging": {
    "fileLoggingMode": "debugOnly",
    "logLevel": {
        "default": "None",
        "Host.Results": "Information",
        "Function": "Information",
        "Host.Aggregator": "Information"
    },
    "applicationInsights": {
        "samplingSettings": {
            "isEnabled": false,
            "maxTelemetryItemsPerSecond": 5
        }
    }
}

azure-pipeline-ontology_tagger.yaml

# ##########
# A build run against multiple Python targets
# ##########

resources:
- repo: self

variables:
  tag: '$(Build.SourceBranchName)-$(Build.BuildNumber)'
  imageName: '$(Build.Repository.Name)-ontology_tagger'
  artifactFeed: grandproject/private-sources
  repositoryUrl: private-sources
  packageDirectory: workers/ontology_tagger

trigger:
  batch: true
  branches:
    include:
    - master
    - development
    - releases/*
  paths:
    include:
    - "workers/ontology_tagger"
    exclude:
    - "workers"
    - "*.md"
pr:
  branches:
    include:
    - master
    - development
    - releases/*
  paths:
    include:
    - "workers/ontology_tagger"
    exclude:
    - "workers"
    - "*.md"

stages:
- stage: BuildWP
  displayName: Build Workers python package
  jobs:

  - job: Build
    displayName: Build Worker python image

    pool:
      name: EKS-grandproject-dev

    steps:
    - bash: env

    - task: PipAuthenticate@0
      displayName: Authenticate with artifact feed
      inputs:
        artifactFeeds: $(artifactFeed)

    - task: TwineAuthenticate@1
      displayName: Authenticate with artifact feed
      inputs:
        artifactFeed: $(artifactFeed)

    - bash: echo "##vso[task.setvariable variable=POETRY_HTTP_BASIC_AZURE_PASSWORD;isOutput=true]$(echo $PIP_EXTRA_INDEX_URL | sed -r 's|https://(.+):(.+)@.*|\2|')"
      name: "PIPAUTH"

    - task: Bash@3
      displayName: Test worker
      inputs:
        targetType: 'inline'
        workingDirectory: '$(packageDirectory)'
        script: |
          docker build . --progress plain --pull --target test \
          --build-arg POETRY_HTTP_BASIC_AZURE_PASSWORD=${PIPAUTH_POETRY_HTTP_BASIC_AZURE_PASSWORD} \
          --build-arg ATLASSIAN_TOKEN=$(ATLASSIAN_TOKEN)
    - task: Bash@3
      displayName: Build and publish package
      inputs:
        targetType: 'inline'
        workingDirectory: '$(packageDirectory)'
        script: |
          set -e
          cp $(PYPIRC_PATH) ./
          docker build . --target package --progress plain  --build-arg REPO=$(repositoryUrl)
    - task: Bash@3
      displayName: Build docker image
      inputs:
        targetType: 'inline'
        workingDirectory: '$(packageDirectory)'
        script: |
          docker build . --tag '$(imageName):$(tag)' --progress plain --pull --target production \
          --build-arg POETRY_HTTP_BASIC_AZURE_PASSWORD=${PIPAUTH_POETRY_HTTP_BASIC_AZURE_PASSWORD} \
          --label com.azure.dev.image.build.sourceversion=$(Build.SourceVersion) \
          --label com.azure.dev.image.build.sourcebranchname=$(Build.SourceBranchName) \
          --label com.azure.dev.image.build.buildnumber=$(Build.BuildNumber)
    - task: ECRPushImage@1
      displayName: Push image with 'latest' tag
      condition: and(succeeded(),eq(variables['Build.SourceBranchName'], 'master'))
      inputs:
        awsCredentials: 'dev-azure-devops'
        regionName: 'eu-central-1'
        imageSource: 'imagename'
        sourceImageName: $(imageName)
        sourceImageTag: $(tag)
        repositoryName: $(imageName)
        pushTag: 'latest'
        autoCreateRepository: true

    - task: ECRPushImage@1
      displayName: Push image with branch name tag
      condition: and(succeeded(),ne(variables['Build.SourceBranchName'], 'merge'))
      inputs:
        awsCredentials: 'iotahoe-dev-azure-devops'
        regionName: 'eu-central-1'
        imageSource: 'imagename'
        sourceImageName: $(imageName)
        sourceImageTag: $(tag)
        repositoryName: $(imageName)
        pushTag: '$(Build.SourceBranchName)'
        autoCreateRepository: true

    - task: ECRPushImage@1
      displayName: Push image with uniq tag
      condition: and(succeeded(),ne(variables['Build.SourceBranchName'], 'merge'))
      inputs:
        awsCredentials: 'dev-azure-devops'
        regionName: 'eu-central-1'
        imageSource: 'imagename'
        sourceImageName: $(imageName)
        sourceImageTag: $(tag)
        repositoryName: $(imageName)
        pushTag: $(tag)
        autoCreateRepository: true
        outputVariable: 'ECR_PUSHED_IMAGE_NAME'

请告诉我是否还需要提供其他内容。


请说明“在Azure DevOps上运行我的GitHub源分支”是什么意思? 您如何执行Python代码?它是Azure函数还是作为管道的一部分运行的脚本?管道是什么样子的? - qbik
@qbik 抱歉,我不知道还有什么其他方式可以解释...我只是在澄清标准做法。远程分支作为一个Pipeline运行,它要么成功,要么失败。在我的情况下,AssertEquals语句和类似的语句会失败;因为Azure DevOps环境不能成功处理脚本日志,如test_logging.py所述。 - StressedBoi69420
1
@StressedBoi69420,你能否分享一下你的Azure DevOps Pipeline配置(例如yaml格式)? - danielorn
当然可以。我已经将 azure-pipeline-ontology_tagger.yaml 附加到了 @danielorn 的帖子中。 - StressedBoi69420
1个回答

2
我认为您在这里基本上混淆了一些事情:您提供并遵循的链接提供了有关在Azure Functions中设置日志记录的指南。但是,您似乎在谈论Azure Pipelines中的日志记录,这是完全不同的事情。因此,只是为了明确:
Azure Pipelines运行构建和部署作业,将您可能在GitHub存储库中拥有的代码部署到Azure Functions。管道在Azure Pipelines代理中执行,可以是Microsoft或Self-hosted。如果我们假设您正在使用Microsoft-Hosted代理执行管道,则不应假定这些代理具有Azure Functions可能具有的任何功能(也不应首先执行针对Azure Functions的代码)。如果您想在管道中执行Python代码,您应该首先查看托管代理预安装的与Python相关的功能,然后从那里开始工作:https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml 如果您想记录有关管道运行的任何内容,您应首先手动排队时检查“启用系统诊断”选项。要自己实现更多日志记录,请检查:https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=bash 对于Azure Functions中的日志记录,您可能希望从这里开始:https://learn.microsoft.com/en-us/azure/azure-functions/functions-monitoring,但这将是与Azure Pipelines中的日志记录完全不同的主题。

1
啊,问题已更新,我会检查yaml文件。 - JukkaK
1
我大胆地假设这里的问题是容器推送没有发生,您希望管道将容器推送到 AWS 容器存储库,并根据触发管道的分支使用不同的标记。可能存在多个问题,但任务条件显然是错误的。可能有更好的方法来实现相同的结果,但请尝试:and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))https://learn.microsoft.com/en-us/azure/devops/pipelines/process/conditions?view=azure-devops&tabs=yaml - JukkaK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接