Skip to content

Commit

Permalink
Merge pull request #8 from highbyte/feature-linuxdeployscripts
Browse files Browse the repository at this point in the history
Feature linuxdeployscripts
  • Loading branch information
highbyte authored Jul 3, 2020
2 parents 2c0b71c + bbd2401 commit 16c335c
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 9 deletions.
33 changes: 24 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ There was no Sonarqube plugin for integration with MS Teams when I checked. As I
You deploy this Azure Function to your own Azure Subscription.

### Deployment via script
There is a provided Powershell script ([src/SonarqubeMSTeamsBridge/CreateAzureResourcesAndPublishFunction.ps1](src/SonarqubeMSTeamsBridge/CreateAzureResourcesAndPublishFunction.ps1)) that creates necessary Azure resources, and compiles/uploads the Azure function project in this repository.
There is a provided Powershell script (Windows) ([src/SonarqubeMSTeamsBridge/CreateAzureResourcesAndPublishFunction.ps1](src/SonarqubeMSTeamsBridge/CreateAzureResourcesAndPublishFunction.ps1)) or Bash script (Linux) ([src/SonarqubeMSTeamsBridge/CreateAzureResourcesAndPublishFunction.sh](src/SonarqubeMSTeamsBridge/CreateAzureResourcesAndPublishFunction.sh)) that creates necessary Azure resources, and compiles/uploads the Azure function project in this repository.

