Skip to content

Commit ac61c39

Browse files
author
gitName
committed
update in progress
1 parent 2755f90 commit ac61c39

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

articles/api-center/register-apis-github-actions.md

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Register APIs using GitHub Actions - Azure API Center
33
description: Learn how to automate the registration of APIs in your API center using a CI/CD workflow based on GitHub Actions.
44
ms.service: azure-api-center
55
ms.topic: how-to
6-
ms.date: 07/24/2024
6+
ms.date: 12/23/2024
77
ms.author: danlep
88
author: dlepow
99
ms.custom: devx-track-azurecli
@@ -20,9 +20,9 @@ The following diagram shows how API registration in your API center can be autom
2020

2121
:::image type="content" source="media/register-apis-github-actions/scenario-overview.svg" alt-text="Diagram showing steps to trigger a GitHub actions workflow to register an API in an Azure API center." lightbox="media/register-apis-github-actions/scenario-overview.svg":::
2222

23-
1. Set up a GitHub Actions workflow in your repository that triggers when a pull request that adds an API definition file is merged.
23+
1. Set up a GitHub Actions workflow in your repository that triggers when a pull request adding an API definition file is merged.
2424
1. Create a branch from the main branch in your GitHub repository.
25-
1. Add an API definition file, commit the changes, and push them to the new branch.
25+
1. Add an API definition file, commit the changes, and push to the new branch.
2626
1. Create a pull request to merge the new branch into the main branch.
2727
1. Merge the pull request.
2828
1. The merge triggers a GitHub Actions workflow that registers the API in your API center.
@@ -133,42 +133,38 @@ In this example:
133133
To configure the workflow file:
134134

135135
1. Copy and save the file under a name such as `register-api.yml`.
136-
1. Update the values for the environment variables to match your API center in Azure.
137136
1. Confirm or update the name of the repository folder (`APIs`) where you'll add the API definition file.
138137
1. Add this workflow file in the `/.github/workflows/` path in your GitHub repository.
138+
1. Set [variables](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables) in your repo for your API center name and resource group name in Azure.
139139

140140
> [!TIP]
141141
> Using the [Visual Studio Code extension](build-register-apis-vscode-extension.md) for Azure API Center, you can generate a starting workflow file by running an extension command. In the Command Palette, select **Azure API Center: Register APIs**. Select **CI/CD** > **GitHub**. You can then modify the file for your scenario.
142142

143143
```yml
144144
name: Register API Definition to Azure API Center
145-
on:
145+
'on':
146146
pull_request:
147-
types: [closed]
147+
types:
148+
- closed
148149
branches:
149150
- main
150151
paths:
151-
- "APIs/**/*.json"
152+
- APIs/**/*.json
152153
permissions:
153154
contents: read
154155
pull-requests: read
155156
env:
156-
# set this to your Azure API Center resource group name
157-
RESOURCE_GROUP: <YOUR_RESOURCE_GROUP>
158-
# set this to your Azure API Center service name
159-
SERVICE_NAME: <YOUR_API_CENTER>
157+
TOKEN: '${{ secrets.GITHUB_TOKEN }}'
160158
jobs:
161159
register:
162160
runs-on: ubuntu-latest
163161
environment: production
164162
steps:
165163
- uses: actions/checkout@v2
166-
167164
- name: Get specification file path in the PR
168165
id: get-file-location
169166
uses: actions/github-script@v5
170167
with:
171-
github-token: ${{ secrets.GITHUB_TOKEN }}
172168
script: |
173169
const pull_number = context.payload.pull_request.number;
174170
const owner = context.repo.owner;
@@ -180,23 +176,37 @@ jobs:
180176
});
181177
if (files.data.length === 1) {
182178
const filename = files.data[0].filename;
183-
core.exportVariable('API_FILE_LOCATION', hfilename);
184-
console.log(`API_FILE_LOCATION: ${{ env.API_FILE_LOCATION }}`);
185-
}
186-
else {
179+
const variableName = 'API_FILE_LOCATION';
180+
const variableValue = filename;
181+
182+
# Set the repository-level variable using the GitHub API
183+
# const octokit = github.getOctokit(core.getInput('GITHUB_TOKEN'));
184+
import { Octokit } from "octokit";
185+
const octokit = new Octokit({
186+
auth: process.env.TOKEN,
187+
});
188+
await octokit.request('PUT /repos/{owner}/{repo}/actions/variables/{name}', {
189+
owner: owner,
190+
repo: repo,
191+
name: variableName,
192+
value: variableValue
193+
});
194+
195+
console.log(`Set repository variable ${variableName} to ${variableValue}`);
196+
} else {
187197
console.log('The PR does not add exactly one specification file.');
188198
}
189199
- name: Azure login
190200
uses: azure/login@v1
191201
with:
192-
creds: ${{ secrets.AZURE_CREDENTIALS }}
193-
202+
creds: '${{ secrets.AZURE_CREDENTIALS }}'
194203
- name: Register to API Center
195204
uses: azure/CLI@v2
196205
with:
197206
azcliversion: latest
198-
inlineScript: |
199-
az apic api register -g ${{ env.RESOURCE_GROUP }} -n ${{ env.SERVICE_NAME }} --api-location ${{ env.API_FILE_LOCATION }}
207+
inlineScript: >
208+
az apic api register -g ${{ vars.RESOURCE_GROUP }} -n ${{
209+
vars.SERVICE_NAME }} --api-location ${{ vars.API_FILE_LOCATION }}
200210
```
201211
202212

0 commit comments

Comments
 (0)