Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a variable with a list of files in a YAML file for Azure DevOps or GitHub

How to avoid duplication of file lists in Azure DevOps / GitHub pipelines?

Suppose I have the following YAML file:

name: Build

on:
  push:
    branches: [ master ]
    paths: 
      - 'SRC/define.inc'
      - 'SRC/SrvMain.pas'
      - 'SRC/Tiny.dpr' 
      - 'SRC/xBase.pas'

  pull_request:
    branches: [ master ]
    paths: 
      - 'SRC/define.inc'
      - 'SRC/SrvMain.pas'
      - 'SRC/Tiny.dpr' 
      - 'SRC/xBase.pas'

  workflow_dispatch:

jobs:

...skipped remaining lines...

(I'm using this pipeline for the TinyWeb repository on GitHub)

As you see, the list of files under the paths: section repeats for the "push" and "pull_request" sections. How can I define a list of files just once, so I would not need to copy it to each of the sections, but only add a reference to the list variable? I've tried to define the list using the variables: keyword and then reference the list as ${{ variables.my_variable_name }}, but it didn't work. I tried multiple variants to no avail.

Please give an example of the YAML file where I can define the list of files once and then use it from multiple sections under on:.

like image 825
Maxim Masiutin Avatar asked Jan 25 '23 06:01

Maxim Masiutin


2 Answers

Hi @Maxim Masiutin I have tried with both env and secrets but it didn't work out. You are asking for using one path for both push and pull_request events and it is not possible for now. There is no statements that shows supporting for variables at workflow level. You can check this for paths/paths_ignore.

However you are using both push and pull_request events exactly same I think you may use like below:

name: Build

on:
  push:
  pull_request:
    branches: [ master ]
    paths: 
      - 'SRC/define.inc'
      - 'SRC/SrvMain.pas'
      - 'SRC/Tiny.dpr' 
      - 'SRC/xBase.pas'

  workflow_dispatch:

jobs:

I hope this works for you.

like image 93
Muhammed Kılıç Avatar answered May 13 '23 17:05

Muhammed Kılıç


This syntax worked for me on Azure.

name: Build

variables:
  src_define: 'SRC/define.inc'
  src_srvmain: 'SRC/SrvMain.pas'
  src_tiny: 'SRC/Tiny.dpr'
  src_xbase: 'SRC/xBase.pas'

on:
  push:
    branches: [ master ]
    paths: 
      - variables['src_define']
      - variables['src_srvmain']
      - variables['src_tiny']
      - variables['src_xbase']

  pull_request:
    branches: [ master ]
    paths: 
      - variables['src_define']
      - variables['src_srvmain']
      - variables['src_tiny']
      - variables['src_xbase']

  workflow_dispatch:

jobs:
like image 39
Dragos Tanase Avatar answered May 13 '23 17:05

Dragos Tanase