Skip to content
Open
20 changes: 20 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,23 @@ jobs:
compose-files: ""
verbose: true
command: "compose up --dry-run --project-name github-action-test"

- name: Deploy with env-file
id: env-file-test
uses: ./
continue-on-error: true # Ignore dry run error
with:
cwd: "./test"
compose-files: "compose.envfile.yaml"
env-file: "action.env"
command: "compose config --project-name github-action-test"
capture-output: true

- name: Verify env-file interpolation
shell: bash
run: |
set -o pipefail
echo "${STDOUT}" | grep -q "hello-from-env-file" \
|| { echo "expected env-file value not found in output"; exit 1; }
env:
STDOUT: ${{ steps.env-file-test.outputs.stdout }}
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ jobs:
DB_CONNECTION_STRING: ${{ secrets.DB_CONNECTION_STRING }}
```

### Using an Environment File

By default, Defang loads a `.env` file next to your Compose file to resolve `${VARIABLE}` interpolation. To use a different environment file (or several), set the `env-file` input. This mirrors `docker compose --env-file` and, when set, replaces the default `.env`.

Requires Defang CLI `v3.12.0` or newer. Older CLI versions ignore this input and silently fall back to the default `.env` file.

```yaml
jobs:
test:
# [...]
steps:
# [...]
- name: Deploy
uses: DefangLabs/defang-github-action@v2
with:
env-file: ".env.production"
```

Pass multiple files whitespace-delimited (`env-file: ".env .env.production"`); later files override earlier ones.

Comment thread
coderabbitai[bot] marked this conversation as resolved.
### Projects in a Subdirectory

If your Compose file is in a different directory than your project root, you can specify the path to the project in the `cwd` input.
Expand Down
22 changes: 22 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ inputs:
description: "The compose files to deploy. Format 'file1 file2 file3'"
required: false
default: ""
env-file:
description: "Environment file(s) used for compose interpolation instead of the default .env. Format 'file1 file2 file3'"
required: false
default: ""
cwd:
description: "The directory containing the compose file to deploy."
required: false
Expand Down Expand Up @@ -114,10 +118,27 @@ runs:
- name: Set Defang environment variables
shell: bash
run: |
set -o pipefail

[ -n "$RUNNER_DEBUG" ] && echo "DEFANG_DEBUG=$RUNNER_DEBUG" >> $GITHUB_ENV || true
[ -n "${{ inputs['provider'] }}" ] && echo "DEFANG_PROVIDER=${{ inputs['provider'] }}" >> $GITHUB_ENV || true
[ -n "${{ inputs['stack'] }}" ] && echo "DEFANG_STACK=${{ inputs['stack'] }}" >> $GITHUB_ENV || true
[ -n "${{ inputs['project'] }}" ] && echo "COMPOSE_PROJECT_NAME=${{ inputs['project'] }}" >> $GITHUB_ENV || true
# The CLI reads COMPOSE_ENV_FILES (comma-separated) like docker compose; convert the
# whitespace-separated input (spaces or newlines, so multiline YAML lists work too).
# Always write it, even empty, so a prior step's env-file doesn't leak into a later
# step of the same job that doesn't set one (GITHUB_ENV entries persist job-wide).
if [ -n "$ENV_FILE" ]; then
# Disable globbing so the unquoted expansion below only word-splits on
# IFS (space/tab/newline, so multiline YAML lists work) without
# expanding literal filenames that start with '-' or contain wildcards.
set -f
env_file_parts=($ENV_FILE)
set +f
echo "COMPOSE_ENV_FILES=$(IFS=,; echo "${env_file_parts[*]}")" >> $GITHUB_ENV
else
echo "COMPOSE_ENV_FILES=" >> $GITHUB_ENV
fi
# Docker Compose (and the Defang CLI) natively read the compose file
# list from COMPOSE_FILE, so export that once instead of building a
# repeated -f/-f/-f list in every step. The unquoted echo collapses
Expand All @@ -130,6 +151,7 @@ runs:
fi
env:
COMPOSE_FILES: ${{ inputs['compose-files'] }}
ENV_FILE: ${{ inputs['env-file'] }}

- name: Login to Defang
shell: bash
Expand Down
1 change: 1 addition & 0 deletions test/action.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GREETING=hello-from-env-file
7 changes: 7 additions & 0 deletions test/compose.envfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
web:
build: .
ports:
- "3000:3000"
environment:
GREETING: ${GREETING}
Loading