github-actions

GitHub vars/secrets are defined but not found in workflows


I want to publish a built image to my Docker repository. The only issue is my GitHub secrets are not recognized in my workflow and I can't figure out why.

name: CI Pipeline

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
permissions:
  contents: read
jobs:
  docker:
    name: Docker Build & Push
    runs-on: ubuntu-latest
    steps:
      - name: Check secrets availability
        run: |
          echo "🔍 Checking secrets..."
          if [ -z "${{ vars.DOCKERHUB_USERNAME }}" ]; then
            echo "❌ DOCKERHUB_USERNAME is not set"
          else
            echo "✅ DOCKERHUB_USERNAME is available"
          fi
          if [ -z "${{ secrets.DOCKERHUB_TOKEN }}" ]; then
            echo "❌ DOCKERHUB_TOKEN is not set"
          else
            echo "✅ DOCKERHUB_TOKEN is available"
          fi

Result:

🔍 Checking secrets...
❌ DOCKERHUB_USERNAME is not set
❌ DOCKERHUB_TOKEN is not set
Error: Process completed with exit code 1.

Env config:

Environment list screen

Environment config screen


Solution

  • There are 2 issues with your workflow:

    1. Incorrectly accessing environment variable

      You've defined DOCKERHUB_USERNAME as an environment variable within your environment, but use the secrets context key to access it in your workflow. Use the vars key instead.

      So, instead of using

      secrets.DOCKERHUB_USERNAME
      

      you should use

      vars.DOCKERHUB_USERNAME
      

      to correctly access the environment variable.

    2. Environment not referenced by job

      You've created an Api environment, but didn't reference it in the job within your workflow. Without doing so, none of the values defined by the environment will be available to your workflow.

      You need to add the environment key under your job, in your case it would look like so:

      jobs:
        docker:
          environment: Api
          # ... existing contents