更新:
在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'
请告诉我是否还需要提供其他内容。
AssertEquals
语句和类似的语句会失败;因为Azure DevOps环境不能成功处理脚本日志,如test_logging.py
所述。 - StressedBoi69420azure-pipeline-ontology_tagger.yaml
附加到了 @danielorn 的帖子中。 - StressedBoi69420