Home > Software design >  Bash: How to execute paths
Bash: How to execute paths

Time:09-16

I have job in my gitlab-cicd.yml file:

unit_test:
  stage: test
  image: $MAVEN_IMAGE
  script:
    - *tests_variables_export
    - mvn ${MAVEN_CLI_OPTS} clean test
    - cat ${CI_PROJECT_DIR}/rest-service/target/site/jacoco/index.html
    - cat ${CI_PROJECT_DIR}/soap-service/target/site/jacoco/index.html
  artifacts:
    expose_as: 'code coverage'
    paths:
      - ${CI_PROJECT_DIR}/soap-service/target/surefire-reports/
      - ${CI_PROJECT_DIR}/rest-service/target/surefire-reports/
      - ${CI_PROJECT_DIR}/soap-service/target/site/jacoco/index.html
      - ${CI_PROJECT_DIR}/rest-service/target/site/jacoco/index.html

And I want to change it to this one:

unit_test:
  stage: test
  image: $MAVEN_IMAGE
  script:
    - *tests_variables_export
    - mvn ${MAVEN_CLI_OPTS} clean test
    - cat ${CI_PROJECT_DIR}/rest-service/target/site/jacoco/index.html
    - cat ${CI_PROJECT_DIR}/soap-service/target/site/jacoco/index.html
  artifacts:
    expose_as: 'code coverage'
    paths:
      - *resolve_paths

I try to use this bash script:

.resolve_paths: &resolve_paths |-
  if [ "${MODULE_FIRST}" != "UNKNOWN" ]; then
    echo "- ${CI_PROJECT_DIR}/${MODULE_FIRST}/target/surefire-reports/"
    echo "- ${CI_PROJECT_DIR}/${MODULE_FIRST}/target/site/jacoco/index.html"
  fi
  if [ "${MODULE_SECOND}" != "UNKNOWN" ]; then
    echo "- ${CI_PROJECT_DIR}/${MODULE_SECOND}/target/surefire-reports/"
    echo "- ${CI_PROJECT_DIR}/${MODULE_SECOND}/target/site/jacoco/index.html"
  fi

And right now I'm getting this error in pipeline:

WARNING: if [ "rest-service" != "UNKNOWN" ]; then\n echo "- /builds/minv/common/testcommons/taf-api-support/rest-service/target/surefire-reports/"\n echo "- /builds/minv/common/testcommons/taf-api-support/rest-service/target/site/jacoco/index.html"\nfi\nif [ "soap-service" != "UNKNOWN" ]; then\n echo "- /builds/minv/common/testcommons/taf-api-support/soap-service/target/surefire-reports/"\n echo "- /builds/minv/common/testcommons/taf-api-support/soap-service/target/site/jacoco/index.html"\nfi: no matching files ERROR: No files to upload

CodePudding user response:

Can I execute [sic] paths using bash script like this?

No, scripts cannot alter the current YAML, particularly not if you specify the script (which is just a string) in a place where it is interpreted as a path.

You could trigger a dynamically generated YAML:

generate:
  stage: build
  script:
    - |
      exec > generated.yml
      echo ".resolve_paths: &resolve_paths"
      for module in "${MODULE_FIRST}" "${MODULE_SECOND}"; do
        [[ "$module" = UNKNOWN ]] && continue
          echo "- ${CI_PROJECT_DIR}/${module}/target/surefire-reports/"
          echo "- ${CI_PROJECT_DIR}/${module}/target/site/jacoco/index.html"
      done
      sed '1,/^\.\.\. *$/d' "${CI_CONFIG_PATH}"
  artifacts:
    paths:
      - generated.yml

run:
  stage: deploy
  trigger:
    include:
      - artifact: generated.yml
        job: generate

...

# Start of actual CI. When this runs, there will be an
# auto-generated job `.resolve_paths: &resolve_paths`.
# Put the rest of your CI (e.g. `unit_test:`) here.

But there are so many extensions in GitLab's YAML that you likely will find a tremendously better solution, which depends on what you plan to do with .resolve_paths. Maybe have a look at

  • Related