Using Variables and Secrets
Applicable Scenarios: When you need to use environment variables, configuration variables, secrets, or input parameters in a workflow and understand their scope and priority rules.
Prerequisites
- Required vars and secrets have been created in the AtomGit interface (Path: Project Settings → Action Secrets and Variables).
- Understand the differences between env, vars, secrets, and inputs.
Quick Example
name: ci-with-vars
on:
push:
branches:
- main
workflow_dispatch:
inputs:
environment:
description: "Deployment environment"
type: string
default: test
env:
APP_NAME: demo-app
jobs:
build:
runs-on: [ubuntu-latest, x64, small]
env:
BUILD_MODE: release
steps:
- uses: checkout
- name: Print variables
env:
STEP_VAR: step-level
run: |
echo "APP_NAME=$APP_NAME"
echo "BUILD_MODE=$BUILD_MODE"
echo "STEP_VAR=$STEP_VAR"
echo "CONFIG_VAR=${{ vars.DEPLOY_TARGET }}"
echo "SHA=${{ atomgit.sha }}"
- name: Use secret
env:
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
run: echo "password is masked"
Configuration Explanation
Four Types of Variables
| Type | Suitable for storing | Sensitive | Defined location | Reference method |
|---|---|---|---|---|
env | Temporary environment variables within the workflow | No | YAML file | $APP_NAME or ${{ env.APP_NAME }} |
vars | General configuration at repository/organization level | No | AtomGit interface | ${{ vars.DEPLOY_TARGET }} |
secrets | Passwords, tokens, private keys | Yes | AtomGit interface | ${{ secrets.REGISTRY_PASSWORD }} |
inputs | Inputs for workflow_dispatch / workflow_call | No | YAML file | ${{ inputs.environment }} |
env Environment Variables
env supports three levels of scope: Workflow level → Job level → Step level
env:
APP_NAME: demo-app # Workflow level, visible to all jobs and steps
jobs:
build:
env:
BUILD_MODE: release # Job level, visible to all steps within this job
steps:
- env:
STEP_VAR: step-level # Step level, visible only to this step
run: echo "$STEP_VAR"
Priority Rules: Step level > Job level > Workflow level. A step-level variable with the same name will override the job-level and workflow-level variables.
YAML reference vs Runner reference:
| Reference method | Syntax | Applicable scenario |
|---|---|---|
| YAML reference (expression) | ${{ env.APP_NAME }} | Used in YAML fields, replaced before the runner executes |
| Runner reference (environment variable) | $APP_NAME | Used in run commands, interpreted by the shell |
vars Configuration Variables
vars are created in the AtomGit interface and support repository-level and organization-level scope:
steps:
- name: Use config variable
run: echo "deploy to ${{ vars.DEPLOY_TARGET }}"
Creation path: Repository settings → Variables → New variable