diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 14bfa73..fd602bc 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -46,3 +46,21 @@ 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" + + - name: Verify env-file interpolation + shell: bash + run: | + 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 }} diff --git a/README.md b/README.md index e89b935..662ce86 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,24 @@ 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`. + +```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. + ### 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. diff --git a/action.yaml b/action.yaml index 4986854..b2975b2 100644 --- a/action.yaml +++ b/action.yaml @@ -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 @@ -118,6 +122,8 @@ runs: [ -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 space-separated input. + [ -n "$ENV_FILE" ] && echo "COMPOSE_ENV_FILES=$(echo "$ENV_FILE" | tr ' ' ',')" >> $GITHUB_ENV || true # 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 @@ -130,6 +136,7 @@ runs: fi env: COMPOSE_FILES: ${{ inputs['compose-files'] }} + ENV_FILE: ${{ inputs['env-file'] }} - name: Login to Defang shell: bash diff --git a/test/action.env b/test/action.env new file mode 100644 index 0000000..7fde724 --- /dev/null +++ b/test/action.env @@ -0,0 +1 @@ +GREETING=hello-from-env-file diff --git a/test/compose.envfile.yaml b/test/compose.envfile.yaml new file mode 100644 index 0000000..d13e8f0 --- /dev/null +++ b/test/compose.envfile.yaml @@ -0,0 +1,7 @@ +services: + web: + build: . + ports: + - "3000:3000" + environment: + GREETING: ${GREETING}