The Problem
Last week I was working on a composite action for our deployments and wanted to use the Slack Github Action inside of the composite action.
I came across a syntax issue and was unable to pass the secure variables via env, such as
${{ secrets.SLACK_WEBHOOK_URL }} or ${{ secrets.SLACK_BOT_TOKEN }}
Lets take a look at the YAML to show you what I mean:
- name: Send custom JSON data to Slack workflow
id: slack
uses: slackapi/[email protected]
with:
payload-file-path: "./payload-slack-content.json"
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
If you try and take the following YAML and incorporate it with you composite action you will get a Syntax error and Actions won't be able to run the composite action. As the env section is not allowed when adding this task to the action.yml file for a composite action.
The slack action expects you to pass these secure variables in this manner. Is there a better way? Or better than that, a way to still use this task and change our approach?
The Solution
To get around this you have to re-adjust the order the env is passed.
First in your action.yml for your composite action completely remove the env section. The example below will send a rich message to a slack channel about a deployment I want to do. Notice env is completely removed from the end of the YAML as mentioned in the docs for the Slack Action.
- id: slack
uses: slackapi/[email protected]
with:
# The following message update step does not accept a channel name.
# Setting a channel ID here for consistency is highly recommended.
channel-id: "${{inputs.slack_channel_id}}"
payload: |
{
"text": "Deployment started (In Progress) to ${{inputs.environment_name}}",
"attachments": [
{
"pretext": "Deployment started",
"color": "dbab09",
"fields": [
{
"title": "Status",
"short": true,
"value": "In Progress"
}
]
}
]
}
Now in the YAML that calls the action (Your code repo) declare your env variable there instead. The env will pass down to all the actions inside the composite action.
Ensure the names of your env variables match what is in the documentation of the action you are trying to use. In my case slack needs ${{SLACK_WEBHOOK_URL }} and ${{ SLACK_WEBHOOK_TYPE }} for the task I am using.
- name: Deployment Composite Action
id: deploy-task
uses: MyAccount/MyRepo
with:
iis-website-path: ${{ vars.IIS_WEBSITE_PATH }}
iis-website-name: ${{ vars.IIS_WEBSITE_NAME }}
iis-apppool-name: ${{ vars.IIS_APP_POOL_NAME }}
slack_channel_id: ${{ secrets.SLACK_CHANNEL_ID }}
environment_name: ${{ matrix.environment }}
artifact-name: ${{ needs.find.outputs.artifact }}
runID: ${{ needs.find.outputs.runID }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
There is no need to edit your inputs on your action.yml to make this work. GitHub will pass the two values down to all tasks inside of the composite action.
Happy Automating!