Skip to content

Commit ae50e2e

Browse files
committed
feat: cli
1 parent bb7fade commit ae50e2e

3 files changed

Lines changed: 148 additions & 4 deletions

File tree

.github/workflows/integration.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ jobs:
248248
env:
249249
PAYLOAD_FILE_OUTPUT_TIME: ${{ steps.payload_file.outputs.time }}
250250

251+
- name: "integration(cli): run a slack cli version check"
252+
id: cli-version
253+
uses: ./
254+
with:
255+
command: "version"
256+
257+
- name: "integration(cli): confirm the cli command succeeded"
258+
run: test "$CLI_OK" = "true"
259+
env:
260+
CLI_OK: ${{ steps.cli-version.outputs.ok }}
261+
251262
- name: "chore(health): check up on recent changes to the health score"
252263
uses: slackapi/slack-health-score@d58a419f15cdaff97e9aa7f09f95772830ab66f7 # v0.1.1
253264
with:

.github/workflows/publish.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ jobs:
3737
id: tag
3838
uses: teunmooij/github-versioned-release@3edf649c6e5e5e976d43f2584b15bdc8b4c8f0df # v1.2.1
3939
with:
40-
template: javascript-action
40+
template: composite-action
41+
include: |
42+
dist/**/*
4143
env:
4244
GITHUB_TOKEN: ${{ github.token }}
4345

action.yml

