Search code examples
dockergithub-actions

Github actions workflow to docker container environment


I have the following environment variable set in GitHub workflow file (on the top level):

env: 
  GITHUB_RUN_NUMBER: ${github.run_number}

I would like to access it in my docker container (frontend-react) and log in console:

console.log(`Build number= ${process.env.GITHUB_RUN_NUMBER}`)

Currently, the GITHUB_RUN_NUMBER does not reach the container build in the workflow exec container env

How should I pass environment variables from workflow to container? Thanks!

name: CI_DEMO

on:

  workflow_dispatch
    
env: 
  GITHUB_RUN_NUMBER: ${github.run_number}
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:

  Test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '14.20.1'
          cache: 'npm'
      - run: npm ci
      - run: CI=true npm test -- --verbose --coverage --watchAll=false

  Build_Demo:
    runs-on: ubuntu-latest
    needs: [ Test ]
    steps:
      - uses: actions/checkout@v3
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID  }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: eu-central-1
      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1
      - name: Build and Push Docker Image
        env:
          CI_COMMIT_TAG: N-${{ github.run_number }}
        run: |
          sudo docker build -t $CI_REGISTRY/$CI_REGISTRY_REPOSITORY:$CI_COMMIT_TAG . \
            -f ./deploy/Dockerfile \
            --build-arg WEB_PRIVATE_KEY="$WEB_PRIVATE_KEY" \
            --build-arg REACT_APP_GA_TRACKING_ID=$REACT_APP_GA_TRACKING_ID \
            --build-arg 
          docker push $CI_REGISTRY/$CI_REGISTRY_REPOSITORY:$CI_COMMIT_TAG

  Deploy_Demo:
    runs-on: ubuntu-latest
    needs: [ Build_NIX_Demo ]
    steps:
      - uses: actions/checkout@v3
      - name: Preparing for deploy
        env:
          DEMO_PRIVATE_KEY: ${{ secrets.DEMO_PRIVATE_KEY }}
        run: |
          
      - name: Deploy
        env:
          
        run: |
          
          export TMP_AFRONTEND_VERSION=$CI_COMMIT_TAG
          ssh -o SendEnv=TMP_AFRONTEND_VERSION  $DEMO_IP "AFRONTEND_VERSION=$TMP_AFRONTEND_VERSION; unset AFRONTEND_VERSION; export AFRONTEND_VERSION; docker-compose -f $DEMO_PROJECT_PATH/docker-compose.yml --env-file $DEMO_PROJECT_PATH/.env up -d --no-deps a_frontend"

Solution

  • The missing step was defining the variable in Docker file as mentioned by aknosis