Env variables set in GitHub Actions workflow not accessible in AWS lambda project

660 Views Asked by At

I have a Chalice (AWS lambda Python framework) project the following CI/CD GitHub Action workflow:

name: Production Workflow

on:
  push:
    branches:
      - "main"

env:
  REPO: ${{ github.repository }}
  GITHUB_REF_NAME: ${{ github.ref_name }}
  GITHUB_SHA: ${{ github.sha }}

jobs:
  production:
    name: Deploy production
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v1
        with:
          python-version: "3.9"

      - name: Install requirements
        run: pip3 install -r requirements.txt

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-region: ${{ secrets.AWS_REGION }}
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

      - name: Check branch
        run: echo "${{ env.GITHUB_REF_NAME }}"

      - name: Check branch
        run: echo "${{ env.GITHUB_SHA }}"

      - name: Run tests
        run: python3 -m unittest discover -s tests

      - name: Deploy with Chalice
        run: chalice deploy --stage=production

However, from inside the project, the env variables REPO, GITHUB_REF_NAME and GITHUB_SHA are not accessible (i.e. os.environ.get("GITHUB_REF_NAME", None)). Why?

I also tried setting the env variables not globally, but in the "Deploy with Chalice" step only, with the same result. Also, I can successfully see the branch and commit ID written in GitHub Actions by the "Check branch" and "Check branch" steps.

Other env variables that are set in the Chalice config file .chalice/config.json are accessible.

1

There are 1 best solutions below

1
Grzegorz Krukowski On

You need to set up the environment and explicitly list all ENV variables you want to use, like this:

- name: Deploy with Chalice
  run: chalice deploy --stage=production
  env:
     GITHUB_REF_NAME: ${{ secrets.GITHUB_REF_NAME }} 
     GITHUB_SHA: ${{ secrets.GITHUB_SHA }}