在 gitlab-ci.yml 中运行多行脚本

fra*_*182 7 bash gitlab gitlab-ci

我在尝试根据本文档配置的管道时遇到困难 -链接

我正在尝试将示例管道代码集成到我当前的管道代码中,到目前为止我已经做到了:

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
Run Code Online (Sandbox Code Playgroud)

但是,运行时我在输出中收到此错误:

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 "you@example.com"
  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
Run Code Online (Sandbox Code Playgroud)

似乎它没有正确解析多行脚本,我无法弄清楚如何准确地使其工作。我还在链接页面上的原始脚本中看到,脚本的调用方式不同,说实话,我不完全理解他们如何调用脚本。

任何帮助都会很棒。

agi*_*lob 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
Run Code Online (Sandbox Code Playgroud)

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

  • 虽然这会让您在配置中得到漂亮的短行,但作业日志只会打印多行构造的第一行。您会收到一些通知,一些内容已被省略,例如“#collapsed multiline command”。这显然不利于故障排除或审计。 (5认同)
  • 你不需要反斜杠。 (3认同)