Search code examples
testinggitlabcontinuous-integrationcommand-line-interfacegitlab-ci-runner

Any altenatives of "gitlab-runner exec docker <job-name>" to test CI/CD locally? I get "FATAL: Command exec not found."


I would like to test the .gitlab-ci.yml in local, Im following other posts like: Run tests locally gitlab, using the command: gitlab-runner exec docker <job-name>. Im using gitlab-runner 17.1.0 for my gitlab server in version 16.9.1, and this command was deprecated since 10 and its no longer avaible.

I know there are some unoficial options gitlab emulator for testing .gitlab-ci.yml locally since gitlab-runner -exec docker <job_name> is no longer working. I was wondering, does exist any better option, maybe official, for testing .gitlab-ci.yml locally?


Solution

  • for my project I just had to test very simple jobs such as installing, packaging and linting different projects in different languages. In order to test them locally, I ended up executing them in docker with the same image. For example, for the job of linting python in gitlab CI/CD:

    lint-before-merge:
      stage: linting
      image: python:3.12
      rules:
        - if: ($CI_PIPELINE_SOURCE == "merge_request_event" && 
                  ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop"|| $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"))
      script:
        - pip install flake8
        - flake8 . 
    

    I tested it with the docker:

    sudo docker run -it --rm --name my-running-script -w "/app" -v "$PWD":"/app" python:3.12 /bin/bash -c "pip install flake8 ; flake8 --exclude venv  ; echo "executed""
    

    Where $PWD is my project with its ".gitlab-ci.yml". This was the best and most trustable way to test simple jobs in local before uploading them. Obviusly, .venv is not excluded in CI since its not uploaded.

    If anyone finds a simplier, better and official way to test gitlab CI/CD jobs, I kindly ask to add an answer to this question.