|
| 1 | +# GitHub action that builds and deploys an Azure Function App |
| 2 | +# Author: Francisco Leyva |
| 3 | +name: Deploy Solidus Functions to Azure |
| 4 | + |
| 5 | +# Controls when the workflow will run |
| 6 | +on: |
| 7 | + # Triggers the workflow on push or pull request events but only for the "main" branch |
| 8 | + push: |
| 9 | + branches: [ "main" ] |
| 10 | + paths: |
| 11 | + - "SolidusFunctions/**" |
| 12 | + |
| 13 | + # Allows you to run this workflow manually from the Actions tab |
| 14 | + workflow_dispatch: |
| 15 | + inputs: |
| 16 | + # Update API Manager with the function endpoints (OpenAPI) |
| 17 | + update_api: |
| 18 | + type: boolean |
| 19 | + required: true |
| 20 | + default: false |
| 21 | + description: Update API Endpoints |
| 22 | + |
| 23 | +env: |
| 24 | + # Azure functions configuration |
| 25 | + AZURE_FUNCTIONAPP_NAME : 'fn-solidus-dev' |
| 26 | + AZURE_FUNCTIONAPP_PACKAGE_PATH: 'SolidusFunctions' # set this to the path to your web app project, defaults to the repository root |
| 27 | + DOTNET_VERSION: '6.0.x' # set this to the dotnet version to use |
| 28 | + |
| 29 | +# A workflow run is made up of one or more jobs that can run sequentially or in parallel |
| 30 | +jobs: |
| 31 | + # This job builds and deploy an azure function to Azure |
| 32 | + build-and-deploy: |
| 33 | + # The type of runner that the job will run on |
| 34 | + runs-on: ubuntu-latest |
| 35 | + |
| 36 | + # Steps represent a sequence of tasks that will be executed as part of the job |
| 37 | + steps: |
| 38 | + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it |
| 39 | + - uses: actions/checkout@v3 |
| 40 | + |
| 41 | + # Login to Azure using secrets credentials |
| 42 | + - name: 'Login via Azure CLI' |
| 43 | + uses: azure/login@v1 |
| 44 | + with: |
| 45 | + creds: ${{ secrets.AZURE_CREDENTIALS }} # Your Azure credentials |
| 46 | + |
| 47 | + # Setup dotnet command |
| 48 | + - name: Setup DotNet ${{ env.DOTNET_VERSION }} Environment |
| 49 | + uses: actions/setup-dotnet@v3 |
| 50 | + with: |
| 51 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 52 | + |
| 53 | + # Build Azure Function |
| 54 | + - name: 'Run dotnet' |
| 55 | + shell: pwsh |
| 56 | + run: | |
| 57 | + pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}' |
| 58 | + dotnet build --configuration Release --output ./output |
| 59 | + popd |
| 60 | + # Deploy build to Azure function app environment |
| 61 | + - name: 'Run Azure Functions Action' |
| 62 | + uses: Azure/functions-action@v1 |
| 63 | + with: |
| 64 | + app-name: ${{ env.AZURE_FUNCTIONAPP_NAME }} |
| 65 | + package: '${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}/output' |
| 66 | + |
| 67 | + # Azure logout |
| 68 | + - name: logout |
| 69 | + run: | |
| 70 | + az logout |
| 71 | + if: always() |
| 72 | + |
| 73 | + # This job updates the Azure (APIM) API with the OpenAPI definition file |
| 74 | + # coming from the Azure Function |
| 75 | + update-api: |
| 76 | + needs: build-and-deploy |
| 77 | + if: inputs.update_api |
| 78 | + # The type of runner that the job will run on |
| 79 | + runs-on: ubuntu-latest |
| 80 | + |
| 81 | + # Azure API Import configuration (optional) |
| 82 | + env: |
| 83 | + AZURE_RG: ${{ secrets.AZURE_RESOURCE_GROUP }} # Resource group name |
| 84 | + AZURE_API_SERVICE: ${{ secrets.AZURE_API_MANAGER }} # API Manager ID |
| 85 | + AZURE_API_ID: 'solidus-api' |
| 86 | + AZURE_API_PATH: '/solidus' |
| 87 | + AZURE_API_NAME: 'Solidus API' |
| 88 | + AZURE_API_SPEC_URL: 'https://fn-solidus-dev.azurewebsites.net/api/openapi/v3.json' |
| 89 | + |
| 90 | + # Steps represent a sequence of tasks that will be executed as part of the job |
| 91 | + steps: |
| 92 | + # Login to Azure using secrets credentials |
| 93 | + - name: 'Login via Azure CLI' |
| 94 | + uses: azure/login@v1 |
| 95 | + with: |
| 96 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 97 | + |
| 98 | + # Run Azure CLI command to import an API definition from an OpenID definition file |
| 99 | + - name: Run Azure API Import (CLI) |
| 100 | + run: | |
| 101 | + az apim api import --path $AZURE_API_PATH --api-id $AZURE_API_ID -g $AZURE_RG -n $AZURE_API_SERVICE --display-name "$AZURE_API_NAME" --specification-url "$AZURE_API_SPEC_URL" --specification-format OpenAPI --protocols https |
| 102 | + |
| 103 | + # Azure logout |
| 104 | + - name: logout |
| 105 | + run: | |
| 106 | + az logout |
| 107 | + if: always() |
0 commit comments