-
Select Topic AreaQuestion BodyWhen I write a reusable workflow, I can set environment variables that are available to the action templating engine under a top-level # .github/workflows/docker-build.yml
name: "Docker Build"
on:
workflow_call:
inputs:
tag_name:
description: "Docker tag to publish"
type: string
required: false
env:
IMAGE_TAG: "my-docker-registry.example.com/my-images:${{ inputs.tag }}"
jobs:
docker_build:
runs-on: [self-hosted]
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Build Image
uses: docker/build-push-action@v3
with:
tags: ${{ env.IMAGE_TAG }} However, I'm struggling to figure out how to do this in a composite action. Putting an # /docker-build/action.yml
name: "Docker Build"
inputs:
tag_name:
description: "Docker tag to publish"
type: string
required: false
env:
IMAGE_TAG: "my-docker-registry.example.com/my-images:${{ inputs.tag }}"
runs:
using: "composite"
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Build Image
uses: docker/build-push-action@v3
with:
tags: ${{ env.IMAGE_TAG }}
Moving # /docker-build/action.yml
...
runs:
using: "composite"
env:
IMAGE_TAG: "my-docker-registry.example.com/my-images:${{ inputs.tag }}"
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Build Image
uses: docker/build-push-action@v3
with:
tags: ${{ env.IMAGE_TAG }}
Is this possible? Do I have a syntax error? Can I build template variables in a composite action? Or do I have to write those values out longhand everywhere I want to use them? Note that I'm not trying to set shell environment variables. I need variables that I can pass as inputs to other actions in Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 12 comments 15 replies
-
@chrispat The question is valid! Setting environment variables in |
Beta Was this translation helpful? Give feedback.
-
I have the same question ✋ - Trying to set NPM_TOKEN for one of our customs actions and wanted to define it for every step |
Beta Was this translation helpful? Give feedback.
-
The composite action should have the environment variable available by default if it's defined in the calling workflow. But I'm also getting the same issue regardless of whether I pass in the environment variable or not! |
Beta Was this translation helpful? Give feedback.
-
I found a partial work around. Still seems to be a bug or at least an unfortunate design pattern. In a step before the uses call save the environment variable to $GITHUB_ENV
In a subsequent step you can pass it as one of the variables named using the with parameter
Then in the action's action.yml of the composite script you can use it as any
I swear this YML syntax for what should have been a simple function call is quite silly. They would have been much more efficient just starting with Python or Lua as a embedded scripting language with strong supporting libraries rather than reinventing the wheel in such a convoluted fashion. Even the old perl syntax for includes or requires is cleaner. Even ADA is less verbose and easier to read. |
Beta Was this translation helpful? Give feedback.
-
I found a working workaround and I did as suggested here: The workaround from @joeatnetgear did not work for me. |
Beta Was this translation helpful? Give feedback.
-
@mildmojo In your 2nd try, the error is that "env" is not allowed under a "runs" in a composite action. The docs are here https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-actions and following. As you can see looking down through there, 'env' is only allowed under one of the steps (not step itself). When 'env' is under a step, it only applies to that step. This is the doc for that https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runsstepsenv but notice that it says " If you want to modify the environment variable stored in the workflow, use echo "{name}={value}" >> $GITHUB_ENV in a composite step." When you do that, the env should be set starting in the next step after that "echo ..." and all subsequent steps. |
Beta Was this translation helpful? Give feedback.
-
AnswerThere's no top-level YAML way to declare a variable scoped to a composite action and available in step options, like you can with workflows and the top-level Docker actions do have a Thanks to everyone who replied! DetailsHere's what I learned from the replies, docs, and my own testing:
WorkaroundUsing name: "Test"
description: "Test composite actions setting global variables"
runs:
using: "composite"
steps:
- name: Set globals
id: globals
shell: bash
run: |
echo "TEST_ACTION_SHELL=bash" >> "${GITHUB_OUTPUT}"
echo "TEST_ACTION_RUN=date" >> "${GITHUB_OUTPUT}"
echo "TEST_ACTION_NODE_VERSION=12.0.0" >> "${GITHUB_OUTPUT}"
# Runs the `date` command in bash and outputs the date/time.
- name: Run command
shell: ${{ steps.globals.outputs.TEST_ACTION_SHELL }}
run: ${{ steps.globals.outputs.TEST_ACTION_RUN }}
- uses: actions/setup-node@v4
with:
node-version: ${{ steps.globals.outputs.TEST_ACTION_NODE_VERSION }}
# Correctly outputs v12.0.0
- name: Check node version
shell: bash
run: node --version |
Beta Was this translation helpful? Give feedback.
-
Hey guys, why it says in docs that we can do it: https://docs.github.com/en/actions/creating-actions/creating-a-composite-action#creating-an-action-metadata-file
|
Beta Was this translation helpful? Give feedback.
-
This is a needed feature; only suitable workaround as I see it, is to use inputs. However, it would be nice with an isolated approach as you propose, as this workaround seem artificial. |
Beta Was this translation helpful? Give feedback.
-
Hey, |
Beta Was this translation helpful? Give feedback.
-
Forgive me if I'm wrong, but setting 'top level' env vars in the workflow invoking the composite action, allows these env vars to be accessed in the composite action:
|
Beta Was this translation helpful? Give feedback.
-
on: jobs:
|
Beta Was this translation helpful? Give feedback.
Answer
There's no top-level YAML way to declare a variable scoped to a composite action and available in step options, like you can with workflows and the top-level
env
. 😔 Maybe they'll add it someday! See a workaround below.Docker actions do have a
runs.env
that lets you declare variables at a top-ish level, but those aren't composite actions, and I haven't tested to see if that modifies the caller'senv
.Thanks to everyone who replied!
Details
Here's what I learned from the replies, docs, and my own testing:
env
,jobs.<id>.env
, and the space you manipulate by writing to$GITHUB_ENV
in a shell command (in both workflows and actions) are all the same globally-shar…