Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Gitlab CI variables and script section give different results

I’ve searched a lot of examples but they did not work for me.

I’m trying to run linters for changed files when MR is opened.

My .gitlab-ci.yml

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

run_linters:
  image: python:3
  variables:
    FILES: git diff --name-only $CI_MERGE_REQUEST_TARGET_BRANCH_NAME | grep *.py
  before_script:
    - python3 -m pip install black==21.5b1
    - python3 -m pip install flake8==3.9.2
  script:
    - echo $FILES
    - git diff --name-only $CI_MERGE_REQUEST_TARGET_BRANCH_NAME | grep *.py
    - black --check $FILES
    - flake8 $FILES
  only:
    - merge_requests

And I’m getting strange output.

echo $FILES says git diff --name-only main | grep incoming_file.py

incoming_file.py is the only file in that MR. Why is it around grep?

And git diff at script section says fatal: ambiguous argument 'main': unknown revision or path not in the working tree.

  1. Why is filename present around grep?
  2. Why are same git diff commands give different result?

>Solution :

Why is filename present around grep?

In bash when you refer to * this will expand and try to match the files/directories present in your current path, in your case since only the incoming_file.py is present, so it expands to this.

Why are same git diff commands give different result?

variables:
  FILES: git diff --name-only $CI_MERGE_REQUEST_TARGET_BRANCH_NAME | grep *.py

When you define a variable in variables section, Gitlab doesnt execute the command, it simple populates the variable FILES with the string git diff --name-only $CI_MERGE_REQUEST_TARGET_BRANCH_NAME | grep *.py

Then in the script section, the runner expands *.py to incoming_file.py and $CI_MERGE_REQUEST_TARGET_BRANCH_NAME to main

that’s why in echo you see git diff --name-only main | grep incoming_file.py

Here

 - git diff --name-only $CI_MERGE_REQUEST_TARGET_BRANCH_NAME | grep *.py

You actually execute the command and you get the mentioned message

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading