w9jds / firebase-action

GitHub Action for interacting with Firebase
MIT License
927 stars 200 forks source link

Unable to set environment variables from GitHub Actions #105

Closed irby closed 3 years ago

irby commented 3 years ago

Hi all,

This issue is building off the commented in Issue #86

For some reason, I am unable to update my firebase function environment variables the YAML pipeline.

Here's my code:

jobs:
  deploy_function:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v2
    # Runs a single command using the runners shell
    # And of course we need to goto our functions folder to deploy
    - name: Install npm packages
      run: |
        cd functions
        npm install
    # Switch the firebase CLI to my project
    - name: Switch environment
      uses: w9jds/firebase-action@master
      with:
        args: use
      env:
        FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
        PROJECT_ID: my-project
    # Set the environment variables on my project
    - name: Set environment variables
      uses: w9jds/firebase-action@master
      with:
        args: functions:config:set ${{ secrets.FIREBASE_FUNCTIONS_ENV_VARIABLES }}
      env:
        FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
        PROJECT_ID: my-project
    # Deploy the function to my-project
    - name: Deploy to Firebase
      uses: w9jds/firebase-action@master
      with:
        args: deploy --only functions
      env:
        FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
        PROJECT_ID: my-project

I know it's kind of redundant to use the firebase use in the steps, but I figured it may have been the reason why my environment variables weren't setting. However, I was expecting that the Set environment variables step would have updated the environment variables within the my-project scope, but when I check out the .runtimeconfig.json within Google Cloud Functions, it's not any different than its value from my local environment.

I've tried doing chaining the following, but it errors due to the Firebase CLI not being able to deploy after setting the environment variables:

# Deploy the function to my-project
    - name: Deploy to Firebase
      uses: w9jds/firebase-action@master
      with:
        args: functions:config:set ${{ secrets.FIREBASE_FUNCTIONS_ENV_VARIABLES }} deploy --only functions
      env:
        FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
        PROJECT_ID: my-project

Also, doing the following doesn't work either as it doesn't recognize the '&&' operand:

# Deploy the function to my-project
    - name: Deploy to Firebase
      uses: w9jds/firebase-action@master
      with:
        args: functions:config:set ${{ secrets.FIREBASE_FUNCTIONS_ENV_VARIABLES }} && firebase deploy --only functions
      env:
        FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
        PROJECT_ID: my-project

Is there a step to replacing the environment variables within the YAML code for my Firebase function? I feel like there is, I'm just missing where it would be done.

irby commented 3 years ago

Figured out the solution. The reason was that while using the '&&' operator in the shell script isn't allowed, you can use a semicolon ';' and follow up the command this way. This will simultaneously set the environment variables and deploy the function

jobs:
deploy_function:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v2
    # Runs a single command using the runners shell
    # And of course we need to goto our functions folder to deploy
    - name: Install npm packages
      run: |
        cd functions
        npm install
    # Update configs and deploy function to Firebase
    - name: Deploy to Firebase
      uses: w9jds/firebase-action@master
      with:
        args: functions:config:set ${{ secrets.FIREBASE_FUNCTIONS_ENV_VARIABLES }} ; firebase deploy --only functions
      env:
        FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
        PROJECT_ID: my-project