Skip to content

Commit

Permalink
Add GHA workflow for API registration (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinyoo authored Apr 23, 2024
1 parent 9da46d2 commit 2abab6a
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 1 deletion.
62 changes: 62 additions & 0 deletions .github/workflows/register-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Register API Definition to Azure API Center

on:
workflow_dispatch:
inputs:
resource-id:
description: 'The resource ID of the API Center'
required: false
default: ''
resource-group:
description: 'The resource group name of the API Center'
required: false
default: ''
api-center-service:
description: 'The service name of the API Center'
required: false
default: ''
file-location:
description: 'The file path relative to the repository root'
required: false
default: ''

permissions:
id-token: write
contents: read

jobs:
register-api:
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_ENV_NAME: ${{ vars.AZURE_ENV_NAME }}
AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}

steps:
- name: Checkout
uses: actions/checkout@v4

- 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
- name: Login to Azure
uses: Azure/login@v2
with:
client-id: ${{ env.AZURE_CLIENT_ID }}
subscription-id: ${{ env.AZURE_SUBSCRIPTION_ID }}
tenant-id: ${{ env.AZURE_TENANT_ID }}

- name: Register API
shell: pwsh
run: |
./infra/scripts/New-ApiRegistration.ps1 `
-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 }}"
82 changes: 82 additions & 0 deletions infra/scripts/New-ApiRegistration.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Registers API to API Center.
Param(
[string]
[Parameter(Mandatory=$false)]
$ResourceId = "",

[string]
[Parameter(Mandatory=$false)]
$ResourceGroup = "",

[string]
[Parameter(Mandatory=$false)]
$ApiCenterService = "",

[string]
[Parameter(Mandatory=$false)]
$FileLocation = "",

[string]
[Parameter(Mandatory=$false)]
$ApiVersion = "2024-03-01",

[switch]
[Parameter(Mandatory=$false)]
$Help
)

function Show-Usage {
Write-Output " This registers API to API Center
Usage: $(Split-Path $MyInvocation.ScriptName -Leaf) ``
[-ResourceId <Resource ID>] ``
[-ResourceGroup <Resource group>] ``
[-ApiCenterService <API Center instance name>] ``
[-FileLocation <File location to register>] ``
[-ApiVersion <API version>] ``
[-Help]
Options:
-ResourceId Resource ID. It must be provided unless `ResourceGroup` is provided.
-ResourceGroup Resource group. It must be provided unless `ResourceId` is provided.
-ApiCenterService API Center instance name. It must be provided unless `ResourceId` is provided.
-FileLocation File location to register.
-ApiVersion REST API version. Default is `2024-03-01`.
-Help: Show this message.
"

Exit 0
}

# Show usage
$needHelp = $Help -eq $true
if ($needHelp -eq $true) {
Show-Usage
Exit 0
}

if (($ResourceId -eq "") -and ($ResourceGroup -eq "" -or $ApiCenterService)) {
Write-Output "`ResourceId` must be provided, or both `ResourceGroup` and `ApiCenterService` must be provided"
Exit 0
}
if ($FileLocation -eq "") {
Write-Output "`FileLocation` must be provided"
Exit 0
}

$segments = $ResourceId.Split("/", [System.StringSplitOptions]::RemoveEmptyEntries)
if ($ResourceGroup -eq "") {
$ResourceGroup = $segments[3]
}
if ($ApiCenterService -eq "") {
$ApiCenterService = $segments[7]
}

$REPOSITORY_ROOT = git rev-parse --show-toplevel

$registered = az apic api register `
-g $ResourceGroup `
-s $ApiCenterService `
--api-location "$REPOSITORY_ROOT/$FileLocation"
2 changes: 1 addition & 1 deletion infra/scripts/Set-ApiMetadata.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function Show-Usage {
-MetadataValue Metadata value.
-ApiVersion REST API version. Default is `2024-03-01`.
-Help: Show this message.
-Help: Show this message.
"

Exit 0
Expand Down
Binary file not shown.

0 comments on commit 2abab6a

Please sign in to comment.