Skip to content

Commit

Permalink
Update API registration part (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinyoo authored May 9, 2024
1 parent 988ae47 commit dc76540
Show file tree
Hide file tree
Showing 25 changed files with 1,048 additions and 104 deletions.
139 changes: 71 additions & 68 deletions .github/workflows/azure-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ permissions:
contents: read

jobs:
build-dotnet:
build:
runs-on: ubuntu-latest

env:
AZURE_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID }}
AZURE_TENANT_ID: ${{ vars.AZURE_TENANT_ID }}
AZURE_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }}
AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}
AZURE_ENV_NAME: ${{ vars.AZURE_ENV_NAME }}
AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}
AZD_INITIAL_ENVIRONMENT_CONFIG: ${{ secrets.AZD_INITIAL_ENVIRONMENT_CONFIG }}

steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -34,85 +43,79 @@ jobs:
with:
dotnet-version: '8.x'

- name: Restore NuGet packages
shell: bash
run: |
dotnet restore ./dotnet
- name: Build .NET Apps
shell: bash
run: |
dotnet build ./dotnet -c Release
- name: Publish .NET Apps
shell: bash
run: |
dotnet publish ./dotnet/src/ApiApp -c Release -o ./dotnet/publish/ApiApp
dotnet publish ./dotnet/src/WebApp -c Release -o ./dotnet/publish/WebApp
- name: Upload artifacts
uses: actions/upload-artifact@v2
with:
name: dotnet
path: ./dotnet/publish

deploy-dotnet:
if: ${{ vars.AZURE_CLIENT_ID != '' }}
runs-on: ubuntu-latest
needs:
- build-dotnet

env:
AZURE_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID }}
AZURE_TENANT_ID: ${{ vars.AZURE_TENANT_ID }}
AZURE_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }}
AZURE_ENV_NAME: ${{ vars.AZURE_ENV_NAME }}
AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}

steps:
- name: Download artifacts
uses: actions/download-artifact@v2
- name: Setup node.js
uses: actions/setup-node@v4
with:
name: dotnet
path: ./dotnet/publish
node-version: 'lts/Iron'

- name: Install SWA CLI
shell: bash
run: |
npm install --global @azure/static-web-apps-cli@latest
- name: Login to Azure
- name: Install azd
uses: Azure/setup-azd@v1.0.0

- name: Log in with Azure Developer CLI (Federated Credentials)
if: ${{ env.AZURE_CLIENT_ID != '' }}
run: |
azd auth login `
--client-id "$Env:AZURE_CLIENT_ID" `
--federated-credential-provider "github" `
--tenant-id "$Env:AZURE_TENANT_ID"
shell: pwsh

- name: Login to Azure CLI (Federated Credentials)
if: ${{ env.AZURE_CLIENT_ID != '' }}
uses: Azure/login@v2
with:
client-id: ${{ env.AZURE_CLIENT_ID }}
subscription-id: ${{ env.AZURE_SUBSCRIPTION_ID }}
tenant-id: ${{ env.AZURE_TENANT_ID }}

- name: Get API app name
id: get-apiapp-name
shell: bash
- name: Log in with Azure Developer CLI (Client Credentials)
if: ${{ env.AZURE_CREDENTIALS != '' }}
run: |
export AZURE_WEBAPP_NAME=$(az webapp list -g rg-${{ env.AZURE_ENV_NAME }} --query "[].name | [?contains(@, 'dotnet')]" -o tsv)
echo "AZURE_WEBAPP_NAME=$AZURE_WEBAPP_NAME" >> "$GITHUB_OUTPUT"
- name: Deploy to Azure - API app
uses: azure/webapps-deploy@v3
$info = $Env:AZURE_CREDENTIALS | ConvertFrom-Json -AsHashtable;
Write-Host "::add-mask::$($info.clientSecret)"
azd auth login `
--client-id "$($info.clientId)" `
--client-secret "$($info.clientSecret)" `
--tenant-id "$($info.tenantId)"
shell: pwsh
env:
AZURE_CREDENTIALS: ${{ env.AZURE_CREDENTIALS }}

