Skip to content

Commit

Permalink
azdeploy
Browse files Browse the repository at this point in the history
  • Loading branch information
segraef committed Mar 22, 2020
1 parent b0245f1 commit 0c88e13
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 2 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
# azdeploy
Azure GitHub Action Deploy (azdeploy)
# GitHub Action for Managing Azure Resources

This action can be used to create or update a resource group in Azure using the [Azure Resource Manager templates](https://azure.microsoft.com/en-in/documentation/articles/resource-group-template-deploy/)
One can also use this to delete a resource group, including all the resources within the resource group.

To log into a Azure, I recommend using [azlogin](https://github.com/segraef/azlogin) Action.

## Usage

```
- uses: segraef/azdeploy@v1
with:
resourceGroupName: ${{ secrets.resourceGroupName }}
templateFile: ${{ secrets.templateFile }}
parametersFile: ${{ secrets.parametersFile }}
resourceGroupCommand: ${{ secrets.resourceGroupCommand }}
```

### Environment variables

- `resourceGroupCommand`**Optional**.

- If `resourceGroupCommand` is not specified or is "create"
- `resourceGroupName`**Mandatory**
- `templateFile`**Mandatory** - Can be a URL or relative path in your github repository
- `parametersFile`**Mandatory** - Can be a URL or relative path in your github repository

- If `resourceGroupCommand` is "delete"
- `resourceGroupName`**Mandatory**
30 changes: 30 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: azdeploy
description: 'Deploy ARM template'
inputs:
resourceGroupName:
description: 'Name of the resource group.'
required: true
templateFile:
description: 'ARM template file location.'
required: true
parametersFile:
description: 'ARM parameters file location.'
required: false
resrouceGroupCommand:
description: 'RG command.'
required: false
outputs:
output:
description: 'ARM outputs'
runs:
using: "docker"
image: "dockerfile"
args:
- -resourceGroupName
- ${{inputs.resourceGroupName}}
- -templateFile
- ${{inputs.templateFile}}
- -parametersFile
- ${{inputs.parametersFile}}
- -resrouceGroupCommand
- ${{inputs.resrouceGroupCommand}}
7 changes: 7 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM mcr.microsoft.com/azure-cli:latest

COPY entry.sh /

RUN chmod +x /entry.sh

ENTRYPOINT ["/entry.sh"]
76 changes: 76 additions & 0 deletions entry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash

set -e
export AZURE_HTTP_USER_AGENT="GITHUBACTIONS_${GITHUB_ACTION_NAME}_${GITHUB_REPOSITORY}"

if [[ -z "$resourceGroupName" ]]
then
echo "resourceGroupName is not set." >&2
exit 1
fi

if [[ -z "$resourceGroupCommand" ]] || [[ ${resourceGroupCommand,,} == 'create' ]]
then
echo "Executing commands to Create/Update resource group."
# Create Resource group if it does not exists

resourceGroupExists=$(az group exists -n "$resourceGroupName")
if [[ $resourceGroupExists == "false" ]]
then
if [[ -z "$resourceGroupLocation" ]]
then
echo "resourceGroupLocation is not set." >&2
exit 1
fi
az group create --name "$resourceGroupName" --location "$resourceGroupLocation"
fi

uri_regex="^(http://|https://)\\w+"
guid=$(uuidgen | cut -d '-' -f 1)

# Download parameters file if it is a remote URL

if [[ $parametersFile =~ $uri_regex ]]
then
PARAMETERS=$(curl "$parametersFile")
echo "Downloaded parameters from ${parametersFile}"
else
paramsFile="${GITHUB_WORKSPACE}/${parametersFile}"
if [[ ! -e "$paramsFile" ]]
then
echo "Parameters file ${paramsFile} does not exists." >&2
exit 1
fi
PARAMETERS="@${paramsFile}"
fi

# Generate deployment name if not specified

if [[ -z "$deploymentName" ]]
then
deploymentName="Github-Action-azdeploy-${guid}"
echo "Generated Deployment Name ${deploymentName}"
fi

# Deploy ARM template

if [[ $templateFile =~ $uri_regex ]]
then
az group deployment create -g "$resourceGroupName" --name "$deploymentName" --template-uri "$templateFile" --parameters "$PARAMETERS"
else
templFile="${GITHUB_WORKSPACE}/${templateFile}"
if [[ ! -e "$templFile" ]]
then
echo "Template file ${templFile} does not exists." >&2
exit 1
fi
az group deployment create -g "$resourceGroupName" --name "$deploymentName" --template-file "$templFile" --parameters "$PARAMETERS"
fi
elif [[ ${resourceGroupCommand,,} == 'delete' ]]
then
echo "Executing commands to Delete resource group."
az group delete -n "$resourceGroupName" --yes
else
echo "Invalid resourceGroupCommand. Allowed values are: CREATE, DELETE." >&2
exit 1
fi

0 comments on commit 0c88e13

Please sign in to comment.