Lines changed: 134 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
name: "Slack: Send to Slack"
22
author: "slackapi"
3-
description: "Send data to Slack to start a Slack workflow in Workflow Builder, call a Slack API method, or post a message into a channel"
3+
description: "Send data to Slack to start a Slack workflow in Workflow Builder, call a Slack API method, post a message into a channel, or run a Slack CLI command"
44
inputs:
55
api:
66
description: "A custom API URL to send Slack API method requests to."
77
required: false
8+
command:
9+
description: "A Slack CLI command to run without the 'slack' prefix."
10+
required: false
811
errors:
912
default: "false"
1013
description: "If the step exits with an error on errors or continues."
@@ -34,6 +37,9 @@ inputs:
3437
token:
3538
description: "The authentication value used with the Slack API."
3639
required: false
40+
version:
41+
description: "The version of the Slack CLI to install."
42+
required: false
3743
webhook:
3844
description: "A location for posting request payloads."
3945
required: false
@@ -43,16 +49,141 @@ inputs:
4349
outputs:
4450
ok:
4551
description: "If the request completed without returning errors."
52+
value: ${{ steps.slack-send.outputs.ok || steps.slack-cli.outputs.ok }}
4653
response:
4754
description: "A JSON stringified version of the Slack API response."
55+
value: ${{ steps.slack-send.outputs.response || steps.slack-cli.outputs.response }}
4856
time:
4957
description: "The Unix epoch time that the step completed."
58+
value: ${{ steps.slack-send.outputs.time || steps.slack-cli.outputs.time }}
5059
channel_id:
5160
description: "The channel ID returned with some of the Slack API methods."
61+
value: ${{ steps.slack-send.outputs.channel_id }}
5262
thread_ts:
5363
description: "The timestamp of a parent Slack message with threaded replies."
64+
value: ${{ steps.slack-send.outputs.thread_ts }}
5465
ts:
5566
description: "The timestamp of a Slack message or event in the response."
67+
value: ${{ steps.slack-send.outputs.ts }}
5668
runs:
57-
using: "node20"
58-
main: "dist/index.js"
69+
using: composite
70+
steps:
71+
- name: Validate inputs
72+
if: inputs.command != '' && (inputs.method != '' || inputs.webhook != '')
73+
shell: bash
74+
run: |
75+
echo "::error::The 'command' input cannot be combined with 'method' or 'webhook' inputs."
76+
exit 1
77+
78+
- name: Send data to Slack
79+
id: slack-send
80+
if: inputs.command == ''
81+
shell: bash
82+
run: node "${{ github.action_path }}/dist/index.js"
83+
env:
84+
INPUT_API: ${{ inputs.api }}
85+
INPUT_ERRORS: ${{ inputs.errors }}
86+
INPUT_METHOD: ${{ inputs.method }}
87+
INPUT_PAYLOAD: ${{ inputs.payload }}
88+
INPUT_PAYLOAD-DELIMITER: ${{ inputs.payload-delimiter }}
89+
INPUT_PAYLOAD-FILE-PATH: ${{ inputs.payload-file-path }}
90+
INPUT_PAYLOAD-TEMPLATED: ${{ inputs.payload-templated }}
91+
INPUT_PROXY: ${{ inputs.proxy }}
92+
INPUT_RETRIES: ${{ inputs.retries }}
93+
INPUT_TOKEN: ${{ inputs.token }}
94+
INPUT_WEBHOOK: ${{ inputs.webhook }}
95+
INPUT_WEBHOOK-TYPE: ${{ inputs.webhook-type }}
96+
97+
- name: Check if Slack CLI already exists
98+
id: slack-cli-check
99+
if: inputs.command != ''
100+
shell: bash
101+
run: |
102+
if command -v slack &> /dev/null; then
103+
echo "cli-exists=true" >> "$GITHUB_OUTPUT"
104+
else
105+
echo "cli-exists=false" >> "$GITHUB_OUTPUT"
106+
fi
107+
108+
- name: Cache Slack CLI
109+
if: inputs.command != '' && steps.slack-cli-check.outputs.cli-exists != 'true'
110+
id: cache-cli
111+
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
112+
with:
113+
path: |
114+
${{ runner.os == 'Windows' && format('{0}\AppData\Local\slack-cli', env.USERPROFILE) || format('{0}/.slack/bin', env.HOME) }}
115+
key: slack-cli-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }}
116+
117+
- name: Add Slack CLI to PATH (Linux/macOS)
118+
if: inputs.command != '' && steps.slack-cli-check.outputs.cli-exists != 'true' && runner.os != 'Windows'
119+
shell: bash
120+
run: echo "$HOME/.slack/bin" >> "$GITHUB_PATH"
121+
122+
- name: Add Slack CLI to PATH (Windows)
123+
if: inputs.command != '' && steps.slack-cli-check.outputs.cli-exists != 'true' && runner.os == 'Windows'
124+
shell: pwsh
125+
run: Add-Content -Path $env:GITHUB_PATH -Value "$env:USERPROFILE\.slack\bin"
126+
127+
- name: Install Slack CLI (Linux/macOS)
128+
if: >-
129+
inputs.command != '' &&
130+
steps.slack-cli-check.outputs.cli-exists != 'true' &&
131+
steps.cache-cli.outputs.cache-hit != 'true' &&
132+
runner.os != 'Windows'
133+
shell: bash
134+
run: |
135+
if [ -n "$SLACK_CLI_VERSION" ]; then
136+
curl -fsSL https://downloads.slack-edge.com/slack-cli/install.sh | bash -s -- -v "$SLACK_CLI_VERSION"
137+
else
138+
curl -fsSL https://downloads.slack-edge.com/slack-cli/install.sh | bash -s
139+
fi
140+
env:
141+
SLACK_CLI_VERSION: ${{ inputs.version }}
142+
143+
- name: Install Slack CLI (Windows)
144+
if: >-
145+
inputs.command != '' &&
146+
steps.slack-cli-check.outputs.cli-exists != 'true' &&
147+
steps.cache-cli.outputs.cache-hit != 'true' &&
148+
runner.os == 'Windows'
149+
shell: pwsh
150+
run: |
151+
if ($env:SLACK_CLI_VERSION) {
152+
$installer = Join-Path $env:TEMP "install-slack-cli.ps1"
153+
Invoke-WebRequest -Uri "https://downloads.slack-edge.com/slack-cli/install-windows-dev.ps1" -OutFile $installer
154+
& $installer -v $env:SLACK_CLI_VERSION
155+
} else {
156+
irm https://downloads.slack-edge.com/slack-cli/install-windows-dev.ps1 | iex
157+
}
158+
env:
159+
SLACK_CLI_VERSION: ${{ inputs.version }}
160+
161+
- name: Run Slack CLI command
162+
id: slack-cli
163+
if: inputs.command != ''
164+
shell: bash
165+
env:
166+
SLACK_COMMAND: ${{ inputs.command }}
167+
SLACK_TOKEN: ${{ inputs.token }}
168+
run: |
169+
args="$SLACK_COMMAND --skip-update"
170+
if [ -n "$SLACK_TOKEN" ]; then
171+
args="$args --token $SLACK_TOKEN"
172+
fi
173+
174+
set +e
175+
output=$(slack $args 2>&1)
176+
exit_code=$?
177+
set -e
178+
179+
echo "ok=$([ $exit_code -eq 0 ] && echo 'true' || echo 'false')" >> "$GITHUB_OUTPUT"
180+
echo "time=$(date +%s)" >> "$GITHUB_OUTPUT"
181+
182+
echo "response<<SLACKCLIEOF" >> "$GITHUB_OUTPUT"
183+
echo "$output" >> "$GITHUB_OUTPUT"
184+
echo "SLACKCLIEOF" >> "$GITHUB_OUTPUT"
185+
186+
if [ $exit_code -ne 0 ]; then
187+
echo "::error::Slack CLI command failed with exit code $exit_code"
188+
exit $exit_code
189+
fi

0 commit comments

Comments
 (0)