- name: Login to Azure CLI (Client Credentials)
if: ${{ env.AZURE_CREDENTIALS != '' }}
uses: Azure/login@v2
with:
app-name: ${{ steps.get-apiapp-name.outputs.AZURE_WEBAPP_NAME }}
package: ./dotnet/publish/ApiApp

- name: Get Static Web app name
id: get-sttapp-name
shell: bash
run: |
export AZURE_STTAPP_NAME=$(az staticwebapp list -g rg-${{ env.AZURE_ENV_NAME }} --query "[].name | [?contains(@, 'dotnet')]" -o tsv)
export AZURE_STTAPP_DEPLOYMENT_KEY=$(az staticwebapp secrets list -g rg-${{ env.AZURE_ENV_NAME }} -n $AZURE_STTAPP_NAME --query "properties.apiKey" -o tsv)
echo "AZURE_STTAPP_NAME=$AZURE_STTAPP_NAME" >> "$GITHUB_OUTPUT"
echo "::add-mask::$AZURE_STTAPP_DEPLOYMENT_KEY"
echo "AZURE_STTAPP_DEPLOYMENT_KEY=$AZURE_STTAPP_DEPLOYMENT_KEY" >> "$GITHUB_OUTPUT"
- name: Deploy to Azure - Static Web app
shell: bash
run: |
swa deploy ./dotnet/publish/WebApp/wwwroot --env production --deployment-token ${{ steps.get-sttapp-name.outputs.AZURE_STTAPP_DEPLOYMENT_KEY }}
creds: ${{ env.AZURE_CREDENTIALS }}

- name: Provision Infrastructure
if: ${{ env.AZURE_CLIENT_ID != '' || env.AZURE_CREDENTIALS != '' }}
run: azd provision --no-prompt
env:
AZURE_ENV_NAME: ${{ env.AZURE_ENV_NAME }}
AZURE_LOCATION: ${{ env.AZURE_LOCATION }}
AZURE_SUBSCRIPTION_ID: ${{ env.AZURE_SUBSCRIPTION_ID }}
AZD_INITIAL_ENVIRONMENT_CONFIG: ${{ env.AZD_INITIAL_ENVIRONMENT_CONFIG }}

- name: Build Artifacts
if: ${{ env.AZURE_CLIENT_ID != '' || env.AZURE_CREDENTIALS != '' }}
run: azd package --all --no-prompt
env:
AZURE_ENV_NAME: ${{ env.AZURE_ENV_NAME }}
AZURE_LOCATION: ${{ env.AZURE_LOCATION }}
AZURE_SUBSCRIPTION_ID: ${{ env.AZURE_SUBSCRIPTION_ID }}
AZD_INITIAL_ENVIRONMENT_CONFIG: ${{ env.AZD_INITIAL_ENVIRONMENT_CONFIG }}

- name: Deploy Application
if: ${{ env.AZURE_CLIENT_ID != '' || env.AZURE_CREDENTIALS != '' }}
run: azd deploy --all --no-prompt
env:
AZURE_ENV_NAME: ${{ env.AZURE_ENV_NAME }}
AZURE_LOCATION: ${{ env.AZURE_LOCATION }}
AZURE_SUBSCRIPTION_ID: ${{ env.AZURE_SUBSCRIPTION_ID }}
AZD_INITIAL_ENVIRONMENT_CONFIG: ${{ env.AZD_INITIAL_ENVIRONMENT_CONFIG }}
14 changes: 9 additions & 5 deletions .github/workflows/register-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ on:
description: 'The file path relative to the repository root'
required: false
default: ''
api-management-id:
description: 'The resource ID of the API Management service'
required: false
default: ''

permissions:
id-token: write
Expand All @@ -32,8 +36,8 @@ jobs:
AZURE_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID }}
AZURE_TENANT_ID: ${{ vars.AZURE_TENANT_ID }}
AZURE_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }}
AZURE_ENV_NAME: ${{ vars.AZURE_ENV_NAME }}
AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}
# AZURE_ENV_NAME: ${{ vars.AZURE_ENV_NAME }}
# AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}

