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

2021-02-20 15:10:24 +08:00
 xiaohantx

想问下为什么会

$ 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
1850 次点击
所在节点    问与答
29 条回复
br_wang
2021-02-20 15:20:34 +08:00
你再 node -v 一下,看看是不是 node 也没有。
感觉是镜像那里,为啥要 default 阿?不是直接 image 就可以?
xiaohantx
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
2021-02-20 15:23:42 +08:00
执行指令的用户是 gitlab-runner,盲猜你的 npm 是在当前用户 home 目录下,比如我用的 nvm 管理 node 就在 /home/dev/.nvm 下面。可以用 whereis npm 看看 npm 究竟在什么目录下。
或者也可能是没有访问权限。
xiaohantx
2021-02-20 15:23:42 +08:00
@br_wang 是的 node 也没有= =
xiaohantx
2021-02-20 15:24:41 +08:00
@chenluo0429 是要联系管理员在当前用户配置环境嘛,我以为会自动拉 docker 镜像然后里面有 node...和 npm
br_wang
2021-02-20 15:25:16 +08:00
@xiaohantx 所以命令执行时就没使用 node 这个镜像…… 把 default 去掉应该就好了。
xiaohantx
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
2021-02-20 15:35:57 +08:00
你可以在执行 npm 之前打印一下 whoami,肯定不对
xiaohantx
2021-02-20 15:38:11 +08:00
@wxsm 输出的是 gitlab-runner
wxsm
2021-02-20 15:48:01 +08:00
@xiaohantx 你这个 runner 确定是以 docker 模式运行的吗,确定不是配了 shell 模式吗
AngryPanda
2021-02-20 15:51:19 +08:00
npm 在 path 下面吗?或者直接写绝对路径
xiaohantx
2021-02-20 15:53:15 +08:00
@AngryPanda 用 which 找,找不到
xiaohantx
2021-02-20 15:53:50 +08:00
@wxsm 这两个是有区别的嘛....我不确定因为我看大部分文档没有提到有 docker 和 shell 两种模式都是直接的 docker
xiaohantx
2021-02-20 15:54:07 +08:00
@AngryPanda node 都没有目前来看
xiaohantx
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
2021-02-20 16:09:17 +08:00
@xiaohantx 你可以看一下 CI 的执行日志,开头几行会有类似 `Using Shell executor` 的输出,如果是 Shell 说明配错了,你在 yarml 里面指定了 image 没有用,它会直接在宿主机上执行。如果是 Using Docker executor,那才是对的。
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
2021-02-20 16:10:53 +08:00
@wxsm 确实,这里显示了 Using Shell executor...
xiaohantx
2021-02-20 16:12:14 +08:00
@wxsm 那请问下 shell 的话我可以通过 yml 配置环境嘛,我看后端的 java 好像是程序打包起来
wxsm
2021-02-20 16:13:03 +08:00
@xiaohantx Shell 模式写 image 没有用,会被忽略。这个需要重新配置一下 runner,或者放弃使用 docker,在 runner 宿主机上安装 nodejs,两种方式都行。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/754621

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX