1

I have a gitlab ci configuration that I want to run locally. I can see that I can do that using gitlab-runner docker exec in this answer: Use GitLab CI to run tests locally?. However, my gitlab configuration uses a default before_script, and when I run a job with gitlab-runner docker exec <job-id>, the default before_script doesn't get run.

Here is an example configuration that demonstrates the issue

image: openjdk:8

default:
  before_script:
    - echo TEST1
    - echo TEST1
    - echo TEST1
    - echo TEST1

test:
  script:
    - echo TEST3

I can get default before_script to run in the gitlab pipeline, just not locally. If I run gitlab-runner exec docker test with this configuration, only the TEST3 gets printed. How can I get the default before_script to run?

Matt
  • 2,232
  • 8
  • 35
  • 64
  • just a suggestion, what about adding `stages` after `image` define. in it `default,test` stages are defined. If it doesnt work, place `before_script` as a separate stage. – Sachith Muhandiram Apr 01 '21 at 23:16
  • 1
    @SachithMuhandiram sorry I can't understand your comment, can you clarify? – Matt Apr 02 '21 at 02:37

3 Answers3

1

It seems that the default: yml keyword is not currently supported by the gitlab-runner exec command: https://docs.gitlab.com/runner/commands/#limitations-of-gitlab-runner-exec. So there appears to be no way to do what I am trying to do.

There is apparently plans by Gitlab to fix this: https://gitlab.com/gitlab-org/gitlab-runner/-/issues/2797, but no timeline given for when/if this will be delivered.

Matt
  • 2,232
  • 8
  • 35
  • 64
0
image: openjdk:8

stages:
  - default
  - test

default:
  before_script:
    - echo TEST1
    - echo TEST1
    - echo TEST1
    - echo TEST1

test:
  script:
    - echo TEST3

or

before_script:
    - echo TEST1
    - echo TEST1
    - echo TEST1
    - echo TEST1

test:
  script:
    - echo TEST3
Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94
  • 1
    I think there's some confusion. `default:` is not the name of a job, it's the `default` keyword in gitlab: https://docs.gitlab.com/ee/ci/yaml/. It means that all jobs should inherit the before_script in it. Also, adding the `stages` section does not help because the only way I know of to run the docker executor locally is to run a specific job, which bypasses the `stages` section. Moving the `before_script` out of `default` works for this case, but would require me to make major changes to my gitlab yml which I'm trying to avoid. – Matt Apr 02 '21 at 14:55
0

As mentioned in this issue Local pipeline execution issue, the default keyword is deprecated, gitlab-runner not support it. Just write gloabl before_script like this:

stages:
  - test

before_script:
  - echo TEST1
  - echo TEST1
  - echo TEST1
  - echo TEST1

test:
  stage: test
  script:
    - echo TEST3

test with comamnd:

gitlab-runner exec docker --docker-image openjdk:11.0.16 --docker-pull-policy if-not-present test
wzjing
  • 11
  • 6