Skip to content

Commit 165d9f6

Browse files
committed
feat: cli
1 parent ae50e2e commit 165d9f6

4 files changed

Lines changed: 140 additions & 150 deletions

File tree

.github/workflows/integration.yml

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -248,21 +248,26 @@ 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: ./
251+
cli:
252+
name: Run CLI tests
253+
runs-on: ${{ matrix.os }}
254+
strategy:
255+
matrix:
256+
os:
257+
- ubuntu-latest
258+
- macos-latest
259+
- windows-latest
260+
permissions:
261+
contents: read
262+
steps:
263+
- name: "build: checkout the latest changes"
264+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
254265
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 }}
266+
persist-credentials: false
267+
ref: ${{ inputs.ref || github.event.pull_request.head.sha || github.sha }}
261268

262-
- name: "chore(health): check up on recent changes to the health score"
263-
uses: slackapi/slack-health-score@d58a419f15cdaff97e9aa7f09f95772830ab66f7 # v0.1.1
269+
- name: "integration(cli): run a slack cli version check"
270+
id: version
271+
uses: ./cli
264272
with:
265-
codecov_token: ${{ secrets.CODECOV_API_TOKEN }}
266-
github_token: ${{ secrets.GITHUB_TOKEN }}
267-
extension: js
268-
include: src
273+
command: "version"

.github/workflows/publish.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ jobs:
3737
id: tag
3838
uses: teunmooij/github-versioned-release@3edf649c6e5e5e976d43f2584b15bdc8b4c8f0df # v1.2.1
3939
with:
40-
template: composite-action
40+
template: javascript-action
4141
include: |
4242
dist/**/*
43+
cli/**/*
4344
env:
4445
GITHUB_TOKEN: ${{ github.token }}
4546

action.yml

Lines changed: 3 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
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, post a message into a channel, or run a Slack CLI command"
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"
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
118
errors:
129
default: "false"
1310
description: "If the step exits with an error on errors or continues."
@@ -37,9 +34,6 @@ inputs:
3734
token:
3835
description: "The authentication value used with the Slack API."
3936
required: false
40-
version:
41-
description: "The version of the Slack CLI to install."
42-
required: false
4337
webhook:
4438
description: "A location for posting request payloads."
4539
required: false
@@ -49,141 +43,16 @@ inputs:
4943
outputs:
5044
ok:
5145
description: "If the request completed without returning errors."
52-
value: ${{ steps.slack-send.outputs.ok || steps.slack-cli.outputs.ok }}
5346
response:
5447
description: "A JSON stringified version of the Slack API response."
55-
value: ${{ steps.slack-send.outputs.response || steps.slack-cli.outputs.response }}
5648
time:
5749
description: "The Unix epoch time that the step completed."
58-
value: ${{ steps.slack-send.outputs.time || steps.slack-cli.outputs.time }}
5950
channel_id:
6051
description: "The channel ID returned with some of the Slack API methods."
61-
value: ${{ steps.slack-send.outputs.channel_id }}
6252
thread_ts:
6353
description: "The timestamp of a parent Slack message with threaded replies."
64-
value: ${{ steps.slack-send.outputs.thread_ts }}
6554
ts:
6655
description: "The timestamp of a Slack message or event in the response."
67-
value: ${{ steps.slack-send.outputs.ts }}
6856
runs:
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
57+
using: "node20"
58+
main: "dist/index.js"

