Search code examples
gitlabyamlgitlab-cipipelinegitlab-ci.yml

How to insert a variable in a folded string in yaml?


How do I insert a variable in my .gitlab-ci.yml?

I would like to insert it in the after_script section so I can make a webhook call with the link of the artifact, that is going to be generated with this pipeline.

I tried the following, but it just reads it as a string.

stages:
- build

flutter_build_android:
  timeout: 1h
  stage: build
  before_script:
    - flutter clean
    - flutter pub get
  script:
    - flutter build apk --dart-define=SDK_REGISTRY_TOKEN="${MAPBOX_TOKEN}"
    - cp build/app/outputs/apk/release/app-release.apk ./
  artifacts:
    paths:
      - ./app-release.apk
    when: on_success
    expire_in: 30 days
  after_script:
    - >
      curl -H 'Content-Type: application/json' -d '{"text": "http://gitlab.company.pl/mobile/flutter/myproject/-/jobs/artifacts/$CI_JOB_ID/download?job=$CI_JOB_NAME"}' https://link.to.my/webhook/eca8
  tags:
    - gradle
    - flutter
  rules:
    - if: '$CI_COMMIT_BRANCH == "rc"'

Solution

  • Bash doesn't expand variables inside single quotes. You can end the single quotes before the variable and continue afterwards:

        - >
          curl -H 'Content-Type: application/json'
          -d '{"text": "http://gitlab.company.pl/mobile/flutter/myproject/-/jobs/artifacts/'"$CI_JOB_ID"'/download?job='"$CI_JOB_NAME"'"}'
          https://link.to.my/webhook/eca8
    

    (I put the variable in double quotes instead which shouldn't be necessary in this case but is generally good practice.)