在天蓝色管道中分离构建和测试阶段的正确机制是什么?
What is the proper mechanism for separating build and test stages in azure-pipelines?
我有一个带有以下 azure-pipelines.yml 的玩具 C++ cmake 项目(请参阅 HelloAzure 了解来源)。
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- master
variables:
SOURCE_DIR: '$(System.DefaultWorkingDirectory)'
BUILD_DIR: '$(SOURCE_DIR)/build'
INSTALL_DIR: '$(SOURCE_DIR)/install'
stages:
- stage: Build
displayName: Build
pool:
vmImage: ubuntu-latest
jobs:
- job: BuildLinux
strategy:
matrix:
Release:
BUILD_TYPE: 'Release'
Debug:
BUILD_TYPE: 'Debug'
steps:
- script: |
echo "mkdir BUILD_DIR $(BUILD_DIR)"
mkdir $(BUILD_DIR)
echo "cd build directory "
cd $(BUILD_DIR)
echo " Run configure command"
cmake -DCMAKE_INSTALL_PREFIX=$(INSTALL_DIR)/$(BUILD_TYPE) $(SOURCE_DIR)
echo "running cmake build command"
cmake --build . --target install --config $(BUILD_TYPE) -j 12
- stage: BuildTests
dependsOn: Build
displayName: BuildTests
jobs:
- job: RunCTestLinux
steps:
- script: |
cd $(BUILD_DIR)
ctest
在本地我可以执行以下操作:
# download sources
git clone https://github.com/CiaranWelsh/HelloAzure.git
# build
cd HelloAzure
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=../install ..
cmake --build . --target install --config Release -j 12
# test
ctest
我正在尝试在 Azure 中重现此工作流程。只使用一个阶段是微不足道的,但是一旦我尝试 运行 在“测试”阶段进行测试,Azure simply re-downloads the sources and tried to run the tests without first having ran the build command.
所以我的问题是,在 azure-pipelines 中分离项目的构建和测试阶段的正确机制是什么?
针对您的场景,您可以尝试以下方法:
1 .在Build阶段,添加一个发布构建输出的任务。例如:
steps:
- publish: string # path to a file or folder
artifact: string # artifact name
displayName: string # friendly name to display in the UI
2 .在BuildTests阶段,添加下载构建输出的任务:
steps:
- download: [ current | pipeline resource identifier | none ] # disable automatic download if "none"
artifact: string ## artifact name, optional; downloads all the available artifacts if not specified
patterns: string # patterns representing files to include; optional
displayName: string # friendly name to display in the UI
3 .非部署作业自动签出源代码。使用 checkout
关键字来配置或抑制此行为。在 BuildTests 阶段,添加以下步骤以自动抑制检出的源代码:
steps:
- checkout: none
我有一个带有以下 azure-pipelines.yml 的玩具 C++ cmake 项目(请参阅 HelloAzure 了解来源)。
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- master
variables:
SOURCE_DIR: '$(System.DefaultWorkingDirectory)'
BUILD_DIR: '$(SOURCE_DIR)/build'
INSTALL_DIR: '$(SOURCE_DIR)/install'
stages:
- stage: Build
displayName: Build
pool:
vmImage: ubuntu-latest
jobs:
- job: BuildLinux
strategy:
matrix:
Release:
BUILD_TYPE: 'Release'
Debug:
BUILD_TYPE: 'Debug'
steps:
- script: |
echo "mkdir BUILD_DIR $(BUILD_DIR)"
mkdir $(BUILD_DIR)
echo "cd build directory "
cd $(BUILD_DIR)
echo " Run configure command"
cmake -DCMAKE_INSTALL_PREFIX=$(INSTALL_DIR)/$(BUILD_TYPE) $(SOURCE_DIR)
echo "running cmake build command"
cmake --build . --target install --config $(BUILD_TYPE) -j 12
- stage: BuildTests
dependsOn: Build
displayName: BuildTests
jobs:
- job: RunCTestLinux
steps:
- script: |
cd $(BUILD_DIR)
ctest
在本地我可以执行以下操作:
# download sources
git clone https://github.com/CiaranWelsh/HelloAzure.git
# build
cd HelloAzure
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=../install ..
cmake --build . --target install --config Release -j 12
# test
ctest
我正在尝试在 Azure 中重现此工作流程。只使用一个阶段是微不足道的,但是一旦我尝试 运行 在“测试”阶段进行测试,Azure simply re-downloads the sources and tried to run the tests without first having ran the build command.
所以我的问题是,在 azure-pipelines 中分离项目的构建和测试阶段的正确机制是什么?
针对您的场景,您可以尝试以下方法:
1 .在Build阶段,添加一个发布构建输出的任务。例如:
steps:
- publish: string # path to a file or folder
artifact: string # artifact name
displayName: string # friendly name to display in the UI
2 .在BuildTests阶段,添加下载构建输出的任务:
steps:
- download: [ current | pipeline resource identifier | none ] # disable automatic download if "none"
artifact: string ## artifact name, optional; downloads all the available artifacts if not specified
patterns: string # patterns representing files to include; optional
displayName: string # friendly name to display in the UI
3 .非部署作业自动签出源代码。使用 checkout
关键字来配置或抑制此行为。在 BuildTests 阶段,添加以下步骤以自动抑制检出的源代码:
steps:
- checkout: none