|
| 1 | +# Deployment Environment Resolver |
| 2 | + |
| 3 | +A reusable GitHub Action that resolves deployment environments from workflow inputs, branch/ref mappings, or tag patterns — with wildcard support. |
| 4 | + |
| 5 | +Replace inline shell scripts in your workflows with a single, configurable action step. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Input mapping** — resolve from `workflow_dispatch` inputs |
| 10 | +- **Branch mapping** — map branches to environments with glob/wildcard support |
| 11 | +- **Tag mapping** — map tag patterns to environments (e.g. `v*` → prod) |
| 12 | +- **Default fallback** — configurable fallback environment |
| 13 | +- **Strict mode** — fail if no mapping matches |
| 14 | +- **Debug mode** — verbose logging for troubleshooting |
| 15 | +- **Match source output** — know _how_ the environment was resolved |
| 16 | + |
| 17 | +## Quick Start |
| 18 | + |
| 19 | +```yaml |
| 20 | +- name: Resolve environment |
| 21 | + id: env |
| 22 | + uses: code4mk/deployment-env-resolver-action@v1 |
| 23 | + |
| 24 | +- name: Deploy |
| 25 | + run: echo "Deploying to ${{ steps.env.outputs.environment }}" |
| 26 | +``` |
| 27 | +
|
| 28 | +With zero configuration, the action defaults to `dev`. |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +### Basic — Branch Mapping |
| 33 | + |
| 34 | +```yaml |
| 35 | +- name: Resolve environment |
| 36 | + id: env |
| 37 | + uses: code4mk/deployment-env-resolver-action@v1 |
| 38 | + with: |
| 39 | + ref-mapping: | |
| 40 | + main=prod |
| 41 | + develop=staging |
| 42 | + feature/*=preview |
| 43 | + default-env: dev |
| 44 | +``` |
| 45 | + |
| 46 | +### With Workflow Dispatch |
| 47 | + |
| 48 | +```yaml |
| 49 | +on: |
| 50 | + workflow_dispatch: |
| 51 | + inputs: |
| 52 | + environment: |
| 53 | + type: choice |
| 54 | + options: |
| 55 | + - dev |
| 56 | + - staging |
| 57 | + - prod |
| 58 | +
|
| 59 | +jobs: |
| 60 | + deploy: |
| 61 | + runs-on: ubuntu-latest |
| 62 | + steps: |
| 63 | + - uses: actions/checkout@v4 |
| 64 | +
|
| 65 | + - name: Resolve environment |
| 66 | + id: env |
| 67 | + uses: code4mk/deployment-env-resolver-action@v1 |
| 68 | + with: |
| 69 | + input-mapping: | |
| 70 | + prod=production |
| 71 | + staging=staging |
| 72 | + dev=development |
| 73 | + ref-mapping: | |
| 74 | + main=production |
| 75 | + develop=staging |
| 76 | + default-env: development |
| 77 | +``` |
| 78 | + |
| 79 | +### With Tag Mapping |
| 80 | + |
| 81 | +```yaml |
| 82 | +- name: Resolve environment |
| 83 | + id: env |
| 84 | + uses: code4mk/deployment-env-resolver-action@v1 |
| 85 | + with: |
| 86 | + tag-mapping: | |
| 87 | + v*=prod |
| 88 | + rc-*=staging |
| 89 | + beta-*=preview |
| 90 | + ref-mapping: | |
| 91 | + main=prod |
| 92 | + develop=staging |
| 93 | + default-env: dev |
| 94 | +``` |
| 95 | + |
| 96 | +### Strict Mode |
| 97 | + |
| 98 | +Fail the step if no mapping matches and no default is suitable: |
| 99 | + |
| 100 | +```yaml |
| 101 | +- name: Resolve environment |
| 102 | + id: env |
| 103 | + uses: code4mk/deployment-env-resolver-action@v1 |
| 104 | + with: |
| 105 | + ref-mapping: | |
| 106 | + main=prod |
| 107 | + develop=staging |
| 108 | + strict: "true" |
| 109 | + default-env: "" |
| 110 | +``` |
| 111 | + |
| 112 | +### Debug Mode |
| 113 | + |
| 114 | +Enable verbose logging to see which mappings are being checked: |
| 115 | + |
| 116 | +```yaml |
| 117 | +- name: Resolve environment |
| 118 | + id: env |
| 119 | + uses: code4mk/deployment-env-resolver-action@v1 |
| 120 | + with: |
| 121 | + ref-mapping: | |
| 122 | + main=prod |
| 123 | + debug: "true" |
| 124 | +``` |
| 125 | + |
| 126 | +## Inputs |
| 127 | + |
| 128 | +| Input | Required | Default | Description | |
| 129 | +|---|---|---|---| |
| 130 | +| `input-mapping` | No | — | Mapping for `workflow_dispatch` inputs (`key=value` per line) | |
| 131 | +| `ref-mapping` | No | — | Mapping for branch/ref names with wildcard support (`pattern=env` per line) | |
| 132 | +| `tag-mapping` | No | — | Mapping for tag patterns (`pattern=env` per line) | |
| 133 | +| `default-env` | No | `dev` | Fallback environment if no mapping matches | |
| 134 | +| `strict` | No | `false` | Fail the step if no mapping matches and no default is set | |
| 135 | +| `debug` | No | `false` | Enable debug logging | |
| 136 | + |
| 137 | +## Outputs |
| 138 | + |
| 139 | +| Output | Description | |
| 140 | +|---|---| |
| 141 | +| `environment` | Resolved environment name | |
| 142 | +| `match-source` | How the environment was resolved: `input`, `ref`, `tag`, or `default` | |
| 143 | + |
| 144 | +## Resolution Order |
| 145 | + |
| 146 | +The action resolves the environment in this order (first match wins): |
| 147 | + |
| 148 | +1. **Input mapping** — if the event is `workflow_dispatch` and an input is provided |
| 149 | +2. **Tag mapping** — if the ref type is a tag |
| 150 | +3. **Ref mapping** — branch/ref pattern matching |
| 151 | +4. **Default** — fallback value |
| 152 | + |
| 153 | +## Mapping Format |
| 154 | + |
| 155 | +Each mapping is a newline-separated list of `pattern=environment` pairs: |
| 156 | + |
| 157 | +``` |
| 158 | +main=prod |
| 159 | +develop=staging |
| 160 | +feature/*=preview |
| 161 | +release-*=staging |
| 162 | +``` |
| 163 | + |
| 164 | +- Patterns support bash-style glob wildcards (`*`, `?`, `[...]`) |
| 165 | +- Lines starting with `#` are treated as comments |
| 166 | +- Empty lines are ignored |
| 167 | +- Whitespace around keys and values is trimmed |
| 168 | + |
| 169 | +## Full Example Workflow |
| 170 | + |
| 171 | +```yaml |
| 172 | +name: Deploy |
| 173 | +
|
| 174 | +on: |
| 175 | + push: |
| 176 | + branches: [main, develop, "feature/*"] |
| 177 | + tags: ["v*"] |
| 178 | +
|
| 179 | + workflow_dispatch: |
| 180 | + inputs: |
| 181 | + environment: |
| 182 | + type: choice |
| 183 | + description: Target environment |
| 184 | + options: |
| 185 | + - dev |
| 186 | + - staging |
| 187 | + - prod |
| 188 | +
|
| 189 | +jobs: |
| 190 | + deploy: |
| 191 | + runs-on: ubuntu-latest |
| 192 | + steps: |
| 193 | + - uses: actions/checkout@v4 |
| 194 | +
|
| 195 | + - name: Resolve environment |
| 196 | + id: env |
| 197 | + uses: code4mk/deployment-env-resolver-action@v1 |
| 198 | + with: |
| 199 | + input-mapping: | |
| 200 | + prod=prod |
| 201 | + staging=staging |
| 202 | + dev=dev |
| 203 | + tag-mapping: | |
| 204 | + v*=prod |
| 205 | + ref-mapping: | |
| 206 | + main=prod |
| 207 | + develop=staging |
| 208 | + feature/*=preview |
| 209 | + default-env: dev |
| 210 | + debug: "true" |
| 211 | +
|
| 212 | + - name: Use resolved environment |
| 213 | + run: | |
| 214 | + echo "Environment: ${{ steps.env.outputs.environment }}" |
| 215 | + echo "Matched via: ${{ steps.env.outputs.match-source }}" |
| 216 | +``` |
| 217 | + |
| 218 | +## License |
| 219 | + |
| 220 | +[MIT](LICENSE) |
0 commit comments