如何在GitHub Actions中设置和访问环境变量?

6

我正在自动化我的react-native Expo发布周期。我在Expo中使用release channels来构建stagingproduction版本。例如,在每次推送到staging-v1 GitHub分支时,将触发下面的操作。

//staging.yaml

name: Release to staging
on:
  push:
    branches:
      - staging*
jobs:
  publish:
    name: Install and publish on staging channel
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v1
        with:
          node-version: 12.x
      - uses: expo/expo-github-action@v5
        with:
          expo-version: 3.x
          expo-username: ${{ secrets.EXPO_CLI_USERNAME }}
          expo-password: ${{ secrets.EXPO_CLI_PASSWORD }}
          expo-packager: npm
      - run: npm install
      - run: expo publish --release-channel ${{ GITHUB_REF }}

{{ GITHUB_REF }} 存储当前的分支名称。所以当我将更改推送到 staging-v1 时,会运行此操作。但是,我遇到了这个错误。

github-actions-error

我尝试设置 env 变量,但这也没有起作用。我只想将我的 branch_name 添加到 expo publish 命令中。最终,在构建时,run 命令应该是这样的。

 - run: npm install
 - run: expo publish --release-channel staging-v1

非常感激对这个问题的任何见解。谢谢 :)


这个问题的标题“如何在GitHub Actions中设置和访问工作流变量?”太过笼统。答案只涉及特定值;它并不涉及读写任意变量。 - Andreas Abel
2个回答

3
我为此创建了一个 GitHub 操作: FranzDiebold/github-env-vars-action 使用方法如下:
steps:
  - uses: FranzDiebold/github-env-vars-action@v1.2.0
  - name: Print environment variables
    run: |
      echo "GITHUB_REPOSITORY_SLUG=$GITHUB_REPOSITORY_SLUG"
      echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"
      echo "GITHUB_REPOSITORY_OWNER_SLUG=$GITHUB_REPOSITORY_OWNER_SLUG"
      echo "GITHUB_REPOSITORY_NAME=$GITHUB_REPOSITORY_NAME"
      echo "GITHUB_REPOSITORY_NAME_SLUG=$GITHUB_REPOSITORY_NAME_SLUG"
      echo "GITHUB_REF_SLUG=$GITHUB_REF_SLUG"
      echo "GITHUB_REF_NAME=$GITHUB_REF_NAME"
      echo "GITHUB_REF_NAME_SLUG=$GITHUB_REF_NAME_SLUG"
      echo "GITHUB_SHA_SHORT=$GITHUB_SHA_SHORT"

一个例子的输出是:
GITHUB_REPOSITORY_SLUG=ajinkabeer-test-repo
GITHUB_REPOSITORY_OWNER=ajinkabeer
GITHUB_REPOSITORY_OWNER_SLUG=ajinkabeer
GITHUB_REPOSITORY_NAME=test-repo
GITHUB_REPOSITORY_NAME_SLUG=test-repo
GITHUB_REF_SLUG=refs-heads-staging-v1
GITHUB_REF_NAME=staging-v1
GITHUB_REF_NAME_SLUG=staging-v1
GITHUB_SHA_SHORT=e2e4f0ab

所有操作系统(Linux、macOS和Windows)均可使用存储库的演示工作流文件进行演示!


谢谢你的回答。看起来不错,我一定会试一试。再次感谢 :) - Ajin Kabeer

1
经过许多尝试和错误,我找到了解决方法。我使用了github-slug-action workflow来访问我的branch_name
name: Release to staging
on:
  push:
    branches:
      - staging*
jobs:
  publish:
    name: Install and publish on staging channel
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v1
        with:
          node-version: 12.x
      - uses: expo/expo-github-action@v5
        with:
          expo-version: 3.x
          expo-username: ${{ secrets.EXPO_CLI_USERNAME }}
          expo-password: ${{ secrets.EXPO_CLI_PASSWORD }}
          expo-packager: npm
      - run: npm install
      - name: Run tests
        run: |
          npm test
      - uses: rlespinasse/github-slug-action@v2.x
      - run: expo publish --release-channel=${{ env.GITHUB_REF_SLUG }}

这里是日志。

github-actions-log


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接