steps:
- name: Checkout
Expand All @@ -42,8 +46,7 @@ jobs:
- name: Install APIC extension
shell: bash
run: |
# az extension add --name apic-extension --allow-preview true --yes
az extension add --source ./infra/scripts/apic_extension-1.0.0b4-py3-none-any.whl --allow-preview true --yes
az extension add --name apic-extension --allow-preview true --yes
- name: Login to Azure
uses: Azure/login@v2
Expand All @@ -59,4 +62,5 @@ jobs:
-ResourceId "${{ github.event.inputs.resource-id }}" `
-ResourceGroup "${{ github.event.inputs.resource-group }}" `
-ApiCenterService "${{ github.event.inputs.api-center-service }}" `
-FileLocation "${{ github.event.inputs.file-location }}"
-FileLocation "${{ github.event.inputs.file-location }}" `
-ApiManagementId ${{ github.event.inputs.api-management-id }} `
133 changes: 118 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,23 @@ Throughout this reference sample, we'd like to give developer experiences how to

1. Fork this repository.
1. Clone the forked repository to your local machine.
1. Login to Azure Developer CLI.
1. Log in with the following command. Then, you will be able to use the `azd` cli to quickly provision and deploy the application.

```bash
# Authenticate with Azure Developer CLI
azd auth login

