Skip to content

Commit 7de5408

Browse files
committed
Implement support for env_file parameter
1 parent 0e6d0ca commit 7de5408

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ GitHub Action and Docker image used to deploy a Docker stack on a Docker Swarm.
3636
| `stack_file` | `STACK_FILE` | Path to the stack file used in the deploy. || |
3737
| `stack_name` | `STACK_NAME` | Name of the stack to be deployed. || |
3838
| `stack_param` | `STACK_PARAM` | Additional parameter (env var) to be passed to the stack. | | |
39+
| `env_file` | `ENV_FILE` | Additional environment variables to be passed to the stack. | | |
3940
| `debug` | `DEBUG` | Verbose logging | | **0** |
4041

4142

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ inputs:
4545
description: "Additional stack parameter value"
4646
required: false
4747
default: ""
48+
env_file:
49+
description: "Additional environment variables in the format VAR_01=VALUE\nVAR_02=VALUE"
50+
required: false
51+
default: ""
4852
debug:
4953
description: "Debug information"
5054
required: false
@@ -64,4 +68,5 @@ runs:
6468
STACK_FILE: ${{ inputs.stack_file }}
6569
STACK_NAME: ${{ inputs.stack_name }}
6670
STACK_PARAM: ${{ inputs.stack_param }}
71+
ENV_FILE: ${{ inputs.env_file }}
6772
DEBUG: ${{ inputs.debug }}

scripts/docker-entrypoint.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set -e
55
SSH_DIR="/root/.ssh"
66
SSH_KEY="${SSH_DIR}/docker"
77
KNOWN_HOSTS="${SSH_DIR}/known_hosts"
8+
ENV_FILE_PATH="/root/.env"
89

910
login() {
1011
echo "${PASSWORD}" | docker login "${REGISTRY}" -u "${USERNAME}" --password-stdin
@@ -27,6 +28,26 @@ configure_ssh_key() {
2728
ssh-add "${SSH_KEY}"
2829
}
2930

31+
configure_env_file() {
32+
echo "Environment Variables: Additional values"
33+
printf '%s' "$ENV_FILE" > "${ENV_FILE_PATH}"
34+
env_file_len=$(grep -v '^#' ${ENV_FILE_PATH}|grep -v '^$' -c)
35+
if [[ $env_file_len -gt 0 ]]; then
36+
if [ "${DEBUG}" != "0" ]; then
37+
echo "Environment vars before: $(env|wc -l)"
38+
fi
39+
# shellcheck disable=SC2046
40+
export $(grep -v '^#' ${ENV_FILE_PATH} | grep -v '^$' | xargs -d '\n')
41+
if [ "${DEBUG}" != "0" ]; then
42+
echo "Environment vars after: $(env|wc -l)"
43+
fi
44+
fi
45+
46+
chmod 600 "${ENV_FILE_PATH}"
47+
eval "$(ssh-agent)"
48+
ssh-add "${SSH_KEY}"
49+
}
50+
3051
configure_ssh_host() {
3152
ssh-keyscan -p "${REMOTE_PORT}" "${REMOTE_HOST}" > "${KNOWN_HOSTS}"
3253
chmod 600 "${KNOWN_HOSTS}"
@@ -54,6 +75,14 @@ check_deploy() {
5475

5576
[ -z ${DEBUG+x} ] && export DEBUG="0"
5677

78+
# ADDITIONAL ENV VARIABLES
79+
if [[ -z "${ENV_FILE}" ]]; then
80+
export ENV_FILE=""
81+
else
82+
configure_env_file;
83+
fi
84+
85+
# SET DEBUG
5786
if [ "${DEBUG}" != "0" ]; then
5887
OUT=/dev/stdout;
5988
SSH_VERBOSE="-vvv"
@@ -112,6 +141,8 @@ if [[ -z "${STACK_NAME}" ]]; then
112141
exit 1
113142
fi
114143

144+
145+
# CONFIGURE SSH CLIENT
115146
if configure_ssh > $OUT 2>&1; then
116147
echo "SSH client: Configured"
117148
else

0 commit comments

Comments
 (0)