如何在作业之间共享步骤

问题描述 投票:0回答:1

我有

azure-pipelines.yml
文件,其中包含 4 个作业:
Format_Checking
Unit_Tests
Cypress_Tests
Build

这些步骤在所有步骤中都是通用的:

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'

每当我想修改某些内容时,我都必须去修改所有内容。

  1. 有什么方法可以在作业之间重用相同的步骤吗?
  2. 我实际上可以让作业从同一点开始,以避免多次安装
    node_modules
    吗?
azure azure-pipelines
1个回答
0
投票

有什么方法可以在作业之间重用相同的步骤吗?

是的。我们可以在 YAML 模板中定义任务。

然后我们可以在作业之间重复使用相同的步骤。

例如:

模板.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

jobs:
- job: Format_Checking
  steps:
  - template: template.yml
  

- job: Unit_Tests
  steps:
  - template: template.yml

- job: Cypress_Tests
  steps:
  - template: template.yml

我实际上可以让作业从同一点开始,以避免多次安装node_modules吗?

每项工作都是独立的。恐怕我们无法让工作从同一点开始。

但是为了减少安装node_modules的时间,我们可以使用Cache任务

它可以缓存node_modules文件夹并重用管道缓存中的包。它将减少安装node_modules的时间。

或者您可以使用自托管代理并让作业在同一个代理上运行。然后他们可以在作业之间共享node_modules。

© www.soinside.com 2019 - 2024. All rights reserved.