cli/action.yml

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
name: "Slack: Run CLI Command"
2+
author: "slackapi"
3+
description: "Install the Slack CLI and run a command in a GitHub Actions workflow"
4+
inputs:
5+
command:
6+
description: "A Slack CLI command to run without the 'slack' prefix."
7+
required: true
8+
token:
9+
description: "A service token passed as the '--token' flag to the CLI command."
10+
required: false
11+
version:
12+
description: "The version of the Slack CLI to install. Defaults to the latest."
13+
required: false
14+
outputs:
15+
ok:
16+
description: "If the command completed with a '0' exit code."
17+
value: ${{ steps.slack-cli.outputs.ok }}
18+
response:
19+
description: "The standard output from the CLI command."
20+
value: ${{ steps.slack-cli.outputs.response }}
21+
time:
22+
description: "The Unix epoch time that the step completed."
23+
value: ${{ steps.slack-cli.outputs.time }}
24+
runs:
25+
using: composite
26+
steps:
27+
- name: Check if Slack CLI already exists
28+
id: slack-cli-check
29+
shell: bash
30+
run: |
31+
if command -v slack &> /dev/null; then
32+
echo "cli-exists=true" >> "$GITHUB_OUTPUT"
33+
else
34+
echo "cli-exists=false" >> "$GITHUB_OUTPUT"
35+
fi
36+
37+
- name: Cache Slack CLI
38+
if: steps.slack-cli-check.outputs.cli-exists != 'true'
39+
id: cache-cli
40+
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
41+
with:
42+
path: |
43+
${{ runner.os == 'Windows' && format('{0}\AppData\Local\slack-cli', env.USERPROFILE) || format('{0}/.slack/bin', env.HOME) }}
44+
key: slack-cli-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }}
45+
46+
- name: Add Slack CLI to PATH (Linux/macOS)
47+
if: steps.slack-cli-check.outputs.cli-exists != 'true' && runner.os != 'Windows'
48+
shell: bash
49+
run: echo "$HOME/.slack/bin" >> "$GITHUB_PATH" # zizmor: ignore[github-env]
50+
51+
- name: Add Slack CLI to PATH (Windows)
52+
if: steps.slack-cli-check.outputs.cli-exists != 'true' && runner.os == 'Windows'
53+
shell: pwsh
54+
run: Add-Content -Path $env:GITHUB_PATH -Value "$env:USERPROFILE\.slack\bin" # zizmor: ignore[github-env]
55+
56+
- name: Install Slack CLI (Linux/macOS)
57+
if: >-
58+
steps.slack-cli-check.outputs.cli-exists != 'true' &&
59+
steps.cache-cli.outputs.cache-hit != 'true' &&
60+
runner.os != 'Windows'
61+
shell: bash
62+
run: |
63+
if [ -n "$SLACK_CLI_VERSION" ]; then
64+
curl -fsSL https://downloads.slack-edge.com/slack-cli/install.sh | bash -s -- -v "$SLACK_CLI_VERSION"
65+
else
66+
curl -fsSL https://downloads.slack-edge.com/slack-cli/install.sh | bash -s
67+
fi
68+
env:
69+
SLACK_CLI_VERSION: ${{ inputs.version }}
70+
71+
- name: Install Slack CLI (Windows)
72+
if: >-
73+
steps.slack-cli-check.outputs.cli-exists != 'true' &&
74+
steps.cache-cli.outputs.cache-hit != 'true' &&
75+
runner.os == 'Windows'
76+
shell: pwsh
77+
run: |
78+
if ($env:SLACK_CLI_VERSION) {
79+
$installer = Join-Path $env:TEMP "install-slack-cli.ps1"
80+
Invoke-WebRequest -Uri "https://downloads.slack-edge.com/slack-cli/install-windows-dev.ps1" -OutFile $installer
81+
& $installer -v $env:SLACK_CLI_VERSION
82+
} else {
83+
irm https://downloads.slack-edge.com/slack-cli/install-windows-dev.ps1 | iex
84+
}
85+
env:
86+
SLACK_CLI_VERSION: ${{ inputs.version }}
87+
88+
- name: Run Slack CLI command
89+
id: slack-cli
90+
shell: bash
91+
env:
92+
SLACK_COMMAND: ${{ inputs.command }}
93+
SLACK_TOKEN: ${{ inputs.token }}
94+
run: |
95+
args="$SLACK_COMMAND --skip-update"
96+
if [ -n "$SLACK_TOKEN" ]; then
97+
args="$args --token $SLACK_TOKEN"
98+
fi
99+
100+
set +e
101+
output=$(slack $args 2>&1)
102+
exit_code=$?
103+
set -e
104+
105+
echo "ok=$([ $exit_code -eq 0 ] && echo 'true' || echo 'false')" >> "$GITHUB_OUTPUT"
106+
echo "time=$(date +%s)" >> "$GITHUB_OUTPUT"
107+
108+
echo "response<<SLACKCLIEOF" >> "$GITHUB_OUTPUT"
109+
echo "$output" >> "$GITHUB_OUTPUT"
110+
echo "SLACKCLIEOF" >> "$GITHUB_OUTPUT"
111+
112+
if [ $exit_code -ne 0 ]; then
113+
echo "::error::Slack CLI command failed with exit code $exit_code"
114+
exit $exit_code
115+
fi

0 commit comments

Comments
 (0)