Call composite actions with dynamic names in a workflow #63675
-
Select Topic AreaQuestion BodyI have Github Workflow that run certain jobs for different repos using strategy matrix. In each matrix job I am trying to call a action from different repo that runs specific steps for the repo. However I am unable to achieve this while trying call action using ${{ matrix.repo }} expression. Is this unsupported or I am missing anything here, please suggest any good way to achieve this. Thanks
But somehow this fails with below error
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
The error message "Unrecognized named-value: 'matrix'. Located at position 1 within expression: matrix.repo" indicates that the To fix this, you can use the Here is an example of how you can fix this:
|
Beta Was this translation helpful? Give feedback.
-
Even using env throws the same error.
|
Beta Was this translation helpful? Give feedback.
-
Test-Run:
strategy:
matrix:
repo: ${{ REPOS_LIST }}
runs-on: ubuntu-latest
container:
image: image_name
env:
DYNAMIC_ACTION: ./.dynamic-action
LANG: en_US.utf8
steps:
- name: Setup config
run: some steps
- name: prepare composite action
uses: actions/github-script@v6
with:
script: |
const path = require('path');
const fs = require('fs/promises');
const dyn = '${{ env.DYNAMIC_ACTION }}';
const action = {
runs: {
using: 'composite',
steps: [
{
// clean up dynamically created action
name: 'Cleanup',
uses: `actions/github-script@${{ github.action_ref }}`,
with: {
script: `await io.rmRF('${dyn}')`
}
},
{
name: `Call action for ${{ matrix.repo }}`,
uses: `${{ matrix.repo }}/.github/actions/some_action/action.yml`
}
]
}
};
const content = JSON.stringify(action);
core.debug(`Writing action:\n${content}\n`);
await io.mkdirP(dyn);
await fs.writeFile(path.join(dyn, 'action.yml'), content);
- name: Call wrapper action
uses: ./.dynamic-action I am using this approach currently to test github action branch here. |
Beta Was this translation helpful? Give feedback.
-
See also #27048 I ran into this same limitation awhile back and went with a similar approach as @soumyamahunt... I wrapped it in to a reusable action so that I could use it in various repos/scenarios. Give Given a step like so: - uses: actions/setup-node@v3
with:
node-version: 18 If you want your - uses: jenseng/dynamic-uses@v1
with:
# now you can use expressions 🥳
uses: actions/setup-node@${{ inputs.version }}
# the `with` needs to be converted to a valid json string
with: '{ "node-version": 18 }' |
Beta Was this translation helpful? Give feedback.
-
Thanks @jenseng @soumyamahunt , both approaches looks good but unfortunately I switched to use workflow instead of actions and running it through api calls. Thanks. |
Beta Was this translation helpful? Give feedback.
uses
expects the action name to be determined before running the action, so while running the action you can't specify dynamic action foruses
. You have to wrap your dynamic invocation inside a composite action and invoke the composite action path from your job. That way you can adjust the content of the composite action just before the action is invoked. You can use something like this: