You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Register APIs in your API center using GitHub Actions
14
14
15
-
This article shows how to set up a GitHub Actions workflow to register APIs in your organization's [API center](overview.md). [GitHub Actions](https://docs.github.com/en/actions/learn-github-actions) is ...
15
+
This article shows how to set up a basic GitHub Actions workflow to register an API in your organization's [API center](overview.md) when an API specification file is added to a GitHub repository. [GitHub Actions](https://docs.github.com/en/actions/learn-github-actions) is a CI/CD platform that automates software workflows, enabling you to build, test, and deploy your code right from GitHub.
16
16
17
17
18
-
General configuration steps are similar to those in [Deploy to App Service using GitHub Actions](../app-service/deploy-github-actions).
18
+
General configuration steps are similar to those in [Deploy to App Service using GitHub Actions](../app-service/deploy-github-actions), which provides more detail.
19
19
20
20
**Benefits of using GitHub Actions to register APIs in your API center:**
21
21
* Save time and reduces manual effort
@@ -28,53 +28,52 @@ GitHub Actions provides:
28
28
29
29
30
30
<!-- Add Video link? -->
31
+
## Prerequisites
31
32
32
-
## Scenario overview
33
+
* An API center in your Azure subscription. If you haven't created one already, see [Quickstart: Create your API center](set-up-api-center.md).
34
+
* Permissions to create a service principal in the tenant
35
+
* A GitHub account and a GitHub repo in which you can configure secrets and GitHub Actions workflows
> Azure CLI command examples in this article can run in PowerShell or a bash shell. Where needed because of different variable syntax, separate command examples are provided for the two shells.
37
43
44
+
## Scenario overview
45
+
46
+
The following diagram shows how API registration in your API center can be automated using GitHub Actions.
38
47
39
48
:::image type="content" source="media/register-apis-github-actions/scenario-overview.svg" alt-text="Diagram showing a GitHub actions workflow to register API in an Azure API center.":::
40
49
41
-
<!-- Explain steps here -->
42
50
43
-
1. Set up a GitHub Actions workflow in your repository.
51
+
1. Set up a GitHub Actions workflow in your repository that triggers when a pull request that adds an API definition file is merged.
44
52
1. Create a branch from the main branch in your GitHub repository.
45
-
1.Commit changes and push them to the new branch.
53
+
1.Add an API definition file, commit the changes, and push them to the new branch.
46
54
1. Create a pull request to merge the new branch into the main branch.
47
55
1. Merge the pull request.
48
56
1. The merge triggers a GitHub Actions workflow that registers the APIs in your API center.
49
57
50
-
51
-
## Prerequisites
52
-
53
-
* An API center in your Azure subscription. If you haven't created one already, see [Quickstart: Create your API center](set-up-api-center.md).
54
-
* Permissions to create a service principal in the tenant (--check--)
> Azure CLI command examples in this article can run in PowerShell or a bash shell. Where needed because of different variable syntax, separate command examples are provided for the two shells.
63
-
64
58
## Set up a GitHub Actions workflow
65
59
60
+
In this section, you set up the GitHub Actions workflow for this scenario:
66
61
62
+
* Create an Azure service principal to use for configuring credentials for the GitHub Actions workflow.
63
+
* Add the credentials as a secret in your GitHub repository.
64
+
* Configure a GitHub Actions workflow that triggers when a pull request adding an API definition file is merged. The workflow YAML file includes a step that uses the Azure CLI to register the API in your API center from the definition file.
67
65
68
66
69
-
### Set up the Azure login action with a service principal secret
70
-
71
-
See [steps](../app-service/deploy-github-actions?tabs=openid%2Caspnetcore#1-generate-deployment-credentials) to generate deployment credentials for your repository.
67
+
### Set up a service principal secret
72
68
69
+
In the following steps, create a Microsoft Entra service principal to use for configuring credentials for the GitHub Actions workflow. The service principal is used to authenticate with Azure to register the API.
73
70
74
71
> [!NOTE]
75
-
> he recommended way to authenticate with Azure API Center for GitHub Actions is with OpenID Connect. This is an authentication method that uses short-lived tokens. Setting up OpenID Connect with GitHub Actions is more complex but offers hardened security.
72
+
> Configuring a service principal is shown for demonstration purposes. The recommended way to authenticate with Azure for GitHub Actions is with OpenID Connect, an authentication method that uses short-lived tokens. Setting up OpenID Connect with GitHub Actions is more complex but offers hardened security. See [steps](../app-service/deploy-github-actions?tabs=openid%2Caspnetcore#1-generate-deployment-credentials).
73
+
74
+
Create a service principal using the [az ad sp create-for-rbac](/azure/ad/sp?view=azure-cli-latest#az-ad-sp-create-for-rbac) command. The following example first uses the [az apic show](/cli/azure/az/apic#az-apic-show) command to retrieve the resource ID of the API center. The service principal is then created with the Contributor role for the API center.
76
75
77
-
For example, create a service principal using the [az ad sp create-for-rbac](/azure/ad/sp?view=azure-cli-latest#az-ad-sp-create-for-rbac) command:
76
+
```bash
78
77
79
78
```azurecli
80
79
apiCenter=<api-center-name>
@@ -87,7 +86,7 @@ az ad sp create-for-rbac --name $spName --role Contributor --scopes $apicResourc
87
86
88
87
```
89
88
90
-
Copy the JSON output to a secure location. The output should look similar to the following:
89
+
Copy the JSON output, which should look similar to the following:
91
90
92
91
```json
93
92
{
@@ -107,29 +106,112 @@ Copy the JSON output to a secure location. The output should look similar to the
107
106
108
107
### Add the workflow file to your GitHub repository
109
108
110
-
# Please create following environment variables to deploy to azure resource: RESOURCE_GROUP, SERVICE_NAME, FILE_LOCATION
111
-
# RESOURCE_GROUP: Azure Resource Group name, please ensure the resource group already created.
112
-
# SERVICE_NAME: Azure API Center resource name, please ensure the API Center resource already created.
A GitHub Actions workflow is represented by a YAML (.yml) definition file. This definition contains the various steps and parameters that make up the workflow. [Learn more](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions)
115
110
116
-
```yml
111
+
The following is a basic workflow file for this example that you can use or modify.
112
+
113
+
In this example:
114
+
* the workflow is triggered when a pull request that adds a specified JSON definition is closed on the main branch
115
+
* The Azure credentials saved in your repo are used to sign into the Azure CLI
116
+
* The [az apic register](/cli/azure/apic/api#az-apic-api-register) command registers the API in the API center specified in the environment variables
117
+
118
+
To configure the workflow file:
119
+
120
+
1. Copy and save the file under a name such as `register-api.yml`.
121
+
1. Update the values forthe environment variables to match your API centerin Azure.
122
+
1. Update the expected path where you'll add the definition file in the repository.
123
+
1. Add this file in the `/.github/workflows/` path in your GitHub repository.
117
124
125
+
> [!TIP]
126
+
> If you use the [Visual Studio Code extension](use-vscode-extension.md) for Azure API Center, you can create a starting workflow file from the extension which you can then modify for your scenario. In the Command Palette, select **Azure API Center: Register APIs**. Select **CI/CD** > **GitHub**.
118
127
128
+
```yml
129
+
name: Register API Definition to Azure API Center
130
+
on:
131
+
pull_request:
132
+
types: [closed]
133
+
branches:
134
+
- main
135
+
136
+
permissions:
137
+
id-token: write
138
+
contents: read
139
+
140
+
env:
141
+
# Set this to your Azure API Center resource group name
142
+
RESOURCE_GROUP: <YOUR_RESOURCE_GROUP>
143
+
# Set this to your Azure API Center service name
144
+
SERVICE_NAME: <YOUR_API_CENTER>
145
+
# Set this to the location of your API definition file
146
+
# Example: ./APIs/catfacts-api/07-15-2024.json
147
+
API_FILE_LOCATION: <YOUR_PATH_TO_API_DEFINITION>
148
+
149
+
jobs:
150
+
register:
151
+
runs-on: ubuntu-latest
152
+
environment: production
153
+
steps:
154
+
- uses: actions/checkout@v2
155
+
- name: Azure login
156
+
uses: azure/login@v1
157
+
with:
158
+
creds: ${{ secrets.AZURE_CREDENTIALS }}
159
+
160
+
- name: Register to API Center
161
+
uses: azure/CLI@v2
162
+
with:
163
+
azcliversion: latest
164
+
inlineScript: |
165
+
az apic api register -g ${{ env.RESOURCE_GROUP }} -n ${{ env.SERVICE_NAME }} --api-location ${{ env.API_FILE_LOCATION }}
119
166
```
120
167
121
168
169
+
## Add API definition file to the repository
170
+
171
+
Test the workflow by adding an API definition file to the repository. Follow these high-level steps, which are typical of a development workflow. For details on working with GitHub branches, see the [GitHub documentation](https://docs.github.com/en/github/collaborating-with-pull-requests/getting-started/about-collaborative-development-models).
172
+
173
+
1. Create a new branch from the main branch in your repository.
174
+
1. Add the API definition file to the repository at the path specified in the workflow file (for example, `./APIs/catfacts-api/07-15-2024.json`).
175
+
1. Commit the changes and push them to the new branch.
176
+
1. Create a pull request to merge the new branch into the main branch.
177
+
1. After review, merge the pull request. The merge triggers the GitHub Actions workflow that registers the API in your API center.
178
+
179
+
:::image type="content" source="media/register-apis-github-actions/workflow-action.png" alt-text="Screenshot showing successful workflow run in GitHub.":::
122
180
123
-
## Add API specification to the repository
181
+
## Verify the API registration
124
182
183
+
Verify that the API is registered in your API center.
125
184
185
+
1. In the [Azure portal](https://portal.azure.com), navigate to your API center.
186
+
1. In the left menu, under **Assets**, select **APIs**.
187
+
1. The newly registered API should appear in the list of APIs.
126
188
127
189
190
+
:::image type="content" source="media/register-apis-github-actions/api-registered-api-center.png" alt-text="Screenshot of API registered in API Center after workflow.":::
191
+
192
+
## Extend the scenario
193
+
194
+
The following sections provide suggestions for extending the scenario to meet your specific requirements.
195
+
196
+
## Add a new version to existing API
197
+
198
+
To add a new API version to an API that's already registered in your API center:
199
+
200
+
1. Update the workflow file to specify the new API definition file at an existing path in the repository. Example: `./APIs/catfacts-api/07-16-2024.json`.
201
+
1. Change to your existing working branch in the repository.
202
+
1. Add the new API definition file to the repository at the specified path.
203
+
1. Follow typical steps to create a pull request to merge the changes into the main branch, and merge the pull request.
204
+
1. The GitHub Actions workflow triggers and registers the new API version in the existing API in your API center.
205
+
206
+
207
+
## Add metadata to the API registration
208
+
209
+
You can add metadata forthe API registration, defined using the [metadata schema](metadata.md)in your API center. For example
210
+
128
211
129
212
130
213
131
214
## Related content
132
215
133
216
* [Using secrets in a GitHub Actions](https://docs.github.com/en/actions/reference/encrypted-secrets)
134
-
135
-
<!-- TBD- >
217
+
* [Creating configuration variables for a repository](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions)
0 commit comments