6

I have the following Job to build Images in my gitlab-ci.yml

dockerize:
  stage: containerize
  before_script:
   - eval $($CONTEXT_SCRIPT_PATH)
  environment:
    name: $CONTEXT
    url: XXX
  image:
      name: gcr.io/kaniko-project/executor:debug-v0.23.0
      entrypoint: [""]  
  script:
      - echo "{\"auths\":{\"$CI_DOCKER_REGISTRY_URL\":{\"username\":\"$CI_DOCKER_REGISTRY_USER\",\"password\":\"$CI_DOCKER_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
      - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_DOCKER_REGISTRY_URL/portal/frontend:$CONTEXT-$CI_PIPELINE_IID --build-arg VERSION_TAG=$CONTEXT-$CI_PIPELINE_IID --build-arg COMMIT_TIME=$COMMIT_TIME
      - echo "Pushed to Registry - $CI_DOCKER_REGISTRY_URL/portal/frontend:$CONTEXT-$CI_PIPELINE_IID"
  retry: 2

In the before_script section the env $CONTEXT gets set. $CONTEXT_SCRIPT_PATH is set in a global variables section:

variables:
  CONTEXT_SCRIPT_PATH: "${CI_PROJECT_DIR}/Scripts/get_context.sh"

But if the Job is running, it can't find the script.

/busybox/sh: eval: line 90: /builds/portal/portal-frontend/Scripts/get_context.sh: not found

It works in other Jobs, so is Kaniko running in some separate environment? How do I specify the right Path?

1 Answers1

0

Sorry for the late response, but for anyone who stumbles across this:

The script file must already exist in the current Job.

Either:

  • It exists in the project git repository and is automatically added by git clone before each job
  • It is manually created during the job - echo 'my script stuff' > $CONTEXT_SCRIPT_PATH
  • By default track all untracked files as artifacts - default: { artifacts: untracked } }
  • Or included as an artifact:
---
default: { variables: { CONTEXT_SCRIPT_PATH: "${CI_PROJECT_DIR}/Scripts/get_context.sh" } }

build-first: 
  stage: build
  artifacts: { paths: "$CONTEXT_SCRIPT_PATH" }  # tell gitlab to artifact the script

build-second:
  stage: build
  dependencies: [ build-first ]  # run this stage after build-first succeeded
  before_script: [ "eval $($CONTEXT_SCRIPT_PATH)" ]

NOTES:

  • global sections are deprecated- use default and put variables within, as shown above
  • If artifacts from a previous job in the same stage are required, use dependencies and reference the required stage
  • If you do not use dependencies, all artifacts from previous stages are passed to each job

References (GitLab): CI Keyword Reference Artifacts, Dependencies, Default

Joseph Riopelle
  • 121
  • 1
  • 6