Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set and access an environment variable in GitHub Actions?

I am in the process of automating my react-native Expo release-cycle. I am using release channels in Expo to build staging and production builds. For example, on every push to staging-v1 GitHub branch, the action below is triggered.

//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 }} holds the current branch name. So when I push my changes to staging-v1 this action runs. However, I am getting this error.

github-actions-error

I've tried setting env variables, it did not work also. I just want to append my branch_name to the expo publish command. Ultimately, when building, the run command should look like this.

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

Any insight into this problem will be greatly appreciated. Thanks :)

like image 935
Ajin Kabeer Avatar asked Jun 14 '20 05:06

Ajin Kabeer


2 Answers

I built a GitHub Action for this: FranzDiebold/github-env-vars-action

The usage is as follows:

steps:
  - uses: FranzDiebold/[email protected]
  - 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"

An example output is:

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

A demo for all Operating systems (Linux, macOS and Windows) is also available in the demo workflows file of the repository!

like image 155
Franz Diebold Avatar answered Oct 11 '22 22:10

Franz Diebold


After a lot of trial and error, I've found the solution. I used the github-slug-action workflow to access my 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/[email protected]
      - run: expo publish --release-channel=${{ env.GITHUB_REF_SLUG }}

Here is the log.

github-actions-log

like image 28
Ajin Kabeer Avatar answered Oct 12 '22 00:10

Ajin Kabeer