首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在gitlab-ci脚本部分使用while循环

问如何在gitlab-ci脚本部分使用while循环
EN

Stack Overflow用户
提问于 2020-04-02 16:58:27
回答 2查看 7.3K关注 0票数 1

我试图迭代文件中的url条目,并将每个文件用作爬虫工具的输入。它的结果应该写到文件中。

这里是gitlab-ci.yml文件:

代码语言:javascript
复制
stages:
  - test
test:
  stage: test

  tags:
    - shell-docker
  script:
    - wget https://github.com/FaKeller/sireg/releases/download/v0.3.1/sireg-linux
    - chmod 775 sireg-linux
    - mkdir output
    - ls -alF
    - while read line; do
         echo $line;
         ./sireg-linux exec --loader-sitemap-sitemap \"$line\" >> ./output/${line##*/}_out.txt;
      done < sitemap-index
    - ls -alF output
  artifacts:
    paths:
      - output/*
    expire_in: 1 hrs

下面是站点地图索引文件(只有一个条目):

代码语言:javascript
复制
http://example.com/sitemap.xml

两个文件位于同一个目录中。我希望将一个文件sitemap.xml_out.txt写入输出文件夹(也是同一个目录)。我很确定./sireg脚本不会执行,因为它通常只需几分钟就能完成(在本地测试)。

舞台的输出如下:

代码语言:javascript
复制
2020-04-02 18:22:21 (4,26 MB/s) - »sireg-linux« saved [62566347/62566347]

$ chmod 775 sireg-linux
$ mkdir output
$ ls -alF
total 61128
drwxrwxr-x  4 gitlab-runner gitlab-runner     4096 Apr  2 18:22 ./
drwxrwxr-x 10 gitlab-runner gitlab-runner     4096 Apr  2 15:46 ../
drwxrwxr-x  5 gitlab-runner gitlab-runner     4096 Apr  2 18:22 .git/
-rw-rw-r--  1 gitlab-runner gitlab-runner      512 Apr  2 18:22 .gitlab-ci.yml
drwxrwxr-x  2 gitlab-runner gitlab-runner     4096 Apr  2 18:22 output/
-rw-rw-r--  1 gitlab-runner gitlab-runner       30 Apr  2 15:46 README.md
-rwxrwxr-x  1 gitlab-runner gitlab-runner 62566347 Nov 11  2017 sireg-linux*
-rw-rw-r--  1 gitlab-runner gitlab-runner       55 Apr  2 18:08 sitemap-index
$ while read line; do echo $line; ./sireg-linux **exec** --loader-sitemap-sitemap \"$line\" >> 
./output/${line##*/}_out.txt; done < sitemap-index
$ ls -alF output
total 8
drwxrwxr-x 2 gitlab-runner gitlab-runner 4096 Apr  2 18:22 ./
drwxrwxr-x 4 gitlab-runner gitlab-runner 4096 Apr  2 18:22 ../
Uploading artifacts...
Runtime platform                                    arch=amd64 os=linux pid=23813 revision=1f513601 version=11.10.1
WARNING: output/*: no matching files               
ERROR: No files to upload                          
Job succeeded

更新

尝试将所有步骤移动到单独的脚本中,但这也不起作用。

更新2

忘记在命令中添加exec:

代码语言:javascript
复制
./sireg-linux exec --loader-sitemap-sitemap \"$line\" >> 
./output/${line##*/}_out.txt;

不幸的是,这没有帮助。

我该怎么做才能让它发挥作用?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-03 13:23:29

尝试将./sireg-linux --loader-sitemap-sitemap \"$line\"更改为./sireg-linux exec --loader-sitemap-sitemap "$line"。希望这能有所帮助!

编辑:而且,看起来脚本根本没有进入while循环。也许文件站点地图索引是空的,或者它只有一行没有新行在末尾?

编辑2:命令行中的反斜杠是错误的.修正了我的答案

票数 2
EN

Stack Overflow用户

发布于 2020-04-02 19:17:57

当然,您可以在YAML中痛苦地调试多行命令。

您甚至可以使用YAML多行字符串:

但是我只需要将代码包装到一个shell脚本中,将其存储在同一个GitLab回购中,然后在.gitlab-ci.yml中调用它。

这样,您就可以在本地和CI中以完全相同的方式运行此脚本,这是连续交付的最佳实践。

代码语言:javascript
复制
    - ./script.sh
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60996973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档