V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
xiaohantx
V2EX  ›  问与答

ci/cd 前端自动化部署的一个问题

  •  1
     
  •   xiaohantx · 2021-02-20 15:10:24 +08:00 · 1841 次点击
    这是一个创建于 1158 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想问下为什么会

    $ echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/gitlab-runner/.local/bin:/home/gitlab-runner/bin
    $ npm install --no-optional --registry=https://registry.npm.taobao.org
    bash: line 85: npm: command not found
    ERROR: Job failed: exit status 1
    

    npm 找不到是为什么

    default:
      image: node:latest
    
    stages:
      - lint
    
    cache:
        paths:
            - node_modules/
    
    test:
      stage: lint
      before_script:
        - echo $PATH
      script:
        - npm install --no-optional --registry=https://registry.npm.taobao.org
        - npm run lint
      only:
        - master
        - dev
    
    29 条回复    2021-02-20 16:58:19 +08:00
    br_wang
        1
    br_wang  
       2021-02-20 15:20:34 +08:00
    你再 node -v 一下,看看是不是 node 也没有。
    感觉是镜像那里,为啥要 default 阿?不是直接 image 就可以?
    xiaohantx
        2
    xiaohantx  
    OP
       2021-02-20 15:22:50 +08:00
    @br_wang 是可以,我原来写的就是直接
    ```
    image: node:latest

    stages:
    - lint

    cache:
    paths:
    - node_modules/

    test:
    stage: lint
    before_script:
    - echo $PATH
    script:
    - npm install --no-optional --registry=https://registry.npm.taobao.org
    - npm run lint
    only:
    - master
    - dev
    ```

    但是也不行所以就试试- -忘记删了。。docker 镜像里应该有环境的呀。。
    chenluo0429
        3
    chenluo0429  
       2021-02-20 15:23:42 +08:00
    执行指令的用户是 gitlab-runner,盲猜你的 npm 是在当前用户 home 目录下,比如我用的 nvm 管理 node 就在 /home/dev/.nvm 下面。可以用 whereis npm 看看 npm 究竟在什么目录下。
    或者也可能是没有访问权限。
    xiaohantx
        4
    xiaohantx  
    OP
       2021-02-20 15:23:42 +08:00
    @br_wang 是的 node 也没有= =
    xiaohantx
        5
    xiaohantx  
    OP
       2021-02-20 15:24:41 +08:00
    @chenluo0429 是要联系管理员在当前用户配置环境嘛,我以为会自动拉 docker 镜像然后里面有 node...和 npm
    br_wang
        6
    br_wang  
       2021-02-20 15:25:16 +08:00
    @xiaohantx 所以命令执行时就没使用 node 这个镜像…… 把 default 去掉应该就好了。
    xiaohantx
        7
    xiaohantx  
    OP
       2021-02-20 15:25:48 +08:00
    @br_wang 现在写的是
    ```
    image: node:latest

    stages:
    - lint

    cache:
    paths:
    - node_modules/

    test:
    stage: lint
    before_script:
    - node -v
    script:
    - npm run lint
    only:
    - master
    - dev
    ```
    wxsm
        8
    wxsm  
       2021-02-20 15:35:57 +08:00
    你可以在执行 npm 之前打印一下 whoami,肯定不对
    xiaohantx
        9
    xiaohantx  
    OP
       2021-02-20 15:38:11 +08:00
    @wxsm 输出的是 gitlab-runner
    wxsm
        10
    wxsm  
       2021-02-20 15:48:01 +08:00 via iPhone
    @xiaohantx 你这个 runner 确定是以 docker 模式运行的吗,确定不是配了 shell 模式吗
    AngryPanda
        11
    AngryPanda  
       2021-02-20 15:51:19 +08:00 via iPhone
    npm 在 path 下面吗?或者直接写绝对路径
    xiaohantx
        12
    xiaohantx  
    OP
       2021-02-20 15:53:15 +08:00
    @AngryPanda 用 which 找,找不到
    xiaohantx
        13
    xiaohantx  
    OP
       2021-02-20 15:53:50 +08:00
    @wxsm 这两个是有区别的嘛....我不确定因为我看大部分文档没有提到有 docker 和 shell 两种模式都是直接的 docker
    xiaohantx
        14
    xiaohantx  
    OP
       2021-02-20 15:54:07 +08:00
    @AngryPanda node 都没有目前来看
    xiaohantx
        15
    xiaohantx  
    OP
       2021-02-20 16:04:54 +08:00
    @wxsm 看后端的打包命令有这个

    ```
    docker-build:
    stage: package
    dependencies:
    - maven-build
    script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build -t $CI_REGISTRY_IMAGE:latest .
    - docker push $CI_REGISTRY_IMAGE:latest
    only:
    - develop

    ```
    wxsm
        16
    wxsm  
       2021-02-20 16:09:17 +08:00
    @xiaohantx 你可以看一下 CI 的执行日志,开头几行会有类似 `Using Shell executor` 的输出,如果是 Shell 说明配错了,你在 yarml 里面指定了 image 没有用,它会直接在宿主机上执行。如果是 Using Docker executor,那才是对的。
    mitsuizzz
        17
    mitsuizzz  
       2021-02-20 16:10:10 +08:00
    把 images 移动到 test 里试试

    test:
    stage: lint
    image: node:12-alpine
    before_script:
    - echo $PATH
    script:
    - npm install --no-optional --registry=https://registry.npm.taobao.org
    - npm run lint
    only:
    - master
    - dev
    xiaohantx
        18
    xiaohantx  
    OP
       2021-02-20 16:10:53 +08:00
    @wxsm 确实,这里显示了 Using Shell executor...
    xiaohantx
        19
    xiaohantx  
    OP
       2021-02-20 16:12:14 +08:00
    @wxsm 那请问下 shell 的话我可以通过 yml 配置环境嘛,我看后端的 java 好像是程序打包起来
    wxsm
        20
    wxsm  
       2021-02-20 16:13:03 +08:00
    @xiaohantx Shell 模式写 image 没有用,会被忽略。这个需要重新配置一下 runner,或者放弃使用 docker,在 runner 宿主机上安装 nodejs,两种方式都行。
    xiaohantx
        21
    xiaohantx  
    OP
       2021-02-20 16:20:00 +08:00
    @wxsm 另外想麻烦请教下,看后端部署文件有个 docker-build,这个是干什么用的~
    xiaohantx
        22
    xiaohantx  
    OP
       2021-02-20 16:20:32 +08:00
    @mitsuizzz 因为 shell 原因楼上说 images 会不生效
    nano91
        23
    nano91  
       2021-02-20 16:21:48 +08:00
    whereis 看看
    wxsm
        24
    wxsm  
       2021-02-20 16:27:08 +08:00
    @xiaohantx 你贴的 docker-build 也是个 CI 任务,估计是你们后端用来打包上传镜像用于部署的,跟 runner 没有关系
    xiaohantx
        25
    xiaohantx  
    OP
       2021-02-20 16:38:39 +08:00
    @nano91 上面已经看啦,no npm
    xiaohantx
        26
    xiaohantx  
    OP
       2021-02-20 16:38:46 +08:00
    @xiaohantx 好的感谢
    mitsuizzz
        27
    mitsuizzz  
       2021-02-20 16:51:06 +08:00
    @xiaohantx 你这个得修改 runner 配置了
    [[runners]]
    name = "xxxxx"
    url = "xxxxx"
    token = "xxxxx"
    executor = "docker"

    你这里的执行器应该是 shell 了
    xiaohantx
        28
    xiaohantx  
    OP
       2021-02-20 16:52:41 +08:00
    @mitsuizzz 好的感谢,如果 shell 模式下,不在服务器安装 nodejs 是不是没啥办法了-v-
    mitsuizzz
        29
    mitsuizzz  
       2021-02-20 16:58:19 +08:00
    @xiaohantx 是的 建议还是重新配置下 runner
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5361 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:07 · PVG 15:07 · LAX 00:07 · JFK 03:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.