Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/colbylwilliams/deployment-environments


https://github.com/colbylwilliams/deployment-environments

actions azure deployment dev-center environments microsoft

Last synced: 5 days ago
JSON representation

Awesome Lists containing this project

README

        

# Azure Deployment Environments action

This action dynamically creates an environment for each branch and pull request, and deletes the environment when the branch is deleted or pull request is closed.

Learn more about Azure Deployment Environments [here][environments].

## Usage

_See [action.yml](action.yml) for [metadata](https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions) that defines the [inputs](#inputs), [outputs](#outputs), and runs configuration for this action._

_For more information about workflows, see [Using workflows](https://docs.github.com/en/actions/using-workflows)._

Once you've configured your workflow, save it as a `.yml` file in your target Repository's `.github/workflows` directory.

**See the usage examples at the bottom of this README. For a complete example, see the [deployment-environments-sample](https://github.com/colbylwilliams/deployment-environments-sample) repo.**

## Environment Name

The environment name is generated by the action in the following format:

`prefix-[branch|pr]-[branch name|pr number]-suffix`

You can override the default `prefix` and `suffix` by providing values as [inputs](#inputs) or in an [`ade.yml`](#config-yaml) config file.

### Examples

- The environment name for a branch named `feature1` would be: `ci-branch-feature1-615502282`
- The environment name for the second pull request in a repo: `ci-pr-2-615502282`

## Environment Type

The [environment type][environment-types] is resolved from the branch or pull request. There are two different configurations depending on whether or not you use a `dev` branch, or create `feature` branches directly from `main`.

| branch/Pull request | Environment Type |
| ----------------------- | ---------------- |
| `feature` branches | Dev |
| `feature` -> `main` PRs | Test |
| `main` branch | Prod |

When you specify the name of a `dev` branch using the `dev-branch` input parameter, the action uses an additional `Staging` environment type.

| branch/Pull request | Environment Type |
| -------------------------- | ---------------- |
| `dev` & `feature` branches | Dev |
| `feature` -> `dev` PRs | Test |
| `dev` -> `main` PRs | Staging |
| `main` branch | Prod |

## Config YAML

You can provide some of the required [inputs](#inputs) (like `devcenter`, `project`, etc.) in a YAML file in your repository.

If an input is provided in the config file and directly within a workflow, the direct input will be used.

### Example

```yaml
# ade.yml

prefix: ade
suffix: 22887658
devcenter: MyDevCenter123
project: MyProject123
catalog: Environments
definition: FunctionApp
prod-environment-name: my-prod-app
```

By default, the action looks for a file named `ade.yml` at the root of the repository, but you can override the path/name with the `config` input.

```yaml
# .github/workflows/create.yml

- uses: colbylwilliams/deployment-environments@v1
with:
action: create
config: /folder/my-app.yml
```

## `action` Input

The `action` input is required. It determines what action to take on the environment.

| Name | Description |
| -------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `setup` | Only resolves the environment `name` and environment `type`. No other outputs will be populated. This action does not require azure login. |
| `get` | Gets an existing environment. If the environment doesn't exist, it is not created. |
| `create` | Creates a new environment. |
| `update` | Updates an existing environment. |
| `ensure` | Gets an existing environment. If the environment doesn't exist, it is created. |
| `delete` | Deletes an existing environment. |
| `auto` | Attempts to set the `action` automatically by from the event context. |

## Inputs

Inputs in **bold** are required for all [`action`s](#action-input) except `setup`. Inputs can also be provided using a [config YAML file](#config-yaml).

| Name | Description | Default |
| -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- |
| **`action`** | Action to take on the environment. Can be setup, get, create, update, ensure, delete, or auto (see table above). | `setup` |
| `tenant` | The Azure tenant ID (GUID). If not provided the action will try to resolve from the azure cli. | |
| `subscription` | The Azure subscription (GUID). If not provided the action will try to resolve from the azure cli. | |
| **`devcenter`** | The [Dev center][devcenter] name. | |
| **`project`** | The [Project][project] name. | |
| **`catalog`** | The name of the [Catalog][catalog]. | |
| **`definition`** | The name of the [Environment Definition][environment-definitions]. | |
| `prefix` | The prefix for the resolved environment name. If none is provided, environment names will start with ci. | `ci` |
| `suffix` | The suffix for the resolved environment name. If none is provided, environment names will end with the repository numeric id. | `github.repository_id` |
| `main-branch` | The name of the main branch. Defaults to main. | `main` |
| `dev-branch` | The name of the dev branch. If a value is provided, the action assumes feature branches are created from the dev branch and the dev branch is merged into main for production deployments. | |
| `prod-environment-name` | Override the name of the production environment. If not specified, it will be [generated](#environment-name). | |
| `prod-environment-type` | The name of the production environment type where the `main` branch will be deployed. | `Prod` |
| `staging-environment-type` | The name of the staging environment type. If `dev-branch` is specified, pull requests from the dev branches to the `main` branch will create environments in this environment type. | `Staging` |
| `test-environment-type` | The name of the testing environment type. If `dev-branch` is specified, pull requests from feature branches to the dev branch will create environments in this environment type. If feature branches are created from `main`, pull requests from feature branches to `main` will use this type. | `Test` |
| `dev-environment-type` | The name of the development environment type. Feature branches will create environments in this environment type. | `Dev` |
| `parameters` | A JSON string containing parameters for the environment. | |
| `config` | Path to a YAML [configuration file](#config-yaml). | `ade.yml` |
| `summary` | If set to `true`, the action will output a [job summary][github-summary] with information about the environment. | `false` |

## Outputs

| Name | Description |
| ------------------- | ------------------------------------------------------------------ |
| `tenant` | The environment tenant (GUID). |
| `subscription` | The environment subscription (GUID). |
| `resource-group` | The name of the environment resource group. |
| `resource-group-id` | The resource id of the environment resource group. |
| `exists` | True if the environment exists otherwise False. |
| `created` | True if the environment was created by the action otherwise False. |
| `portal-url` | Url to the environment resource group in the azure portal. |
| `name` | The environment name. |
| `type` | The environment type. |

## Environment Variables

| Name | Description |
| ----------------------- | ------------------------------------------------------------------ |
| `ADE_TENANT` | The environment tenant (GUID). |
| `ADE_SUBSCRIPTION` | The environment subscription (GUID). |
| `ADE_RESOURCE_GROUP` | The name of the environment resource group. |
| `ADE_RESOURCE_GROUP_ID` | The resource id of the environment resource group. |
| `ADE_EXISTS` | True if the environment exists otherwise False. |
| `ADE_CREATED` | True if the environment was created by the action otherwise False. |
| `ADE_PORTAL_URL` | Url to the environment resource group in the azure portal. |
| `ADE_NAME` | The environment name. |
| `ADE_TYPE` | The environment type. |

## Example Usage: Create Environment

This example workflow runs with a branch is created or a pull request is opened and creates a new environment specific to that branch/pr.

```yaml
# .github/workflows/create.yml

name: Create Environment

on:
create:
pull_request:
types: [opened, reopened]

concurrency: ${{ github.event.number || github.ref_name }}

jobs:
create:
if: github.event_name == 'pull_request' || github.event.ref_type == 'branch'
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- run: dotnet publish ./My.App -c Release -o ./publish && (cd ./publish && zip -r ../publish.zip .)

- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- uses: colbylwilliams/deployment-environments@v1
with:
action: create
parameters: '{ "name": "helloworld" }'

- run: |
az functionapp deployment source config-zip --src publish.zip \
--subscription $ADE_SUBSCRIPTION --resource-group $ADE_RESOURCE_GROUP --name $ADE_NAME

echo "- [View environment resources in the Azure portal]($ADE_PORTAL_URL)" >> $GITHUB_STEP_SUMMARY
```

## Example Usage: Update Environment

This example workflow builds and deploys the My.App app when a branch or pull request is updated.

```yaml
# .github/workflows/update.yml

name: Update Environment

on:
push:
paths:
- 'My.App/**'
pull_request:
types: [synchronize]
paths:
- 'My.App/**'

concurrency: ${{ github.event.number || github.ref_name }}

jobs:
update:
if: github.event_name == 'pull_request' || (contains(github.event.ref, 'refs/heads') && !github.event.created)
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- run: dotnet publish ./My.App -c Release -o ./publish && (cd ./publish && zip -r ../publish.zip .)

- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- uses: colbylwilliams/deployment-environments@v1
with:
action: ensure
parameters: '{ "name": "helloworld" }'

- run: |
az functionapp deployment source config-zip --src publish.zip \
--subscription $ADE_SUBSCRIPTION --resource-group $ADE_RESOURCE_GROUP --name $ADE_NAME

echo "- [View environment resources in the Azure portal]($ADE_PORTAL_URL)" >> $GITHUB_STEP_SUMMARY
```

## Example Usage: Delete Environment

This example workflow deletes the environment associated with a branch or pull request when the branch is deleted or the pull request is closed.

```yaml
# .github/workflows/delete.yml

name: Delete Environment

on:
delete:
pull_request:
types: [closed]

concurrency:
group: ${{ github.event.number || github.event.ref }}
cancel-in-progress: true

jobs:
delete:
if: github.event_name == 'pull_request' || github.event.ref_type == 'branch'
runs-on: ubuntu-latest

steps:
- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- uses: colbylwilliams/deployment-environments@v1
with:
action: delete
```

## Example Usage: Setup

The `setup` action only resolves the environment name and type. It doesn't make any calls to Azure, and is normally used with another action (like `create`) later in the workflow.

`setup` is useful when you want to map the project environment type to a [GitHub environment][github-environments]. This allows you to use environment-type-specific identities to login and create/delete the deployment environments.

This example extends the [`Create Environment`](#example-usage-create-environment) example above.

```yaml
# .github/workflows/create.yml

name: Create Environment

on:
create:
pull_request:
types: [opened, reopened]

concurrency: ${{ github.event.number || github.ref_name }}

jobs:
setup:
if: github.event_name == 'pull_request' || github.event.ref_type == 'branch'
runs-on: ubuntu-latest

outputs:
name: ${{ steps.setup.outputs.name }}
type: ${{ steps.setup.outputs.type }}

steps:
id: setup
uses: colbylwilliams/deployment-environments@v1
with:
action: setup

create:
if: github.event_name == 'pull_request' || github.event.ref_type == 'branch'
runs-on: ubuntu-latest

needs: setup
environment: ${{ needs.setup.outputs.type }}

steps:
- uses: actions/checkout@v3

- run: dotnet publish ./My.App -c Release -o ./publish && (cd ./publish && zip -r ../publish.zip .)

- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- uses: colbylwilliams/deployment-environments@v1
with:
action: create
parameters: '{ "name": "${{ needs.setup.outputs.name }}" }'

- run: |
az functionapp deployment source config-zip --src publish.zip \
--subscription $ADE_SUBSCRIPTION --resource-group $ADE_RESOURCE_GROUP --name $ADE_NAME

echo "- [View environment resources in the Azure portal]($ADE_PORTAL_URL)" >> $GITHUB_STEP_SUMMARY
```

[devcenter]: https://learn.microsoft.com/en-us/azure/deployment-environments/concept-environments-key-concepts#dev-centers
[project]: https://learn.microsoft.com/en-us/azure/deployment-environments/concept-environments-key-concepts#projects
[environments]: https://learn.microsoft.com/en-us/azure/deployment-environments/overview-what-is-azure-deployment-environments
[catalog]: https://learn.microsoft.com/en-us/azure/deployment-environments/concept-environments-key-concepts#catalog
[environment-definitions]: https://learn.microsoft.com/en-us/azure/deployment-environments/concept-environments-key-concepts#catalog-items
[environment-types]: https://learn.microsoft.com/en-us/azure/deployment-environments/concept-environments-key-concepts#project-environment-types
[github-environments]: https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment
[github-summary]: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary