Skip to content

Commit 6ef807d

Browse files
committed
Add Job Summary
1 parent 60eb115 commit 6ef807d

File tree

7 files changed

+62
-30
lines changed

7 files changed

+62
-30
lines changed

.github/workflows/test.yaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ env:
1515
jobs:
1616
test:
1717
name: "Test"
18+
if: ${{ !contains(github.event.head_commit.message, '#notest') }}
1819
runs-on: ubuntu-latest
1920
timeout-minutes: 5
20-
if: ${{ !contains(github.event.head_commit.message, '#notest') }}
2121

2222
steps:
2323
- name: "Checkout"
@@ -28,12 +28,14 @@ jobs:
2828
# cat "${GITHUB_EVENT_PATH}"
2929

3030
- name: "Write YAML Basic"
31+
if: ${{ !github.event.act }}
3132
uses: teunmooij/yaml@v1
3233
with:
3334
data: '{"version":"3.8","services":{"alpine":{"image":"alpine:latest","command":"tail -f /dev/null"}}}'
3435
to-file: "docker-compose.yaml"
3536

36-
- name: "Test Action Basic"
37+
- name: "Test SSH Key"
38+
if: ${{ !github.event.act }}
3739
id: test1
3840
uses: ./
3941
with:
@@ -51,7 +53,7 @@ jobs:
5153
data: '{"version":"3.8","services":{"alpine":{"image":"${{ env.PRIVATE_IMAGE }}","command":"tail -f /dev/null"}}}'
5254
to-file: "docker-compose.yaml"
5355

54-
- name: "Test Action Private"
56+
- name: "Test Password w/ Registry"
5557
id: test2
5658
uses: ./
5759
with:
@@ -60,8 +62,8 @@ jobs:
6062
host: ${{ secrets.DOCKER_HOST }}
6163
port: ${{ secrets.DOCKER_PORT }}
6264
user: ${{ secrets.DOCKER_USER }}
63-
#pass: ${{ secrets.DOCKER_PASS }}
64-
ssh_key: "${{ secrets.DOCKER_SSH_KEY }}"
65+
pass: ${{ secrets.DOCKER_PASS }}
66+
#ssh_key: "${{ secrets.DOCKER_SSH_KEY }}"
6567
#registry_host: "ghcr.io"
6668
registry_user: ${{ vars.DOCKER_HUB_USER }}
6769
registry_pass: ${{ secrets.DOCKER_HUB_PASS }}

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
FROM docker:24-dind
1+
FROM docker:28-dind
22

33
RUN apk add --update --no-cache bash sshpass
44

5-
COPY src/main.sh /main.sh
5+
COPY src/ /src
66

7-
ENTRYPOINT ["bash", "/main.sh"]
7+
ENTRYPOINT ["bash", "/src/main.sh"]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ _Portainer Users_: You can deploy directly to Portainer with: [cssnr/portainer-s
3939
| registry_host | - | - | Registry Authentication Host \* |
4040
| registry_user | - | - | Registry Authentication User \* |
4141
| registry_pass | - | - | Registry Authentication Pass \* |
42+
| summary | - | `true` | Add Job Summary |
4243

4344
**pass/ssh_key** - You must provide either a `pass` or `ssh_key`
4445

action.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ inputs:
2424
required: false
2525
file:
2626
description: "Docker Compose File"
27-
default: "docker-compose.yaml"
2827
required: false
28+
default: "docker-compose.yaml"
2929
name:
3030
description: "Docker Stack Name"
3131
required: true
@@ -44,6 +44,10 @@ inputs:
4444
registry_pass:
4545
description: "Registry Auth Password"
4646
required: false
47+
summary:
48+
description: "Add Summary to Job"
49+
required: false
50+
default: "true"
4751

4852
runs:
4953
using: "docker"

event.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"act": true
3+
}

src/main.sh

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,90 @@
11
#!/usr/bin/env bash
2+
# https://github.com/cssnr/stack-deploy-action
23

34
set -e
45

5-
echo "Running: ${0} as: $(whoami) in: $(pwd)"
6-
76
function cleanup_trap() {
87
_ST="$?"
98
if [[ "${_ST}" != "0" ]]; then
10-
echo -e "\u001b[31;1mScript Exited with Error: ${_ST}"
9+
echo -e "\u001b[31;1mFailed to Deploy Stack! See logs for details..."
1110
fi
1211
if [ -z "${INPUT_SSH_KEY}" ];then
13-
echo -e "\u001b[35mCleaning Up authorized_keys on: ${INPUT_HOST}"
12+
echo -e "Cleaning Up authorized_keys on: \u001b[36;1m${INPUT_HOST}"
1413
ssh -p "${INPUT_PORT}" "${INPUT_USER}@${INPUT_HOST}" \
1514
"sed -i '/docker-stack-deploy-action/d' ~/.ssh/authorized_keys"
1615
fi
1716
if [[ "${_ST}" == "0" ]]; then
18-
echo -e "\u001b[32;1mFinished Success."
17+
echo -e "\u001b[32;1mFinished Success."
1918
fi
2019
exit "${_ST}"
2120
}
2221

22+
echo "::group::Starting Stack Deploy Action"
23+
echo "User: $(whoami)"
24+
echo "Script: ${0}"
25+
echo -e "Directory: $(pwd)"
2326
mkdir -p /root/.ssh
2427
chmod 0700 /root/.ssh
2528
ssh-keyscan -p "${INPUT_PORT}" -H "${INPUT_HOST}" >> /root/.ssh/known_hosts
29+
echo "::endgroup::"
2630

