Azure Pipelines
Using Azure Pipelines, you can easily create/update and query entities in Port.
๐ก Common Azure Pipelines usageโ
Port's API allows for easy integration between Port and your Azure Pipeline jobs, for example:
- Report the status of a running CI job;
- Update the software catalog about a new build version for a microservice;
- Get existing entities.
Setupโ
To interact with Port using Azure Pipelines, you will first need to define your Port credentials as variables for your pipeline.
Then, pass the defined variables to your pipeline script, for example, Python
:
- task: PythonScript@0
env:
PORT_CLIENT_ID: $(PORT_CLIENT_ID) # The variable name for your Port clientId
PORT_CLIENT_SECRET: $(PORT_CLIENT_SECRET) # The variable name for your Port clientSecret
inputs:
scriptSource: "filePath"
scriptPath: "main.py"
Make sure you have an existing Blueprint in your Port installation to create/update entities.
Working with Port's APIโ
Here is an example snippet showing how to integrate a job that uses Port's API with your existing Azure pipelines using Python:
Add the following task to your Azure pipeline:
Azure pipeline YAML
- script: |
pip install -r port_requirements.txt
- task: PythonScript@0
env:
PORT_CLIENT_ID: $(PORT_CLIENT_ID)
PORT_CLIENT_SECRET: $(PORT_CLIENT_SECRET)
inputs:
scriptSource: "filePath"
scriptPath: "port.py"
In the following example, we use Python modules which need to be installed. You can use the following requirements.txt
:
port_requirements.txt
requests>=2.28.2
- Create/Update
- Get
Create the following Python script in your repository to create or update Port entities as part of your pipeline:
import os
import requests
import json
# These are the credentials passed by the variables of your pipeline to your tasks and in to your env
CLIENT_ID = os.environ['PORT_CLIENT_ID']
CLIENT_SECRET = os.environ['PORT_CLIENT_SECRET']
credentials = {
'clientId': CLIENT_ID,
'clientSecret': CLIENT_SECRET
}
token_response = requests.post(f"{API_URL}/auth/access_token", json=credentials)
access_token = token_response.json()['accessToken']
headers = {
'Authorization': f'Bearer {access_token}'
}
entity_json = {
"identifier": "example-entity",
"properties": {
"myStringProp": "My value",
"myNumberProp": 1,
"myBooleanProp": true,
"myArrayProp": ["myVal1", "myVal2"],
"myObjectProp": {"myKey": "myVal", "myExtraKey": "myExtraVal"}
}
}
# request url : {API_URL}/blueprints/<blueprint_id>/entities
create_response = requests.post(f'{API_URL}/blueprints/test-blueprint/entities?upsert=true', json=entity_json, headers=headers)
print(json.dumps(get_response.json(), indent=4))
Create the following Python script in your repository to get Port entities as part of your pipeline:
import os
import requests
import json
# These are the credentials passed by the variables of your pipeline to your tasks and in to your env
CLIENT_ID = os.environ['PORT_CLIENT_ID']
CLIENT_SECRET = os.environ['PORT_CLIENT_SECRET']
credentials = {
'clientId': CLIENT_ID,
'clientSecret': CLIENT_SECRET
}
token_response = requests.post(f"{API_URL}/auth/access_token", json=credentials)
access_token = token_response.json()['accessToken']
headers = {
'Authorization': f'Bearer {access_token}'
}
# request url : {API_URL}/blueprints/<blueprint_id>/entities/<entity_id>
get_response = requests.get(f"{API_URL}/blueprints/test-blueprint/entities/test-entity", headers=headers)
print(json.dumps(get_response.json(), indent=4))
The baseUrl
, port_region
, port.baseUrl
, portBaseUrl
, port_base_url
and OCEAN__PORT__BASE_URL
parameters are used to select which instance or Port API will be used.
Port exposes two API instances, one for the EU region of Port, and one for the US region of Port.
- If you use the EU region of Port (https://app.getport.io), your API URL is
https://api.getport.io
. - If you use the US region of Port (https://app.us.getport.io), your API URL is
https://api.us.getport.io
.
Examplesโ
Refer to the examples page for practical examples of working with Port using Azure Pipelines.