diff --git a/.github/actions/set-variables/action.yml b/.github/actions/set-variables/action.yml new file mode 100644 index 0000000..370c71d --- /dev/null +++ b/.github/actions/set-variables/action.yml @@ -0,0 +1,28 @@ +name: 'Set Variable' +description: 'Convert your variables.json file in .github/variables into environment variables to be used within the scope of a job.' +inputs: + variableFileName: + description: 'Name of variable file. File must be of format {"variables": [{"name": "variable1","value": "variable1value"}]}' + required: true + default: 'variable' +outputs: + status: + description: "Status" + value: "Pass" +runs: + using: "composite" + steps: + - run: echo Setting variables from following file ${{ inputs.variableFileName }}.json + shell: bash + + - name: Set Environment Variables - ${{ inputs.variableFileName }}.json + shell: bash + run: | + variablePath='${{ inputs.variableFileName }}.json' + while read variable; do + key=$(jq -r '.name' <<< $variable) + value=$(jq -r '.value' <<< $variable) + echo $key + echo $value + echo "$key=$value" >> $GITHUB_ENV + done <<< $(jq -c '.variables[]' $variablePath) \ No newline at end of file diff --git a/.github/actions/set-variables/readme.md b/.github/actions/set-variables/readme.md new file mode 100644 index 0000000..1d5e824 --- /dev/null +++ b/.github/actions/set-variables/readme.md @@ -0,0 +1,39 @@ +# GitHub Action - Set Variables +Are you missing the Azure DevOps Library variable groups in GitHub Actions? +If yes, then this action will act as a substitution of Library variable groups in GitHub. [Currently only supports windows and ubuntu agents] + +This is a GitHub Action used to convert variables.json files into environment variables. + +## 1. Add your variable files as json in this folder - .github/variables/{variable_file_name}.json +The format of the file should be: +``` +{ + "variables": [ + { + "name": "variable1", + "value": "variable1value" + }, + { + "name": "variable2", + "value": "variable2value" + } + ] +} +``` + +## 2. Now in your job, to use these variables, add the following action into your job +``` +- name: Set Variable + uses: deep-mm/set-variables@v1.0 + with: + # Name of variable file + variableFileName: 'variables' #Dont write .json here +``` +The variables in your variables.json file will now be converted to environment variables that you can use in the next steps of the same job. +Ths scope of variables is only the job and not the entire workflow. + +## 3. Use these variables in next steps +``` +${{ env.variable1 }} +${{ env.variable2 }} +``` \ No newline at end of file diff --git a/.github/variables/dev.json b/.github/variables/dev.json new file mode 100644 index 0000000..e3bdb85 --- /dev/null +++ b/.github/variables/dev.json @@ -0,0 +1,12 @@ +{ + "variables": [ + { + "name": "resourceGroup", + "value": "rg-dev" + }, + { + "name": "webAppName", + "value": "app-0a1b2c-dev" + } + ] + } \ No newline at end of file diff --git a/.github/variables/prod.json b/.github/variables/prod.json new file mode 100644 index 0000000..7d107d4 --- /dev/null +++ b/.github/variables/prod.json @@ -0,0 +1,16 @@ +{ + "variables": [ + { + "name": "deploymentSlotName", + "value": "stage" + }, + { + "name": "resourceGroup", + "value": "rg-prod" + }, + { + "name": "webAppName", + "value": "app-0a1b2c" + } + ] + } \ No newline at end of file diff --git a/.github/variables/qa.json b/.github/variables/qa.json new file mode 100644 index 0000000..0aaff55 --- /dev/null +++ b/.github/variables/qa.json @@ -0,0 +1,12 @@ +{ + "variables": [ + { + "name": "resourceGroup", + "value": "rg-qa" + }, + { + "name": "webAppName", + "value": "app-0a1b2c-dev" + } + ] + } \ No newline at end of file diff --git a/.github/workflows/continuous-delivery.yml b/.github/workflows/continuous-delivery.yml index 80335cb..47d6254 100644 --- a/.github/workflows/continuous-delivery.yml +++ b/.github/workflows/continuous-delivery.yml @@ -12,16 +12,6 @@ on: pull_request: branches: [main] -env: - deploymentSlotName: stage - devResourceGroup: rg-dev - devUrl: https://app-0a1b2c-dev.azurewebsites.net - prodResourceGroup: rg-prod - prodUrl: https://app-0a1b2c.azurewebsites.net - qaResourceGroup: rg-qa - qaUrl: https://app-0a1b2c-qa.azurewebsites.net - stageUrl: https://app-0a1b2c-stage.azurewebsites.net - jobs: # Build Application @@ -32,10 +22,8 @@ jobs: # Checks-out the repository under $GITHUB_WORKSPACE, so the job can access it - uses: actions/checkout@v2 - - name: Build Steps Here - run: echo "Running build steps..." - # Publish the artificats so they are available by subsequent jobs + # Uploading application to build artifact - name: Upload ARM Templates as Artifact continue-on-error: false @@ -44,7 +32,6 @@ jobs: name: infra path: infra - # Publish the artificats so they are available by subsequent jobs # Uploading application to build artifact - name: Upload Application Build as Artifact continue-on-error: false @@ -53,6 +40,14 @@ jobs: name: build path: src + # Uploading variables folder to build artifact + - name: Upload Application Build as Artifact + continue-on-error: false + uses: actions/upload-artifact@v2 + with: + name: variables + path: .github/variables + # Deploy to the Development servers DeployDev: @@ -62,13 +57,18 @@ jobs: runs-on: ubuntu-latest environment: name: Development - url: ${{ env.devUrl }} steps: # Download Artifacts - name: Download Artifacts uses: actions/download-artifact@v2 continue-on-error: false + + # Load Environment Variables from local file + - name: Load Environment Variables + uses: fredcicles/github-actions/.github/actions/set-variables@samples/variable-files + with: + variableFileName: ./variables/dev # Log in to Azure - uses: azure/login@v1 @@ -80,15 +80,15 @@ jobs: id: deployStep with: subscriptionId: ${{ secrets.SUBSCRIPTIONID }} - resourceGroupName: ${{ env.devResourceGroup }} + resourceGroupName: ${{ env.resourceGroup }} template: ./infra/azuredeploy.json - parameters: ./infra/azuredeploy.dev.parameters.json + parameters: ./infra/azuredeploy.dev.parameters.json webAppName=${{ env.webAppName }} # Deploy code to web app - name: Deploy code to the WebApp uses: Azure/webapps-deploy@v2 with: - app-name: ${{ steps.deployStep.outputs.webAppName }} + app-name: ${{ env.webAppName }} package: ./build - name: Deployment Message @@ -103,13 +103,18 @@ jobs: runs-on: ubuntu-latest environment: name: QA - url: ${{ env.qaUrl }} steps: # Download Artifacts - name: Download Artifacts uses: actions/download-artifact@v2 continue-on-error: false + + # Load Environment Variables from local file + - name: Load Environment Variables + uses: fredcicles/github-actions/.github/actions/set-variables@samples/variable-files + with: + variableFileName: ./variables/qa # Log in to Azure - uses: azure/login@v1 @@ -121,15 +126,15 @@ jobs: id: deployStep with: subscriptionId: ${{ secrets.SUBSCRIPTIONID }} - resourceGroupName: ${{ env.qaResourceGroup }} + resourceGroupName: ${{ env.resourceGroup }} template: ./infra/azuredeploy.json - parameters: ./infra/azuredeploy.qa.parameters.json + parameters: ./infra/azuredeploy.qa.parameters.json webAppName=${{ env.webAppName }} # Deploy code to web app - name: Deploy code to the WebApp uses: Azure/webapps-deploy@v2 with: - app-name: ${{ steps.deployStep.outputs.webAppName }} + app-name: ${{ env.webAppName }} package: ./build - name: Deployment Message @@ -143,15 +148,18 @@ jobs: runs-on: ubuntu-latest environment: name: Stage - url: ${{ env.stageUrl }} - outputs: - webAppName: ${{ steps.deployStep.outputs.webAppName }} steps: # Download Artifacts - name: Download Artifacts uses: actions/download-artifact@v2 continue-on-error: false + + # Load Environment Variables from local file + - name: Load Environment Variables + uses: fredcicles/github-actions/.github/actions/set-variables@samples/variable-files + with: + variableFileName: ./variables/prod # Log in to Azure - uses: azure/login@v1 @@ -163,15 +171,15 @@ jobs: id: deployStep with: subscriptionId: ${{ secrets.SUBSCRIPTIONID }} - resourceGroupName: ${{ env.prodResourceGroup }} + resourceGroupName: ${{ env.resourceGroup }} template: ./infra/azuredeploy.json - parameters: ./infra/azuredeploy.prod.parameters.json + parameters: ./infra/azuredeploy.prod.parameters.json webAppName=${{ env.webAppName }} # Deploy code to web app - name: Deploy code to the WebApp uses: Azure/webapps-deploy@v2 with: - app-name: ${{ steps.deployStep.outputs.webAppName }} + app-name: ${{ env.webAppName }} slot-name: ${{ env.deploymentSlotName }} package: ./build @@ -186,11 +194,18 @@ jobs: runs-on: ubuntu-latest environment: name: Production - url: ${{ env.prodUrl }} steps: - - name: Deploy to Production - run: echo "Deploy Application to Production" + # Download Artifacts + - name: Download Artifacts + uses: actions/download-artifact@v2 + continue-on-error: false + + # Load Environment Variables from local file + - name: Load Environment Variables + uses: fredcicles/github-actions/.github/actions/set-variables@samples/variable-files + with: + variableFileName: ./variables/prod # Log in to Azure - uses: azure/login@v1 @@ -202,4 +217,4 @@ jobs: - uses: azure/cli@v1 with: inlineScript: | - az webapp deployment slot swap -g ${{ env.prodResourceGroup }} -n ${{ needs.DeployStage.outputs.webAppName }} -s ${{ env.deploymentSlotName }} + az webapp deployment slot swap -g ${{ env.resourceGroup }} -n ${{ env.webAppName }} -s ${{ env.deploymentSlotName }} diff --git a/infra/azuredeploy.dev.parameters.json b/infra/azuredeploy.dev.parameters.json index 5f763c1..010708b 100644 --- a/infra/azuredeploy.dev.parameters.json +++ b/infra/azuredeploy.dev.parameters.json @@ -12,7 +12,7 @@ "value": "asp-0a1b2c-dev" }, "webAppName": { - "value": "app-0a1b2c-dev" + "value": "web-app-dev" } } } \ No newline at end of file diff --git a/infra/azuredeploy.prod.parameters.json b/infra/azuredeploy.prod.parameters.json index a25e3ee..4ef4930 100644 --- a/infra/azuredeploy.prod.parameters.json +++ b/infra/azuredeploy.prod.parameters.json @@ -12,7 +12,7 @@ "value": "asp-0a1b2c" }, "webAppName": { - "value": "app-0a1b2c" + "value": "web-app-prod" } } } \ No newline at end of file diff --git a/infra/azuredeploy.qa.parameters.json b/infra/azuredeploy.qa.parameters.json index 68b6a6b..38655ae 100644 --- a/infra/azuredeploy.qa.parameters.json +++ b/infra/azuredeploy.qa.parameters.json @@ -12,7 +12,7 @@ "value": "asp-0a1b2c-qa" }, "webAppName": { - "value": "app-0a1b2c-qa" + "value": "web-app-qa" } } } \ No newline at end of file