2731
if [ -z "${INPUT_SSH_KEY}" ];then
28-
echo -e "\u001b[36mCreating and Copying SSH Key to: ${INPUT_HOST}"
32+
echo -e "::group::Copying SSH Key to: \u001b[36;1m${INPUT_HOST}"
2933
ssh-keygen -q -f /root/.ssh/id_rsa -N "" -C "docker-stack-deploy-action"
3034
eval "$(ssh-agent -s)"
3135
ssh-add /root/.ssh/id_rsa
32-
33-
sshpass -p "${INPUT_PASS}" \
34-
ssh-copy-id -p "${INPUT_PORT}" -i /root/.ssh/id_rsa \
35-
"${INPUT_USER}@${INPUT_HOST}"
36+
mkdir ~/.ssh
37+
sshpass -eINPUT_PASS \
38+
ssh-copy-id -i /root/.ssh/id_rsa -o ConnectTimeout=15 \
39+
-p "${INPUT_PORT}" "${INPUT_USER}@${INPUT_HOST}"
3640
else
37-
echo -e "\u001b[36mAdding SSH Key to SSH Agent"
41+
echo "::group::Adding SSH Key to SSH Agent"
3842
echo "${INPUT_SSH_KEY}" > /root/.ssh/id_rsa
3943
chmod 0600 /root/.ssh/id_rsa
4044
eval "$(ssh-agent -s)"
4145
ssh-add /root/.ssh/id_rsa
4246
fi
47+
echo "::endgroup::"
4348

4449
trap cleanup_trap EXIT HUP INT QUIT PIPE TERM
4550

46-
echo -e "\u001b[36mVerifying Docker and Setting Context."
47-
ssh -p "${INPUT_PORT}" "${INPUT_USER}@${INPUT_HOST}" "docker info" > /dev/null
48-
51+
echo "::group::Verifying Docker and Setting Context."
52+
ssh -o BatchMode=yes -o ConnectTimeout=15 -p "${INPUT_PORT}" \
53+
"${INPUT_USER}@${INPUT_HOST}" "docker info" > /dev/null
4954
if ! docker context inspect remote >/dev/null 2>&1;then
5055
docker context create remote --docker "host=ssh://${INPUT_USER}@${INPUT_HOST}:${INPUT_PORT}"
5156
fi
5257
docker context use remote
5358
docker context ls
59+
echo "::endgroup::"
5460

5561
if [ -n "${INPUT_ENV_FILE}" ];then
56-
echo -e "\u001b[36mSourcing Environment File: \u001b[37;1m${INPUT_ENV_FILE}"
62+
echo -e "::group::Sourcing Environment File: \u001b[36;1m${INPUT_ENV_FILE}"
5763
stat "${INPUT_ENV_FILE}"
5864
set -a
5965
# shellcheck disable=SC1090
6066
source "${INPUT_ENV_FILE}"
61-
# echo TRAEFIK_HOST: "${TRAEFIK_HOST}"
62-
# export ENV_FILE="${INPUT_ENV_FILE}"
67+
echo "::endgroup::"
6368
fi
6469

6570
if [[ -n "${INPUT_REGISTRY_USER}" && -n "${INPUT_REGISTRY_PASS}" ]];then
66-
echo -e "\u001b[36mLogging in to Registry: \u001b[37;1m${INPUT_REGISTRY_HOST:-Docker Hub}"
71+
echo -e "::group::Logging in to Registry: \u001b[36;1m${INPUT_REGISTRY_HOST:-Docker Hub}"
6772
echo "${INPUT_REGISTRY_PASS}" |
6873
docker login --username "${INPUT_REGISTRY_USER}" --password-stdin "${INPUT_REGISTRY_HOST}"
6974
INPUT_REGISTRY_AUTH="true"
75+
echo "::endgroup::"
7076
fi
7177

78+
echo -e "Deploying Stack: \u001b[36;1m${INPUT_NAME}"
7279
EXTRA_ARGS=()
7380
if [[ -n "${INPUT_REGISTRY_AUTH}" ]];then
74-
echo -e "Adding extra arg: --with-registry-auth"
81+
echo -e "Enabling Registry Authentication"
7582
EXTRA_ARGS+=("--with-registry-auth")
7683
fi
84+
# shellcheck disable=SC2034
85+
STACK_RESULTS=$(docker stack deploy -c "${INPUT_FILE}" "${INPUT_NAME}" "${EXTRA_ARGS[@]}")
7786

78-
echo -e "\u001b[36mDeploying Stack: \u001b[37;1m${INPUT_NAME}"
79-
docker stack deploy -c "${INPUT_FILE}" "${INPUT_NAME}" "${EXTRA_ARGS[@]}"
87+
if [[ "${INPUT_SUMMARY}" == "true" ]];then
88+
echo "📝 Writing Job Summary"
89+
source /src/summary.sh >> "${GITHUB_STEP_SUMMARY}"
90+
fi

src/summary.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cat << EOM
2+
## Stack Deploy Action
3+
4+
🎉 Stack \`${INPUT_NAME}\` Successfully Deployed.
5+
6+
\`\`\`text
7+
${STACK_RESULTS}
8+
\`\`\`
9+
10+
[Report an issue or request a feature](https://github.com/cssnr/stack-deploy-action?tab=readme-ov-file#readme)
11+
EOM

0 commit comments

Comments
 (0)