Skip to content

Commit b82bf8e

Browse files
authored
Add Registry Authentication (#13)
* Allows adding --with-registry-auth * Allows running docker login * Fixes docker context bug
1 parent 1dae9f6 commit b82bf8e

File tree

5 files changed

+114
-31
lines changed

5 files changed

+114
-31
lines changed

.github/workflows/test.yaml

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ name: "Test"
22

33
on:
44
workflow_dispatch:
5+
pull_request:
56
push:
7+
branches: [master]
8+
9+
env:
10+
PRIVATE_IMAGE: "smashedr/alpine-private:latest" # amd64/arm64
611

712
jobs:
813
test:
@@ -14,24 +19,44 @@ jobs:
1419
- name: "Checkout"
1520
uses: actions/checkout@v4
1621

17-
- name: "Write YAML"
18-
id: yaml-action
22+
- name: "Write YAML Basic"
1923
uses: teunmooij/yaml@v1
2024
with:
21-
data: '{"version":"3.8","services":{"alpine":{"image":"alpine","command":"tail -f /dev/null"}}}'
25+
data: '{"version":"3.8","services":{"alpine":{"image":"alpine:latest","command":"tail -f /dev/null"}}}'
2226
to-file: "docker-compose.yaml"
2327

24-
- name: "Test Local Action"
25-
id: test
28+
- name: "Test Action Basic"
29+
id: test1
2630
uses: ./
2731
with:
32+
name: "test-stack"
33+
file: "docker-compose.yaml"
2834
host: ${{ secrets.DOCKER_HOST }}
2935
port: ${{ secrets.DOCKER_PORT }}
3036
user: ${{ secrets.DOCKER_USER }}
3137
#pass: ${{ secrets.DOCKER_PASS }}
3238
ssh_key: "${{ secrets.DOCKER_SSH_KEY }}"
33-
file: "docker-compose.yaml"
39+
40+
- name: "Write YAML Private"
41+
uses: teunmooij/yaml@v1
42+
with:
43+
data: '{"version":"3.8","services":{"alpine":{"image":"${{ env.PRIVATE_IMAGE }}","command":"tail -f /dev/null"}}}'
44+
to-file: "docker-compose.yaml"
45+
46+
- name: "Test Action Private"
47+
id: test2
48+
uses: ./
49+
with:
3450
name: "test-stack"
51+
file: "docker-compose.yaml"
52+
host: ${{ secrets.DOCKER_HOST }}
53+
port: ${{ secrets.DOCKER_PORT }}
54+
user: ${{ secrets.DOCKER_USER }}
55+
#pass: ${{ secrets.DOCKER_PASS }}
56+
ssh_key: "${{ secrets.DOCKER_SSH_KEY }}"
57+
#registry_host: "ghcr.io"
58+
registry_user: ${{ vars.DOCKER_HUB_USER }}
59+
registry_pass: ${{ secrets.DOCKER_HUB_PASS }}
3560

3661
lint:
3762
name: "Lint"

.prettierrc.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
"singleQuote": true,
55
"overrides": [
66
{
7-
"files": ["**/*.json", "**/*.yaml", "**/*.yml"],
7+
"files": ["**/*.html", "**/*.yaml", "**/*.yml"],
88
"options": {
99
"singleQuote": false
1010
}
1111
},
1212
{
13-
"files": ["**/*.json", "**/*.yaml", "**/*.yml"],
13+
"files": ["**/*.js", "**/*.css", "**/*.scss"],
1414
"options": {
15-
"tabWidth": 2
15+
"tabWidth": 4
1616
}
1717
}
1818
]

README.md

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,69 @@
1010
# Docker Stack Deploy Action
1111

1212
This action deploys a docker stack from a compose file to a remote docker host using SSH Password or Key File Authentication.
13+
You can also optionally authenticate against a private registry using a username and password.
1314

1415
For more details see [action.yaml](action.yaml) and [src/main.sh](src/main.sh).
1516

17+
_Portainer Users_: You can deploy directly to Portainer with: [cssnr/portainer-stack-deploy-action](https://github.com/cssnr/portainer-stack-deploy-action)
18+
1619
- [Inputs](#Inputs)
1720
- [Examples](#Examples)
1821
- [Support](#Support)
1922
- [Contributing](#Contributing)
2023

2124
## Inputs
2225

23-
| input | required | default | description |
24-
| -------- | -------- | --------------------- | ------------------------- |
25-
| host | **Yes** | - | Remote Docker hostname |
26-
| port | No | `22` | Remote Docker port |
27-
| user | **Yes** | - | Remote Docker username |
28-
| pass | No | - | Remote Docker password \* |
29-
| ssh_key | No | - | Remote SSH Key file \* |
30-
| file | No | `docker-compose.yaml` | Docker Compose file |
31-
| name | **Yes** | - | Docker Stack name |
32-
| env_file | No | - | Docker Environment file |
26+
| input | required | default | description |
27+
| ------------- | ---------------- | --------------------- | --------------------------------- |
28+
| host | **Yes** | - | Remote Docker hostname |
29+
| port | No | `22` | Remote Docker port |
30+
| user | **Yes** | - | Remote Docker username |
31+
| pass | Not w/ `ssh_key` | - | Remote Docker password \* |
32+
| ssh_key | Not w/ `pass` | - | Remote SSH Key file \* |
33+
| file | No | `docker-compose.yaml` | Docker Compose file |
34+
| name | **Yes** | - | Docker Stack name |
35+
| env_file | No | - | Docker Environment file |
36+
| registry_auth | No | - | Enable Registry Authentication \* |
37+
| registry_host | No | - | Registry Authentication Host \* |
38+
| registry_user | No | - | Registry Authentication User \* |
39+
| registry_pass | No | - | Registry Authentication Pass \* |
3340

3441
**pass/ssh_key** - You must provide either a `pass` or `ssh_key`
3542

43+
**registry_auth** - Set to `true` to deploy with `--with-registry-auth`
44+
45+
**registry_host** - To run `docker login` on another registry, example: `ghcr.io`
46+
47+
**registry_user/registry_pass** - Required to run `docker login` before stack deploy
48+
3649
```yaml
3750
- name: 'Docker Stack Deploy'
3851
uses: cssnr/stack-deploy-action@v1
3952
with:
53+
name: 'stack-name'
54+
file: 'docker-compose-swarm.yaml'
4055
host: ${{ secrets.DOCKER_HOST }}
4156
port: ${{ secrets.DOCKER_PORT }}
4257
user: ${{ secrets.DOCKER_USER }}
4358
pass: ${{ secrets.DOCKER_PASS }}
44-
file: 'docker-compose-swarm.yaml'
59+
```
60+
61+
Use `docker login` and enable `--with-registry-auth`
62+
63+
```yaml
64+
- name: 'Docker Stack Deploy'
65+
uses: cssnr/stack-deploy-action@v1
66+
with:
4567
name: 'stack-name'
68+
file: 'docker-compose-swarm.yaml'
69+
host: ${{ secrets.DOCKER_HOST }}
70+
port: ${{ secrets.DOCKER_PORT }}
71+
user: ${{ secrets.DOCKER_USER }}
72+
pass: ${{ secrets.DOCKER_PASS }}
73+
registry_host: 'ghcr.io'
74+
registry_user: ${{ vars.GHCR_USER }}
75+
registry_pass: ${{ secrets.GHCR_PASS }}
4676
```
4777

4878
## Examples
@@ -68,12 +98,12 @@ jobs:
6898
- name: 'Docker Stack Deploy'
6999
uses: cssnr/stack-deploy-action@v1
70100
with:
101+
name: 'stack-name'
102+
file: 'docker-compose-swarm.yaml'
71103
host: ${{ secrets.DOCKER_HOST }}
72104
port: ${{ secrets.DOCKER_PORT }}
73105
user: ${{ secrets.DOCKER_USER }}
74106
pass: ${{ secrets.DOCKER_PASS }}
75-
file: 'docker-compose-swarm.yaml'
76-
name: 'stack-name'
77107
```
78108

79109
Full Example
@@ -115,14 +145,14 @@ jobs:
115145
platforms: linux/amd64,linux/arm64
116146
117147
- name: 'Docker Login'
118-
uses: docker/login-action@v2
148+
uses: docker/login-action@v3
119149
with:
120150
registry: $${{ env.REGISTRY }}
121151
username: ${{ secrets.GHCR_USER }}
122152
password: ${{ secrets.GHCR_PASS }}
123153
124154
- name: 'Build and Push'
125-
uses: docker/build-push-action@v4
155+
uses: docker/build-push-action@v6
126156
with:
127157
context: .
128158
platforms: linux/amd64,linux/arm64
@@ -132,12 +162,12 @@ jobs:
132162
- name: 'Docker Stack Deploy'
133163
uses: cssnr/stack-deploy-action@v1
134164
with:
165+
name: 'stack-name'
166+
file: 'docker-compose-swarm.yaml'
135167
host: ${{ secrets.DOCKER_HOST }}
136168
port: ${{ secrets.DOCKER_PORT }}
137169
user: ${{ secrets.DOCKER_USER }}
138170
ssh_key: '${{ secrets.DOCKER_SSH_KEY }}'
139-
file: 'docker-compose-swarm.yaml'
140-
name: 'stack-name'
141171
```
142172

143173
# Support
@@ -165,6 +195,7 @@ Additionally, you can support other GitHub Actions I have published:
165195
- [Update JSON Value Action](https://github.com/cssnr/update-json-value-action)
166196
- [Parse Issue Form Action](https://github.com/cssnr/parse-issue-form-action)
167197
- [Mirror Repository Action](https://github.com/cssnr/mirror-repository-action)
198+
- [Stack Deploy Action](https://github.com/cssnr/stack-deploy-action)
168199
- [Portainer Stack Deploy](https://github.com/cssnr/portainer-stack-deploy-action)
169200
- [Mozilla Addon Update Action](https://github.com/cssnr/mozilla-addon-update-action)
170201

action.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "Docker Stack Deploy"
2-
description: "Deploy a Docker Stack"
2+
description: "Deploy a Docker Stack to a Remote Host over SSH w/ Optional Registry Authentication"
33
author: "Shane"
44
branding:
55
icon: "layers"
@@ -32,6 +32,18 @@ inputs:
3232
env_file:
3333
description: "Environment File"
3434
required: false
35+
registry_auth:
36+
description: "Enable Registry Auth"
37+
required: false
38+
registry_host:
39+
description: "Registry Auth Hostname"
40+
required: false
41+
registry_user:
42+
description: "Registry Auth Username"
43+
required: false
44+
registry_pass:
45+
description: "Registry Auth Password"
46+
required: false
3547

3648
runs:
3749
using: "docker"

src/main.sh

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ trap cleanup_trap EXIT HUP INT QUIT PIPE TERM
4646
echo -e "\u001b[36mVerifying Docker and Setting Context."
4747
ssh -p "${INPUT_PORT}" "${INPUT_USER}@${INPUT_HOST}" "docker info" > /dev/null
4848

49-
docker context create remote --docker "host=ssh://${INPUT_USER}@${INPUT_HOST}:${INPUT_PORT}"
50-
docker context ls
49+
if ! docker context inspect remote >/dev/null 2>&1;then
50+
docker context create remote --docker "host=ssh://${INPUT_USER}@${INPUT_HOST}:${INPUT_PORT}"
51+
fi
5152
docker context use remote
53+
docker context ls
5254

5355
if [ -n "${INPUT_ENV_FILE}" ];then
54-
echo -e "\u001b[36mSourcing Environment File: ${INPUT_ENV_FILE}"
56+
echo -e "\u001b[36mSourcing Environment File: \u001b[37;1m${INPUT_ENV_FILE}"
5557
stat "${INPUT_ENV_FILE}"
5658
set -a
5759
# shellcheck disable=SC1090
@@ -60,5 +62,18 @@ if [ -n "${INPUT_ENV_FILE}" ];then
6062
# export ENV_FILE="${INPUT_ENV_FILE}"
6163
fi
6264

65+
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}"
67+
echo "${INPUT_REGISTRY_PASS}" |
68+
docker login --username "${INPUT_REGISTRY_USER}" --password-stdin "${INPUT_REGISTRY_HOST}"
69+
INPUT_REGISTRY_AUTH="true"
70+
fi
71+
72+
EXTRA_ARGS=()
73+
if [[ -n "${INPUT_REGISTRY_AUTH}" ]];then
74+
echo -e "Adding extra arg: --with-registry-auth"
75+
EXTRA_ARGS+=("--with-registry-auth")
76+
fi
77+
6378
echo -e "\u001b[36mDeploying Stack: \u001b[37;1m${INPUT_NAME}"
64-
docker stack deploy -c "${INPUT_FILE}" "${INPUT_NAME}"
79+
docker stack deploy -c "${INPUT_FILE}" "${INPUT_NAME}" "${EXTRA_ARGS[@]}"

0 commit comments

Comments
 (0)