Get Public IP address of Azure DevOps hosted agent and Github-hosted runner

Wanting to know the specific Public IP address of an Azure DevOps hosted agent or GitHub-hosted runner? In this blog post I am going to show examples of how to retrieve each using bash within Azure DevOps Pipelines and GitHub Workflows. This sort of requirement is useful if the agent or runner requires access that currently is IP restricted, such as an Azure Storage Account with network restrictions in place

What is used within bash to retrieve the Public IP address?

A simple curl of http://ipinfo.io/json – has the required details needed! Accessing the URL within your browser, will display similar to the below json (have XX some properties)

{
  "ip": "1.2.3.4",
  "hostname": "XX.btcentralplus.com",
  "city": "Craigavon",
  "region": "Northern Ireland",
  "country": "GB",
  "loc": "XXXX",
  "org": "AS2856 British Telecommunications PLC",
  "postal": "BTXX",
  "timezone": "Europe/London",
  "readme": "https://ipinfo.io/missingauth"
}

How is the ip extracted? Using jq – jq is pre-installed on both Azure DevOps hosted agent and GitHub-hosted runner

jq is like sed for JSON data – you can use it to slice and filter and map and transform structured data with the same ease that sedawkgrep and friends let you play with text.

https://stedolan.github.io/jq/

Awesome, lets show some example pipelines to show this constructed! In both examples, have included some regex to also remove the starting and leading “

Azure DevOps Pipeline

Example pipeline task

name: $(BuildDefinitionName)_$(date:yyyyMMdd)$(rev:.r)

trigger: none

pr: none

stages :        
  - stage: check_public_ip
    jobs:
      - job: check_public_ip
        steps:
              - task: Bash@3
                inputs:
                  script: |
                    IP=($(curl -s http://ipinfo.io/json | jq '.ip' | sed -e 's/^"//' -e 's/"$//'))
                    echo "This Microsoft hosted agent public IP is: $IP"

Reviewing the pipeline task, The Microsoft hosted agent public IP is shown

GitHub Workflow

Example workflow job and step

name: 'AzurePublicIP'

on:
  push:
    branches:
    - master
  pull_request:

jobs:

  AzureBicepDeploy:
    name: 'AzurePublicIP'
    runs-on: ubuntu-latest

    steps:

    - name: Azure Public IP
      run: |
        IP=($(curl -s http://ipinfo.io/json | jq '.ip' | sed -e 's/^"//' -e 's/"$//'))
        echo "This GitHub Workflow agent public IP is: $IP"

Reviewing the GitHub Workflow, the GitHub-hosted runner public IP is shown

Both examples complete!

As mentioned initially, this sort of requirement is useful if the agent or runner requires access that currently is IP restricted, such as an Azure Storage Account with network restrictions in place – you can extract the Public IP to be used as a variable within the pipeline to temporary update a specific firewall etc

Hope you enjoyed the blog post & thank you for reading!

GitHub Repository for example pipelines

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s