0

I am trying to configure a pipeline in ADO yaml builds if a specific conditions are satisfies. below are the details

  1. I have shared templates in separate project from where the CI Pipeline exists. (lets say project 'A')
  2. CI Pipeline is present in a separate project.(lets say project 'B')

Issue : I would like to define a condition in the CI pipeline (in project 'B') which starts the build after validating if a specified templates from (project 'A') are present in the yaml file as a build step

divya29
  • 1
  • 2
  • How about the issue? Does the answer and the update below resolved your question, If yes, you could Accept it as an Answer , so it could help other community members who get the same issues and we could archive this thread, thanks – Felix Mar 12 '21 at 01:47
  • How about the issue? Does the answer below resolved your question, If yes, you could accept it as an answer, so it could help other community members who get the same issues and we could archive this thread, thanks. If not, please let us know if you would like further assistance – Felix Mar 29 '21 at 01:49

1 Answers1

0

Do you need to check if the templates files are exist or compare some codes in yaml files

In your current situation, we recommend you can try to make your templates as the template yaml file, and then we can use the resources, stages and pipeline conditions

Here is the example demo script:

resources:
  repositories:
    - repository: templates
      type: git
      name: Tech-Talk/template
      trigger:
        paths:
          include:
            - "temp.yaml"
        
trigger: none

# variables:
#   - name: Test
#     value: TestGroup

pool:
  vmImage: ubuntu-latest

stages:
- stage: A
  jobs:
  - job: A1
    steps:
     - checkout: templates
     - bash: ls $(System.DefaultWorkingDirectory)
     - bash: | 
        if [ -f temp.yaml ]; then  
        # this is used to check if the file is exist: if [ -f your-file-here ] 
          echo "##vso[task.setVariable variable=FILEEXISTS;isOutput=true]true"
        else
          echo "##vso[task.setVariable variable=FILEEXISTS;isOutput=true]false"
        fi
        
        # or on Windows:
        # - script: echo ##vso[task.setvariable variable=FILEEXISTS;isOutput=true]true
       name: printvar

- stage: B
  condition: eq(dependencies.A.outputs['A1.printvar.FILEEXISTS'], 'true')
  dependsOn: A
  jobs:
  - job: B1
    steps:
    - script: echo hello from Stage B
    
  # - template: temp.yaml@templates
  #   parameters:
  #     agent_pool_name: ''
  #     db_resource_path: $(System.DefaultWorkingDirectory)      
  #     variable_group: ${{variables.Test}}

Update:

And if you need to use the Windows agent, please refer this task:

- task: PowerShell@2
       name: printvar 
       inputs:
         targetType: 'inline'
         script: |
            $file = '$(System.DefaultWorkingDirectory)\temp.yaml'  
            if([System.IO.File]::Exists($file)){
                Write-Host "##vso[task.setvariable variable=FILEEXISTS;isOutput=true]true"
            }
            else{
                Write-Host "##vso[task.setvariable variable=FILEEXISTS;isOutput=true]false"
            }

Attach my test result:

enter image description here

Felix
  • 1,104
  • 3
  • 6
  • Thanks , the templates we have in other project are yaml based. I would like to check if that template is present in the pipeline yaml code or not since the templates are security scans and need to be present in every pipeline yaml build step. Is this the approach? – divya29 Mar 09 '21 at 00:20
  • We recommend you can try to use the 'Required template' in the environment(https://learn.microsoft.com/en-us/azure/devops/pipelines/process/approvals?view=azure-devops&tabs=check-pass#required-template). And in your current situation, we do not have the way to check the code in your yaml. We recommend you can make the templates as the Separate file. – Felix Mar 09 '21 at 08:58
  • I do not have to check code inside yaml template. But , I want to check if a particular shared template yaml file is present or not in a pipeline yaml file. if it is present then the trigger to run a pipeline should occur. – divya29 Mar 09 '21 at 17:53
  • How do I run below steps in windows – divya29 Mar 09 '21 at 20:45
  • How do I run below steps in windows for bash script ? steps: - checkout: templates - bash: ls $(System.DefaultWorkingDirectory) - bash: | if [ -f temp.yaml ]; then # this is used to check if the file is exist: if [ -f your-file-here ] echo "##vso[task.setVariable variable=FILEEXISTS;isOutput=true]true" else echo "##vso[task.setVariable variable=FILEEXISTS;isOutput=true]false" fi name: printvar – divya29 Mar 09 '21 at 20:47
  • While I run below , code I get error - script: | if [ -f abc.yml ]; then echo "##vso[task.setVariable variable=FILEEXISTS;isOutput=true]true" else echo "##vso[task.setVariable variable=FILEEXISTS;isOutput=true]false" fi name: printvar error is below -f was unexpected at this time. ##[error]Cmd.exe exited with code '255'. – divya29 Mar 09 '21 at 21:22
  • I update my answer with the new task, you can use the new task instead of the bash. – Felix Mar 11 '21 at 07:43
  • Thanks Yujun it worked but however, the second job is not running although I have defined - stage: B condition: eq(dependencies.A.outputs['A_CI.printvar.FILEEXISTS'], 'true') dependsOn: A pool: name: 'server' demands: - agent.name -equals windowsABC – divya29 Mar 15 '21 at 01:55
  • Please check your demands and make sure your agent 'windowsABC' is available. And also, you can use the windows hosted agent and my demo file to do the test. – Felix Mar 15 '21 at 02:02
  • And also, you can update your yaml file in your question, this will be easy to help me know your current issue. – Felix Mar 15 '21 at 02:03
  • I don't think this is writing or checking if a file exists - Write-Host "##vso[task.setvariable variable=FILEEXISTS;isOutput=true]true – divya29 Mar 15 '21 at 04:04
  • This is use to set the variable. "[System.IO.File]::Exists($file)" this is used to check if the file is exists – Felix Mar 15 '21 at 05:42
  • Please refer the new task script behind the Update on my answer. – Felix Mar 15 '21 at 05:42
  • Dose my answer and update help you to solve your issue/confuse, If yes, you could Accept it as an Answer , so it could help other community members who get the same issues and we could archive this thread, thanks – Felix Mar 17 '21 at 08:47
  • I did try to use powershell script which you have suggested and it worked. However, even when I comment the yaml file still the jobs are suceeded and this is because the repo being checked out is the first step. The $(System.DefaultWorkingDirectory) will contain the temp.yaml file throughtout even though we include trigger or do not. – divya29 Mar 18 '21 at 20:17
  • Thanks a lot @Yujun for the approach and I am working on condition logic to hide and test yaml file in the build steps. This is great start. Thanks you! – divya29 Mar 18 '21 at 20:17