Pavel Kucherenko
Pavel Kucherenko

Reputation: 35

How to extend template script?

I have the following template in my .gitlab-ci.yml file:

x-android-build-tools: &android_build_tools
  image: jangrewe/gitlab-ci-android
  stage: build
  script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - chmod +x ./gradlew
  artifacts:
    expire_in: 1 hours
    paths:
      - app/build/

I want to extend the script part to make actual builds. For example:

android-stage-build:
  <<: *android_build_tools
  environment: stage
  only:
    - dev
  after_script:
    - ./gradlew :app:assembleDebug

It works well, but it has a problem. Docker launches the both jobs instead of ignoring the template.

Is there way to run only the "android-stage-build" job which triggers the template job when it will be needed?

Upvotes: 1

Views: 2154

Answers (2)

In order to make gitlab ci ignore the first entry, you need to add a dot (.) in front of the definition.

.x-android-build-tools: &android_build_tools
  image: jangrewe/gitlab-ci-android
  stage: build
  script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - chmod +x ./gradlew
  artifacts:
    expire_in: 1 hours
    paths:
      - app/build/

android-stage-build:
  <<: *android_build_tools
  environment: stage
  only:
    - dev
  after_script:
    - ./gradlew :app:assembleDebug

Besides that, I think, based on what I read here, you don't want to use after_script.

I think you want to use the before_script in the template, and on the build stage specific the script-key. The main difference is that after_script also runs if the script fails. And by what I read here, it does not look like you would like that to happen.

Upvotes: 3

Yes. You're simply missing a . :)

See https://docs.gitlab.com/ee/ci/yaml/yaml_optimization.html#anchors

It should work if you write it like this:

.x-android-build-tools: &android_build_tools
  image: jangrewe/gitlab-ci-android
  stage: build
  script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - chmod +x ./gradlew
  artifacts:
    expire_in: 1 hours
    paths:
      - app/build/

android-stage-build:
  <<: *android_build_tools
  environment: stage
  only:
    - dev
  after_script:
    - ./gradlew :app:assembleDebug

Upvotes: 1

Related Questions