# Authenticate with Azure CLI
az login
```

1. Provision all resources to Azure and deploy all the apps to those resources.
1. Run `azd up` to provision all the resources to Azure and deploy the code to those resources.

```bash
azd up
```

## Deploy APICenter Analyzer (Optional)
## APICenter Analyzer Integration (Optional)

APICenter Analyzer is a tool to lint API specifications on the server-side. If you want to integrate this server-side linting feature, you can install it by following steps.

Expand All @@ -59,33 +63,132 @@ APICenter Analyzer is a tool to lint API specifications on the server-side. If y

## CI/CD Pipelines

As a default, CI/CD pipelines are disabled. To enable CI/CD pipelines, follow these steps.
If you want to integrate the CI/CD pipeline with GitHub Actions, you can use the following command to create a GitHub repository and push the code to the repository.

1. Configure the CI/CD pipeline.
1. First of all, log in to GitHub.

```bash
azd pipeline config
# Authenticate with GitHub CLI
gh auth login
```

1. Push your changes to the repository.
1. Run the following commands to update your GitHub repository variables.

```bash
# Bash
AZURE_CLIENT_ID=$(./infra/scripts/get-azdvariable.sh --key AZURE_CLIENT_ID)
azd pipeline config --principal-id $AZURE_CLIENT_ID
# PowerShell
$AZURE_CLIENT_ID = $(./infra/scripts/Get-AzdVariable.ps1 -Key AZURE_CLIENT_ID)
azd pipeline config --principal-id $AZURE_CLIENT_ID
```

1. Now, you're good to go! Push the code to the GitHub repository or manually run the GitHub Actions workflow to get your portal deployed.
## API Registration
You can register APIs to API Center in various ways.
You can register APIs to API Center in various ways. But here, we will show you how to register APIs through Azure CLI and the GitHub Actions workflow.
### Azure Portal
### Through Azure CLI
TBD
#### From local machine
### Azure CLI
You can register an API to API Center from a local machine, run the following commands:
TBD
```bash
# Bash
RESOURCE_GROUP=<RESOURCE_GROUP>
APIC_NAME=<API_CENTER_NAME>
API_DOC_FILE_PATH=<API_DOC_FILE_PATH>
az apic api register -g $RESOURCE_GROUP -s $APIC_NAME --api-location $API_DOC_FILE_PATH
# PowerShell
$RESOURCE_GROUP = "<RESOURCE_GROUP>"
$APIC_NAME = "<API_CENTER_NAME>"
$API_DOC_FILE_PATH = "<API_DOC_FILE_PATH>"
az apic api register -g $RESOURCE_GROUP -s $APIC_NAME --api-location $API_DOC_FILE_PATH
```
> **NOTE**: Replace `<RESOURCE_GROUP>`, `<API_CENTER_NAME>` and `<API_DOC_FILE_PATH>` with your values.
Alternatively, you can run the following script pre-written:
```bash
# Bash
RESOURCE_GROUP=<RESOURCE_GROUP>
APIC_NAME=<API_CENTER_NAME>
API_DOC_FILE_PATH=<API_DOC_FILE_PATH>
RESOURCE_ID=$(az apic service show -g $RESOURCE_GROUP -s $APIC_NAME --query "id" -o tsv)
./infra/scripts/new-apiregistration.sh --resource-id $RESOURCE_ID --file-location $API_DOC_FILE_PATH
# PowerShell
$RESOURCE_GROUP = "<RESOURCE_GROUP>"
$APIC_NAME = "<API_CENTER_NAME>"
$API_DOC_FILE_PATH = "<API_DOC_FILE_PATH>"
$RESOURCE_ID = $(az apic service show -g $RESOURCE_GROUP -s $APIC_NAME --query "id" -o tsv)
./infra/scripts/New-ApiRegistration.sh -ResourceId $RESOURCE_ID -FileLocation $API_DOC_FILE_PATH
```
> **NOTE**: Replace `<RESOURCE_GROUP>`, `<API_CENTER_NAME>` and `<API_DOC_FILE_PATH>` with your values.
#### From API Management
You can also register APIs to API Center directly importing from API Management. Run the following commands:
```bash
# Bash
RESOURCE_GROUP=<RESOURCE_GROUP>
APIC_NAME=<API_CENTER_NAME>
APIM_NAME=<API_MANAGEMENT_NAME>
APIM_ID=$(az resource list --namespace "Microsoft.ApiManagement" --resource-type "service" -g $RESOURCE_GROUP --query "[].id" -o tsv)
az apic service import-from-apim -g $RESOURCE_GROUP -s $APIC_NAME --source-resource-ids "$APIM_ID/apis/*"
# PowerShell
$RESOURCE_GROUP = "<RESOURCE_GROUP>"
$APIC_NAME = "<API_CENTER_NAME>"
$APIM_NAME = "<API_MANAGEMENT_NAME>"
$APIM_ID = az resource list --namespace "Microsoft.ApiManagement" --resource-type "service" -g $RESOURCE_GROUP --query "[].id" -o tsv
az apic service import-from-apim -g $RESOURCE_GROUP -s $APIC_NAME --source-resource-ids "$APIM_ID/apis/*"
```
> **NOTE**: Replace `<RESOURCE_GROUP>` `<APIC_NAME>` and `<APIM_NAME>` with your values.
Alternatively, you can run the following script pre-written:
```bash
# Bash
RESOURCE_GROUP=<RESOURCE_GROUP>
APIC_ID=$(az resource list --namespace "Microsoft.ApiCenter" --resource-type "services" -g $RESOURCE_GROUP --query "[].id" -o tsv)
APIM_ID=$(az resource list --namespace "Microsoft.ApiManagement" --resource-type "service" -g $RESOURCE_GROUP --query "[].id" -o tsv)
./infra/scripts/new-apiregistration.sh --resource-id $APIC_ID --api-management-id $APIM_ID
# PowerShell
$RESOURCE_GROUP = "<RESOURCE_GROUP>"
$APIC_ID = $(az resource list --namespace "Microsoft.ApiCenter" --resource-type "services" -g $RESOURCE_GROUP --query "[].id" -o tsv)
$APIM_ID = $(az resource list --namespace "Microsoft.ApiManagement" --resource-type "service" -g $RESOURCE_GROUP --query "[].id" -o tsv)
./infra/scripts/New-ApiRegistration.sh -ResourceId $APIC_ID -ApiManagementId $APIM_ID
```
> **NOTE**: Replace `<RESOURCE_GROUP>` with your values.
### Visual Studio Code
### Through GitHub Actions Workflow
TBD
### GitHub Actions
## Custom Metadata Management
TBD
Expand All @@ -101,7 +204,7 @@ TBD
TBD
## API Center Portal
## API Center Portal Integration (Optional)
TBD
Expand Down
Loading

0 comments on commit dc76540

Please sign in to comment.