Search code examples
github-actions

github actions- if condition inside the for loop


I'm tring to run the below workflow using for loop with if condition inside, but the action is parsed with an error, what is my mistake?

workflow:

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

   

jobs:

  analysis:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Setup Cpp Check
      run: sudo apt-get install cppcheck
    - name: Run Static Analysis
      run: cppcheck . --force --error-exitcode=1
  unit_test:
  #  needs: Scan_changes
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Setup Ruby
      run: sudo apt-get install ruby
    - name: Setup Ceedling
      run: sudo gem install ceedling
    - name: Get changed files
      id: changed-files
      uses: tj-actions/changed-files@v34
      with:
        dir_names: true
        files: |
          code/**
    - name: Unit Test for Changed modules
      run: |
        for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
          if: contains('$file', 'SomeString')
          echo "$file was changed"
        done
    - name: Test
      run: |
        ceedling

and the output is enter image description here

So i'm not sure what is the propore syntex for the if condition inside the fore loop


Solution

  • is not valid bash script code. You should do something like:

        - name: Unit Test for Changed modules
          run: |
            for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
              if [[ $file =~ "SomeString" ]]; then
                echo "$file was changed"
              fi
            done