The Powershell script requires the following command line tools to be installed on the machine you run it from
The scripts requires the following command line tools to be installed on the machine you run it from, on either Windows or Linux.
* [Azure CLI (az)](https://docs.microsoft.com/en-us/cli/azure/?view=azure-cli-latest)
* [Azure Functions Core Tools (func)](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local)

#### Login to Azure via the CLI tool
``` powershell
```
az login
```

#### Set current Subscription (if you have more than one)
``` powershell
```
az account set --subscription [your_subscription_id_or_name_here]
```

Expand All @@ -48,10 +48,17 @@ When running the script, you need to provide the following parameters

#### Run the script
_Note: Example parmeters below, you may want to change them._

_Powershell on Windows_
``` powershell
cd .\src\SonarqubeMSTeamsBridge
.\CreateAzureResourcesAndPublishFunction.ps1 -region "westus" -resourceGroup "rg-sqteamsbridge" -storageName "stsqteamsbridge$(Get-Random -Max 32767)" -functionAppName "func-sqteamsbridge$(Get-Random -Max 32767)"
```
_Bash on Linux_
``` bash
cd ./src/SonarqubeMSTeamsBridge
./CreateAzureResourcesAndPublishFunction.sh "westus" "rg-sqteamsbridge" "stsqteamsbridge$RANDOM" "func-sqteamsbridge$RANDOM"
```

When the script has completed, it will output the **Invoke url** of the Azure Function.
**This is the address that should be configured in Sonarqube as a Webhook URL as described [here](#configure-sonarqube).**
Expand All @@ -75,29 +82,37 @@ func-sqteamsbridgeXXXXX rg-sqteamsbridge westeurope Microsoft.Web/sites
Azure Function settings can be set via script. Change Azure resource names and values. Read [here](#azure-function-settings) about each setting.

Required settings
``` powershell
```
az functionapp config appsettings set --name "func-sqteamsbridgeXXXXX" --resource-group "rg-sqteamsbridge" --settings "TeamsWebhookUrl=https://outlook.office.com/webhook/XXXX"
az functionapp config appsettings set --name "func-sqteamsbridgeXXXXX" --resource-group "rg-sqteamsbridge" --settings "SonarqubeWebhookSecret=MY_SECRET"
```

Optional settings
``` powershell
```
az functionapp config appsettings set --name "func-sqteamsbridgeXXXXX" --resource-group "rg-sqteamsbridge" --settings "QualityGateStatusExcludeList=SUCCESS"
az functionapp config appsettings set --name "func-sqteamsbridgeXXXXX" --resource-group "rg-sqteamsbridge" --settings "Culture=en-US"
```

#### Publish Azure Function trigger only
If you already have a Azure Function resource you want to use instead of creating a new one, or if you update to a newer version of this project, you can use a separate script for this purpose.
If you already have a Azure Function resource you want to use instead of creating a new one, or if you update to a newer version of this project, you can use a separate script for this purpose. See ([src/SonarqubeMSTeamsBridge/PublishFunctionOnly.ps1](src/SonarqubeMSTeamsBridge/PublishFunctionOnly.ps1)) or ([src/SonarqubeMSTeamsBridge/PublishFunctionOnly.sh](src/SonarqubeMSTeamsBridge/PublishFunctionOnly.sh))

Script parameters
* **functionAppName**: _The name of an existing Azure Function app where the Azure Function trigger in this project will be published to._

_Note: Example parmeter below, change it to an existing Azure Function app name in your Subscription. _
_Note: Example parmeter below, change it to an existing Azure Function app name in your Subscription._

_Powershell on Windows_
``` powershell
cd .\src\SonarqubeMSTeamsBridge
.\PublishFunctionOnly.ps1 -functionAppName "func-sqteamsbridgeXXXX"
```

_Bash on Linux_
``` bash
cd ./src/SonarqubeMSTeamsBridge
./PublishFunctionOnly.sh "func-sqteamsbridgeXXXX"
```


### Manual Azure deployment

Expand All @@ -110,7 +125,7 @@ As an alternative to deployment via script (as described above), Visual Studio C
* Select Function App in Azure: **Create new Function App in Azure... Advanced** _(or use an existing Function App if you like)_
* Enter a globally unique name for the function app. _(will used in host name [function app name].azurewebsites.net)_
* Select Runtime: **.NET Core 3.1**
* Select an OS: **Windows** _(Linux probably also works, but not tested)_
* Select an OS: **Windows** _(Linux also works)_
* Select a hosting plan: _Refer to Microsoft [documentation](https://azure.microsoft.com/en-us/pricing/details/functions/) about this. The simplest is Consumption_
* Select a resource group for new resources: _Create a new resource group, or use an exisiting, it's up to you._
* Select a storage account: _Create a new storage account, or use an exisiting, it's up to you._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ az storage account create `
--sku Standard_LRS

# Create a serverless function app in the resource group.
# The .NET Core 3.1 HttpTrigger function we'll deploy works on --os-type either Windows or Linux
az functionapp create `
--name $functionAppName `
--storage-account $storageName `
--consumption-plan-location $region `
--resource-group $resourceGroup `
--os-type "Windows" `
--runtime "dotnet" `
--functions-version 3

# -----------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash
region=$1
resourceGroup=$2
storageName=$3
functionAppName=$4

# -----------------------------------------------------
# Script purpose
# -----------------------------------------------------
# 1. Create Azure Function resource
# 2. Publish Azure Function HttpTrigger (written in C#) to the Azure Function resource

# -----------------------------------------------------
# Script requirements
# -----------------------------------------------------
# Azure CLI (az) and Azure Function Core Tools (func) must have been installed for script below to work.
# Azure CLI: https://docs.microsoft.com/en-us/cli/azure/?view=azure-cli-latest
# Azure Functions Core Tools: https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local

# -----------------------------------------------------
# How to run script
# -----------------------------------------------------
# Send Azure resource names below to match your existing resources in the parameters to the script.
# -arg #1 Code for region where resources will be created. Example: "westus", "eastus", "northeurope", "westeurope"
# -arg #2 Name of Resource Group that will be created
# -arg #3 Name of Storage Account that will be created. Note: name must be unique in all of Azure
# -arg #4 Name of Function App that will be created. Note: name must be unique in all of Azure

# Example:
# ./CreateAzureResourcesAndPublishFunction.sh "westus" "rg-sqteamsbridge" "stsqteamsbridge$RANDOM" "func-sqteamsbridge$RANDOM"

# -----------------------------------------------------
# Create Azure Resources
# -----------------------------------------------------
# Create a resource group.
# Uncomment line if you use an existing group.
az group create --name $resourceGroup --location $region

# Create an Azure storage account in the resource group.
# Uncomment line if you use an existing group.
az storage account create \
--name $storageName \
--location $region \
--resource-group $resourceGroup \
--sku Standard_LRS

# Create a serverless function app in the resource group.
# The .NET Core 3.1 HttpTrigger function we'll deploy works on --os-type either Windows or Linux
az functionapp create \
--name $functionAppName \
--storage-account $storageName \
--consumption-plan-location $region \
--resource-group $resourceGroup \
--os-type "Windows" \
--runtime "dotnet" \
--functions-version 3

# -----------------------------------------------------
# Publish Azure Function application
# -----------------------------------------------------
#Note: As the local caching of the Azure CLI (az) vs the Azure Functions Core Tools (func) is different,
# it can take some time for the "func" command to detect a newly created Function Application with "az".
# Can (probably) be worked around with a sleep delay, ref: https://github.com/Azure/azure-functions-core-tools/issues/1616
sleep 30

# Publish Azure Function Application (it will build and publish SonarqubeMSTeamsBridge.csproj in current folder)
func azure functionapp publish $functionAppName

# When the publish command has completed, it will display the URL used for the SonarqubeMSTeamsBridge function.
# Result from publish command_:
#Functions in func-sqteamsbridgeXXXX:
# SonarqubeMSTeamsBridge - [httpTrigger]
# Invoke url: https://func-sqteamsbridgeXXXXX.azurewebsites.net/api/sonarqubemsteamsbridge

#Afterwards this command can get you the URL also (replace last parameter with the name of the Function resource that was created )
#func azure functionapp list-functions "func-sqteamsbridgeXXXX"

## *** This is the URL you set in the Sonarqube Webhook ***

# -----------------------------------------------------
# List created resources
# -----------------------------------------------------
az resource list --resource-group $resourceGroup --output table

# Query resources created
#az group list --query "[?contains(name, 'sqteamsbridge')]"
#az storage account list --query "[?contains(name, 'sqteamsbridge')]"
#az functionapp list --query "[?contains(name, 'sqteamsbridge')]"

39 changes: 39 additions & 0 deletions src/SonarqubeMSTeamsBridge/PublishFunctionOnly.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash
functionAppName=$1

# -----------------------------------------------------
# Script purpose
# -----------------------------------------------------
# Publish Azure Function HttpTrigger (written in C#) to existing Azure Function resource.
# It creates or updates existing HttpTrigger.
# To create required Azure Resources (and Publish Azure Function), see script CreateAzureResourcesAndPublishFunction.ps1

# -----------------------------------------------------
# Script requirements
# -----------------------------------------------------
# Azure Function Core Tools (func) must have been installed for script below to work.
# Azure Functions Core Tools: https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local

# -----------------------------------------------------
# How to run script
# -----------------------------------------------------
# Set new or existing Azure Function app name.
# Example:
# ./PublishFunctionOnly.sh "func-sqteamsbridge5678"

# -----------------------------------------------------
# Publish Azure Function application
# -----------------------------------------------------
# Publish Azure Function Application (it will build and publish SonarqubeMSTeamsBridge.csproj in current folder)
func azure functionapp publish $functionAppName

# When the publish command has completed, it will display the URL used for the SonarqubeMSTeamsBridge function.
# Result from publish command_:
#Functions in func-sqteamsbridgeXXXX:
# SonarqubeMSTeamsBridge - [httpTrigger]
# Invoke url: https://func-sqteamsbridgeXXXXX.azurewebsites.net/api/sonarqubemsteamsbridge

#Afterwards this command can get you the URL also (replace last parameter with the name of the Function resource that was created )
#func azure functionapp list-functions "func-sqteamsbridgeXXXX"

## *** This is the URL you set in the Sonarqube Webhook ***

0 comments on commit 16c335c

Please sign in to comment.