0

I have the following jobs:

.build-image:
  extends: .build-docker-image
  stage: build
  variables:
    COMPOSER_HOME_DIR: /kaniko/.composer
    BUILD_ARGS: >
      COMPOSER_HOME=${COMPOSER_HOME_DIR}
      COMPOSER_CACHE_READ_ONLY=1
    ADD_KANIKO_ARGS: >
      --reproducible=${REPRODUCIBLE},
      --skip-unused-stages=true,
      --target=${BUILD_TARGET}
    BUILD_ARGS: >
      BUILD_VERSION=${BUILD_VERSION},
    REPRODUCIBLE: "false"
  rules:
    - *push_to_main_branch
    - *merge_request
  id_tokens: !reference [ .gcp_jwt_token, id_tokens ]
  before_script:
    - mkdir -p $COMPOSER_HOME_DIR
    - echo "${COMPOSER_AUTH}" > $COMPOSER_HOME_DIR/auth.json

build-app-image:
  stage: release
  extends: .build-image
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
      when: on_success
    - if: '$CI_MERGE_REQUEST_ID'
      changes:
        - src/**/*
      when: on_success
  variables:
    BUILD_TARGET: production

  before_script:
    # What I would like to happen
    - export COMPOSER_LOCK_HASH=$(sha1sum composer.lock | cut -d' ' -f1)
    - export YARN_LOCK_HASH=$(sha1sum yarn.lock | cut -d' ' -f1)
    - export PREBUILT_APP_IMAGE="europe-docker.pkg.dev/${ORG_PROJECT}/blabla/blabla-deps:sha-${COMPOSER_LOCK_HASH}"
    - export PREBUILT_ASSETS_IMAGE="europe-docker.pkg.dev/${ORG_PROJECT}/mcp/blabla-other-deps:sha-${YARN_LOCK_HASH}"
    - export BUILD_ARGS="COMPOSER_HOME=${COMPOSER_HOME_DIR}, APP_IMAGE_REF=${PREBUILT_APP_IMAGE},ASSETS_IMAGE_REF=${PREBUILT_ASSETS_IMAGE}"
    - !reference [ ".build-image", "before_script" ]

See the comment in the 2nd job, in its before_script section. In practice I have a build-app-image that is extending a .build-image which on its own is extending other jobs defined company-wide. These are defined company-wide in order to have them reusable, and they use Kaniko to run the Dockerfile instructions. They accept a BUILD_ARGS variable that they use for the Dockerfile. My build-app-imageis a job in my repo and the logic I am trying to make is that I calculate on the fly some image tags and I would like these tags to be injected in theBUILD_ARGS, since BUILD_ARGSis the one that gets passed in the Dockerfile. However, if I definedBUILD_ARGSas a variable in mybuild-app-imageI cannot override its value on-the-fly through the logic in mybefore-script. Is there a way therefore to modify a variable on the fly dynamically? Export` won't work either because it declares the variable shell-wise and not Gitlab CI/CD wise.

1
  • This works exactly as you think it should. "export BUILD_ARGS=" in build-app-image's before_script sets the variable for .build-docker-images script section. Commented Oct 29 at 8:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.