diff --git a/.github/workflows/infraworkflow.yml b/.github/workflows/infraworkflow.yml new file mode 100644 index 0000000..cd58628 --- /dev/null +++ b/.github/workflows/infraworkflow.yml @@ -0,0 +1,54 @@ +name: workflow to deploy an ARM Template to a Subscription Scope +on: + push: + paths: + - 'ARM/**' + - '.github/workflows/infraworkflow.yml' + +env: + AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }} # set this to your Azure Subscription Id + AZURE_RESOURCEGROUP_NAME: demo-webapp-gh-actions # set this to your preferred resource group name + AZURE_WEBAPP_NAME: demo-webapp-gh-actions # set this to your preferred resource group name + ASP_NAME: demo-webapp-gh-actions # set this to your preferred resource group name + +jobs: + build-and-deploy-to-dev: + runs-on: ubuntu-latest + steps: + + # Please refer to https://github.com/Azure/login#configure-deployment-credentials for help on the Azure login action and setting up your secrets + - name: Azure Login + uses: azure/login@v1 + with: + creds: ${{ secrets.AZURE_CREDENTIALS }} + + # Checkout + - name: Checkout + uses: actions/checkout@v2 + with: + path: repo + + # Deployment of template + - name: Deploy ARM Template resourcegroup + uses: azure/arm-deploy@v1 + with: + # You can change these environment variables for your configuration: AZURE_SUBSCRIPTION_ID + scope: subscription + subscriptionId: ${{ env.AZURE_SUBSCRIPTION_ID }} + region: centralus # Set this to your target region + template: repo/ARM/azuredeploy.resourcegroup.json # Set this to the location of your template file + parameters: rgName=${{env.AZURE_RESOURCEGROUP_NAME}} rgLocation=centralus #override rgName in parameters file + + # Deployment of template + - name: Deploy ARM Template resources + uses: azure/arm-deploy@v1 + with: + # You can change these environment variables for your configuration: AZURE_SUBSCRIPTION_ID + scope: resourcegroup + subscriptionId: ${{ env.AZURE_SUBSCRIPTION_ID }} + resourceGroupName: ${{env.AZURE_RESOURCEGROUP_NAME}} + region: centralus # Set this to your target region + template: repo/ARM/azuredeploy.json # Set this to the location of your template file + parameters: webappName=${{env.AZURE_WEBAPP_NAME}} aspName=${{env.ASP_NAME}} # Set this to the location of your parameters file + +#TODO: download web app publish profile and add it as a secret to the repo \ No newline at end of file diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 4114d99..059b958 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -4,9 +4,7 @@ on: push: branches: - master - pull_request: - branches: - - '*' + - vermegi/infra # CONFIGURATION # For help, go to https://github.com/Azure/Actions # @@ -15,9 +13,10 @@ on: # # 2. Change these variables for your configuration: env: - AZURE_WEBAPP_NAME: dotnetcoreLinux # set this to your application's name + AZURE_WEBAPP_NAME: demo-webapp-gh-actions # set this to your application's name AZURE_WEBAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root DOTNET_VERSION: '2.2.402' # set this to the dot net version to use + AZURE_RESOURCEGROUP_NAME: demo-webapp-gh-actions # set this to your preferred resource group name jobs: build-and-deploy: @@ -39,12 +38,23 @@ jobs: dotnet build --configuration Release dotnet publish -c Release -o myapp - # Deploy to Azure Web apps + # Deploy to Azure Web apps staging slot - name: 'Run Azure webapp deploy action using publish profile credentials' uses: azure/webapps-deploy@v2 with: app-name: ${{ env.AZURE_WEBAPP_NAME }} # Replace with your app name publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }} # Define secret variable in repository settings as per action documentation package: '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/myapp' + slot-name: staging + + - name: Login for az cli commands + uses: azure/login@v1 + with: + creds: ${{ secrets.AZURE_CREDENTIALS }} + + - name: Swap to production slot + run: | + az webapp deployment slot swap --resource-group ${{ env.AZURE_RESOURCEGROUP_NAME }} --name ${{ env.AZURE_WEBAPP_NAME }} --slot staging --target-slot production + echo "Swap finished. App Service Application URL: https://$(az webapp show --resource-group ${{ env.AZURE_RESOURCEGROUP_NAME }} --name ${{ env.AZURE_WEBAPP_NAME }} --query hostNames[0] -o tsv)" # For more samples to get started with GitHub Action workflows to deploy to Azure, refer to https://github.com/Azure/actions-workflow-samples diff --git a/ARM/azuredeploy.json b/ARM/azuredeploy.json new file mode 100644 index 0000000..3e8f90d --- /dev/null +++ b/ARM/azuredeploy.json @@ -0,0 +1,80 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "webappName": { + "type": "string", + "metadata": { + "description": "Name of your web app" + } + }, + "aspName": { + "type": "string", + "metadata": { + "description": "Name of your app service plan" + } + }, + "aspSKU": { + "type": "string", + "metadata": { + "description": "description" + }, + "defaultValue": "S1" + } + }, + "functions": [], + "variables": {}, + "resources": [ + { + "name": "[parameters('webappName')]", + "type": "Microsoft.Web/sites", + "apiVersion": "2018-11-01", + "location": "[resourceGroup().location]", + "tags": { + "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('aspName'))]": "Resource", + "displayName": "[parameters('webappName')]" + }, + "dependsOn": [ + "[resourceId('Microsoft.Web/serverfarms', parameters('aspName'))]" + ], + "properties": { + "name": "[parameters('webappName')]", + "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('aspName'))]" + } + }, + { + "name": "[parameters('aspName')]", + "type": "Microsoft.Web/serverfarms", + "apiVersion": "2018-02-01", + "location": "[resourceGroup().location]", + "sku": { + "name": "[parameters('aspSKU')]", + "capacity": 1 + }, + "tags": { + "displayName": "[parameters('aspName')]" + }, + "properties": { + "name": "[parameters('aspName')]" + } + }, + { + "apiVersion": "2020-06-01", + "type": "Microsoft.Web/sites/slots", + "name": "[concat(parameters('webappName'), '/', 'staging')]", + "kind": "app", + "location": "[resourceGroup().location]", + "comments": "This specifies the web app slots.", + "tags": { + "displayName": "WebAppSlots" + }, + "properties": { + "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('aspName'))]" + }, + "dependsOn": [ + "[resourceId('Microsoft.Web/Sites', parameters('webappName'))]" + ] + } + ], + "outputs": {} +} \ No newline at end of file diff --git a/ARM/azuredeploy.parameters.json b/ARM/azuredeploy.parameters.json new file mode 100644 index 0000000..f84a146 --- /dev/null +++ b/ARM/azuredeploy.parameters.json @@ -0,0 +1,12 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "webappName":{ + "value": "GEN-UNIQUE" + } , + "aspName": { + "value": "GEN-UNIQUE" + } + } +} \ No newline at end of file diff --git a/ARM/azuredeploy.resourcegroup.json b/ARM/azuredeploy.resourcegroup.json new file mode 100644 index 0000000..2325339 --- /dev/null +++ b/ARM/azuredeploy.resourcegroup.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "rgName": { + "type": "string", + "metadata": { + "description": "Name of the resourceGroup to create" + } + }, + "rgLocation": { + "type": "string", + "metadata": { + "description": "Location for the resourceGroup" + } + } + }, + "variables": { }, + "resources": [ + { + "type": "Microsoft.Resources/resourceGroups", + "apiVersion": "2019-10-01", + "name": "[parameters('rgName')]", + "location": "[parameters('rgLocation')]", + "tags": { + "Note": "subscription level deployment" + }, + "properties": {} + } + ] + } \ No newline at end of file diff --git a/ARM/azuredeploy.resourcegroup.parameters.json b/ARM/azuredeploy.resourcegroup.parameters.json new file mode 100644 index 0000000..a9f3f32 --- /dev/null +++ b/ARM/azuredeploy.resourcegroup.parameters.json @@ -0,0 +1,12 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "rgName": { + "value": "GEN-UNIQUE" + }, + "rgLocation": { + "value": "southcentralus" + } + } + } \ No newline at end of file