1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Gitlab CIでコロンの後ろにスペースが入ると怒られる( script config should be a string or a nested array of strings up to 10 levels deep )

Posted at

.gitlab-ci.yamlで以下の例は前者は成功しますが後者はyaml invalidが出て実行できません。

● 成功するパターン

stages:
  - test

test:
  stage: test
  script:
  - apk add jq curl
  - DOCKER_TOKEN=$(curl "https://auth.docker.io/token?service=registry.docker.io&scope=repository:ratelimitpreview/test:pull" | jq -r .token)
  - curl --head -H "Authorization:Bearer $DOCKER_TOKEN" https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/lates

● 失敗するパターン( script config should be a string or a nested array of strings up to 10 levels deep )

stages:
  - test

test:
  stage: test
  script:
  - apk add jq curl
  - DOCKER_TOKEN=$(curl "https://auth.docker.io/token?service=registry.docker.io&scope=repository:ratelimitpreview/test:pull" | jq -r .token)
  - curl --head -H "Authorization: Bearer $DOCKER_TOKEN" https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/lates

上記のコードはDockerhubのpull制限を確認するためAPIをたたくためスクリプトで、script:の部分をローカルで実行すればどちらも成功します。

これの違いはcurlコマンドのAuthorizationの後ろが前者がコロンだけ、後者がコロン+スペースであることだけです。yamlの場合、コロン+スペースがyaml構文のディクショナリーとなるのでscript中に同様のものがでるとうまく解析できないようです。

別解

コロン+スペースの形式でコマンドを実行させる方法として以下のように複数行に分けるときに使う|をつかってあげるとこれも成功します。

test3:
  stage: test
  script:
  - apt-get update && apt-get install -y jq curl
  - DOCKER_TOKEN=$(curl "https://auth.docker.io/token?service=registry.docker.io&scope=repository:ratelimitpreview/test:pull" | jq -r .token)
  - |
    curl --head -H "Authorization: Bearer $DOCKER_TOKEN" https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/lates
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?