Skip to content

Instantly share code, notes, and snippets.

@beachside-project
Created October 3, 2022 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beachside-project/542c477eb78d68b4613357f8c2f4837e to your computer and use it in GitHub Desktop.
Save beachside-project/542c477eb78d68b4613357f8c2f4837e to your computer and use it in GitHub Desktop.
GitHub Actions: Web API and APIM APIs CI/CD sample
name: Web API and APIM APIs CI/CD
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
workflow_dispatch:
permissions:
id-token: write
contents: read
env:
API_DLL_NAME: WebApi1.dll
SWAGGER_FILE_NAME: swagger.json
OPENAPI_VERSION: v2
AZURE_RESOURCE_GROUP: rg-apim-cicd-hack
AZURE_WEBAPP_NAME: app-eru-api1
AZURE_APIM_SERVICE_NAME: apim-eru
AZURE_APIM_DISPLAY_NAME: sample-apis
AZURE_APIM_API_ID: sample-apis
AZURE_APIM_NAME: apim-eru
AZURE_APIM_SERVICE_URL: https://app-eru-api1.azurewebsites.net
AZURE_APIM_PATH: api
jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: WebApis/WebApi1
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: 6.0.x
- name: Install Swashbuckle.AspNetCore.Cli
run: dotnet tool install --version 6.4.0 Swashbuckle.AspNetCore.Cli
- name: Build
run: dotnet build -c Release -o ${{ github.workspace }}/output/${{ env.AZURE_WEBAPP_NAME }}
- name: Generate a OpenAPI document
run: dotnet swagger tofile --output ${{ github.workspace }}/output/${{ env.SWAGGER_FILE_NAME }} ${{ github.workspace }}/output/${{ env.AZURE_WEBAPP_NAME }}/${{ env.API_DLL_NAME }} ${{ env.OPENAPI_VERSION }}
- name: Upload a API artifact
uses: actions/upload-artifact@v3
with:
name: ${{ env.AZURE_WEBAPP_NAME }}
path: ${{ github.workspace }}/output/${{ env.AZURE_WEBAPP_NAME }}
if-no-files-found: error
- name: Upload a OpenAPI document
uses: actions/upload-artifact@v3
with:
name: ${{ env.SWAGGER_FILE_NAME }}
path: ${{ github.workspace }}/output/${{ env.SWAGGER_FILE_NAME }}
if-no-files-found: error
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Download a Web API artifact
uses: actions/download-artifact@v3.0.0
with:
name: ${{ env.AZURE_WEBAPP_NAME }}
path: api
- name: Download a OpenAPI document
uses: actions/download-artifact@v3.0.0
with:
name: ${{ env.SWAGGER_FILE_NAME }}
- name: Az CLI login via OICD
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Deploy web app via OIDC
uses: azure/webapps-deploy@v2
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
package: api
- name: Upadate APIM APIs
run: az apim api import --resource-group ${{ env.AZURE_RESOURCE_GROUP }} --service-name ${{ env.AZURE_APIM_SERVICE_NAME }} --display-name ${{ env.AZURE_APIM_DISPLAY_NAME }} --api-id ${{ env.AZURE_APIM_API_ID }} --service-url ${{ env.AZURE_APIM_SERVICE_URL }} --path ${{ env.AZURE_APIM_PATH }} --specification-format OpenApiJson --specification-path ${{ env.SWAGGER_FILE_NAME }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment