Run multi line script in gitlab-ci.yml
Asked Answered
U

2

9

I am having difficulty with my pipeline I'm trying to configured, based off of this document - link

I am trying to integrate the example pipeline code into my current one, I have got this far:

default:
  image:
    name: hashicorp/terraform:light
    entrypoint:
      - /usr/bin/env
      - "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

stages:
  - validate
  - yor


validate:
  stage: validate
  before_script:
    - rm -rf .terraform
    - terraform --version
    - terraform init
    - export AWS_DEFAULT_REGION=eu-west-1
  script:
    - terraform validate -json
  only:
    - merge_requests    

run-yor:
  stage: yor
  image: ruby:2.5
  script:
    - git checkout ${CI_COMMIT_SHA}
    - export YOR_VERSION=0.1.62
    - wget -q -O - https://github.com/bridgecrewio/yor/releases/download/${YOR_VERSION}/yor-${YOR_VERSION}-linux-amd64.tar.gz | tar -xvz -C /tmp
    - /tmp/yor tag -d .
  after_script:
    - |
      cd $CI_PROJECT_DIR
      git status
      lines=$(git status -s | wc -l)
      if [ $lines -gt 0 ];then
        echo "committing"
        git config --global user.name "$AUTO_COMMITTER_NAME"
        git config --global user.email "$AUTO_COMMITTER_EMAIL"
        echo ".yor_plugins" >> .gitignore
        git add .
        git commit -m "YOR: Auto add/update yor.io tags."
        git push -o ci.skip "https://${GITLAB_USER_NAME}:${GIT_PUSH_TOKEN}@${CI_REPOSITORY_URL#*@}"
      else
        echo "no updated resources, nothing to commit."
      fi
  only:
    - merge_requests

However, when running I receive this error in the output:

Running after_script
00:01
Running after script...
$ cd $CI_PROJECT_DIR # collapsed multi-line command
HEAD detached at c839d93
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
    modified:   cloudformation.tf
no changes added to commit (use "git add" and/or "git commit -a")
committing
*** Please tell me who you are.
Run
  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: empty ident name (for <>) not allowed

It seems like it is not parsing the multi line script correctly and I cannot figure out how exactly to get this working. I also see in the original script on the linked page, the script is called differently, I'll be honest I don't fully understand how they are calling the script.

Any help would be great.

Unhallowed answered 28/7, 2021 at 16:26 Comment(4)
Not sure if the problem is with multiline. By the way, take a look at this post to understand yaml + multilines: #3790954Zilla
Why not just putting all that stuff into a script file and call this from after_script?Thymol
Also, did you actually define any value for AUTO_COMMITTER_NAME and AUTO_COMMITTER_EMAIL?Thymol
@Thymol yes the variables are set in the repos settings. I will try setting this script in a separate script and call it that way instead, I just followed the instructions in the link I shared. I'm starting to think the issue is not the multi line script but it is something else potentially the commit it's trying to check out.Unhallowed
C
8
deploy:native-application:
  stage: deploy
  script:
    - |
      mvn $MAVEN_CLI_OPTS package -DskipTests \
      -Pnative \
      -Dquarkus.native.builder-image=quay.io/quarkus/ubi-quarkus-mandrel:21.3.0.0-Final-java17 \
      -Dquarkus.container-image.push=true

https://docs.gitlab.com/ee/ci/yaml/script.html#split-long-commands

Causerie answered 29/1, 2022 at 13:7 Comment(2)
you don't need backslashes.Hardpan
While that gets you nice, short lines in your config, the job-log will only print the first line of your multi-line construct. You will get some notice, that things have been omitted like # collapsed multiline command. Which obviously can be bad for troubleshooting or auditing.Tooling

© 2022 - 2024 — McMap. All rights reserved.