Azure Pipelines多Job步骤复用与共享依赖环境咨询
Azure Pipelines 复用步骤与共享依赖状态方案
1. 多Job复用相同步骤的方法
直接使用Azure Pipelines的**步骤模板(Step Templates)**即可解决重复步骤维护麻烦的问题,操作如下:
- 新建一个模板文件(例如
common-steps.yml),将所有重复的步骤移入该文件:
steps: - checkout: self fetchDepth: 1 persistCredentials: true # Set Azure Devops CLI default settings - bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject) displayName: 'Set default Azure DevOps organization and project' # Get last successfully commit infos from Azure Devops - bash: | LAST_SHA=$(az pipelines build list --branch $(Build.SourceBranchName) --definition-ids $(System.DefinitionId) --result succeeded --top 1 --query "[0].triggerInfo.\"ci.sourceSha\"") echo "Last successful commit SHA: $LAST_SHA" echo "##vso[task.setvariable variable=BASE_SHA]$LAST_SHA" displayName: 'Get last successful commit SHA' condition: ne(variables['Build.Reason'], 'PullRequest') env: AZURE_DEVOPS_EXT_PAT: $(System.AccessToken) # Required for nx affected if we're on a branch - script: git fetch origin develop --depth 1 displayName: 'fetch develop branch' - task: NodeTool@0 inputs: versionSpec: '20.x' displayName: 'Install Node.js' - script: | corepack enable yarn install --immutable displayName: 'install dependencies'
- 在主配置文件
azure-pipelines.yml的每个Job中,通过template字段引用该模板,替换原有重复步骤:
jobs: - job: Format_Checking steps: - template: common-steps.yml # 此处添加Format_Checking专属步骤 - script: yarn format:check displayName: 'Check code format' - job: Unit_Tests steps: - template: common-steps.yml # 此处添加Unit_Tests专属步骤 - script: yarn test:unit displayName: 'Run unit tests' - job: Cypress_Tests steps: - template: common-steps.yml # 此处添加Cypress_Tests专属步骤 - script: yarn test:e2e displayName: 'Run Cypress tests' - job: Build steps: - template: common-steps.yml # 此处添加Build专属步骤 - script: yarn build displayName: 'Build project'
后续修改公共步骤时,只需更新common-steps.yml,所有Job会自动同步变更。
2. 让所有Job共享依赖状态,避免重复安装node_modules
有两种实用方案可供选择:
方案一:用Cache任务缓存node_modules
在公共步骤中加入Cache@2任务,基于yarn.lock的哈希值缓存node_modules,后续Job或重新运行时会优先读取缓存,无需重复安装。修改common-steps.yml,在yarn install前添加缓存步骤:
steps: # ... 保留原有checkout、az配置等步骤 ... - task: NodeTool@0 inputs: versionSpec: '20.x' displayName: 'Install Node.js' # 添加缓存步骤 - task: Cache@2 inputs: key: 'yarn | "$(Agent.OS)" | yarn.lock' restoreKeys: | yarn | "$(Agent.OS)" path: $(System.DefaultWorkingDirectory)/node_modules displayName: 'Cache node_modules' - script: | corepack enable yarn install --immutable displayName: 'install dependencies'
该方案配置简单,只要yarn.lock未变更,就会复用缓存的依赖包。
方案二:前置Job+工件共享依赖
创建一个专门的Prepare_Dependencies前置Job,仅执行一次依赖安装,再将node_modules发布为工件,后续所有Job下载该工件直接使用:
- 在主配置文件中添加前置Job:
jobs: - job: Prepare_Dependencies steps: - checkout: self fetchDepth: 1 persistCredentials: true # 保留公共步骤中的基础配置(az配置、fetch develop、Node安装、yarn install) - bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject) displayName: 'Set default Azure DevOps organization and project' - script: git fetch origin develop --depth 1 displayName: 'fetch develop branch' - task: NodeTool@0 inputs: versionSpec: '20.x' displayName: 'Install Node.js' - script: | corepack enable yarn install --immutable displayName: 'install dependencies' # 发布node_modules为工件 - publish: $(System.DefaultWorkingDirectory)/node_modules artifact: node_modules_artifact displayName: 'Publish node_modules artifact' # 其他Job依赖前置Job,下载工件后使用 - job: Format_Checking dependsOn: Prepare_Dependencies steps: - checkout: self fetchDepth: 1 persistCredentials: true # 保留必要的基础步骤(az配置、fetch develop、Node安装) - bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject) displayName: 'Set default Azure DevOps organization and project' - script: git fetch origin develop --depth 1 displayName: 'fetch develop branch' - task: NodeTool@0 inputs: versionSpec: '20.x' displayName: 'Install Node.js' # 下载依赖工件并移至工作目录 - download: current artifact: node_modules_artifact displayName: 'Download node_modules artifact' - script: mv $(Pipeline.Workspace)/node_modules_artifact/* $(System.DefaultWorkingDirectory)/node_modules/ displayName: 'Move cached node_modules to working directory' # 执行专属步骤 - script: yarn format:check displayName: 'Check code format'
Unit_Tests、Cypress_Tests、Build等Job可参照上述Format_Checking的配置,添加依赖和工件下载步骤。该方案能确保所有Job使用完全一致的依赖环境,但配置相对复杂。
内容的提问来源于stack exchange,提问作者Robert
相关产品推荐
相关产品推荐

