如何在GitHub动作中将工作目录添加到部署 [英] How to add working directory to deployment in GitHub actions

查看:119
本文介绍了如何在GitHub动作中将工作目录添加到部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近搬到了GitHub上,所以我想做的就是在完成推送后将我的react项目托管在firebase中.我在此CI/CD流程中使用了GitHub动作.这是我现在拥有的main.yml.

I recently moved into GitHub actions, so what I'm trying to do is host my react project in firebase when a push is done. And I used GitHub actions to this CI/CD process. And this is the main.yml that I have right now.

name: Build and Deploy
on:
  push:
    branches:
      - master

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@master
      - name: Install Dependencies
        working-directory: ./my-app
        run: npm install
      - name: Build
        working-directory: ./my-app
        run: npm run build

  deploy:
    name: Deploy
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@master      
      - name: Deploy to Firebase
        uses: w9jds/firebase-action@master
        with:
          args: deploy --only hosting
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}

在npm安装和项目构建时,我以某种方式设法设置了工作目录.但是在部署过程中,我会不断收到此错误,

And I somehow manage to set the working directory when npm installation and project building. But in deployment I'm keep getting this error,

所以我了解的是,由于工作目录问题而发生此错误.所以我当前的项目结构是这样的.

So what I have understood is, this error occurs due to the working directory problem. So my current project structure looks like this.

. (root of my GitHub repository)
└── my-app
    ├── firebase.json   <-- Git Hub action must point to this sub-dir
└── my-app-mobile
    ├── packages.json

那么我应该如何在Firebase部署过程中做到这一点?如果我对这个问题错了,那么问题和答案将是什么?看来我不能在uses:

So how should I do this within my firebase deployment process? If I'm wrong with the problem, What would be the problem and the answer? It seems I can't use working-directory: ./my-app with uses:

推荐答案

我查看了Firebase CLI的文档,但没有找到通过CLI参数设置firebase.json路径的任何方法.但是,存在一个用于存储根目录的环境变量.虽然它是在predeploypostdeploy钩子的上下文中,所以我不确定CLI是否会尊重它.

I looked at the documentation for the firebase CLI and didn't see any way to set the path to your firebase.json via a CLI parameter. There is, however, an environment variable that stores the root directory. It's in the context of predeploy and postdeploy hooks though, so I'm not sure if the CLI will respect it.

$ PROJECT_DIR —包含firebase.json的根目录

$PROJECT_DIR — The root directory containing firebase.json

https://firebase.google.com/docs/cli#environment_variables

您使用的w9jds/firebase-action只是CLI的包装.我不确定这是否行得通,但您可以尝试按以下方式设置项目目录.在单独的步骤中设置变量的原因是因为您无法在env节中求值表达式.有关更多详细信息,请参见此答案.像w9jds/firebase-action这样的容器操作将有权访问变量,而无需直接通过env传递变量.

The w9jds/firebase-action you are using is just a wrapper around the CLI. I'm not sure if this will work but you could try setting the project directory as follows. The reason the variable is set in a separate step is because you cannot evaluate expressions in env sections. See this answer for more detail. Container actions like w9jds/firebase-action will have access to the variable without passing it directly via env.

      - name: Set project dir environment var
        run: echo ::set-env name=PROJECT_DIR::"$GITHUB_WORKSPACE/my-app"
      - name: Deploy to Firebase
        uses: w9jds/firebase-action@master
        with:
          args: deploy --only hosting
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}

如果这不起作用,则可以选择派生w9jds/firebase-action并将PROJECT_PATH参数添加到entrypoint.sh脚本,如下所示: https://github.com/w9jds/firebase-action/blob/master/entrypoint.sh

If that doesn't work, an alternative is to fork w9jds/firebase-action and add a PROJECT_PATH parameter to the entrypoint.sh script here: https://github.com/w9jds/firebase-action/blob/master/entrypoint.sh

更新:我提出了PR PROJECT_PATH参数添加到w9jds/firebase-action.现在,您可以按以下方式使用操作.

Update: I raised a PR to add a PROJECT_PATH parameter to w9jds/firebase-action. You can now use the action as follows.

      - name: Deploy to Firebase
        uses: w9jds/firebase-action@master
        with:
          args: deploy --only hosting
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
          PROJECT_PATH: ./my-app

这篇关于如何在GitHub动作中将工作目录添加到部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