This repo showcases the possibility to leverage self-hosted build agents in Azure for both Azure Pipelines
and GitHub Actions
without the need to specify Service Connections, VariableGroups, Action Secrets.
Still secrets will be securely managed in Azure KeyVault and are accessible for the self-hosted agent by leveraging MSI (Managed Service Identity).
First, please fork this repo. You need to do that in order to link the GitHub Action Runner and to link the repo in Azure DevOps.
If you are using VSCode, installed the Remote Container extension and have a Docker runtime installed you are ready to go with the provided devcontainer.
Otherwise you will need to install
You will need access to an Azure Subscription or Resource Group with Owner rights. If you don't have that yet you can create a free trial account here.
-
Clone your fork.
-
Run the following script. Where the settings can be found will be explained in the next sections:
cd iac az deployment sub create -l <azureregion> \ -f ./main.bicep --parameters username=<username> \ sshPublicKey="$(cat ~/.ssh/id_rsa.pub)" \ salt="<somesalt>" \ AzDOPATtoken='<AzurePATtoken>' \ AzDOAccountUrl='<AzDOAccountUrl>' \ AzDOAgentPool='<AzDOAgentPool>' \ GitHubRepoURL='<GitHubRepoURL>' \ GitHubToken='<GitHubToken>' \ --name setup
-
The script JSON output contains a section
outputs
please record the values for acr_name and kv_name, you will need them later. Will look like this:"outputs": { "acr_name": { "type": "String", "value": "acr6po7kgbndb7ye" }, "kv_name": { "type": "String", "value": "kv6po7kgbndb7ye" } },
Please create a new project within your Azure DevOps Organization. You can follow this guide if you don't have one.
If you only want to test it for GitHub Actions please leave the AzDO* parameters defined but blank. If you only want to test it for Azure Pipelines leave the GitHub* parameters defined but blank.
azureregion: Select any region you want. You can get a list of all regions available to you with this command az account list-locations -o table
use the Name
value.
username: Select any user name you like.
sshPublicKey: If you don't have a ssh credential yet please create one using this guide. The user .ssh folder will be mounted within the devcontainer.
salt: Enter anything you want. This string will be used to create a unique string for Azure service names which need to be globally unique (esp. KeyVault and Container Registry).
AzDOPATtoken: Go through these steps to generate your PAT Token, it will only need the Agent Pools (read, manage)
scope.
AzDOAccountUrl: This will be your AzDO Server URL like described here.
AzDOAgentPool: You can use the Default
pool named Default
or create a new one under Agent Pools. See here.
GitHubRepoURL: The URL of your fork of this repo e.g. https://github.com/<yourid>/azure-managed-identity-pipelines
.
GitHubToken: The token to add a Runner to your repo. Go to this menu mentioned here and use the token displayed.
If you get following error message while spinning up the environment above, please just re-execute. Sometimes the propagation of objects within AAD takes some time.
Principal
xyz
does not exist in the directoryabc
.
GitHub Actions require to be within a specific folder of the repo. There is no additional setup needed to use them.
- Navigate to your forked GitHub repo.
- Click on Actions.
- Under all workflows click on 'Build the agent container image'.
- Select run workflow.
- Enter the name of your Azure Container Registry.
- Click on
Run the workflow
.
Wait for the workflow to be completed. Now you can execute the second workflow which showcases the retrieval of a configuration item from KeyVault.
- Under all workflows click on 'Retrieve Configuration without providing a secret'.
- Select run workflow.
- Enter the name of your Azure Container Registry.
- Enter the name of your KeyVault.
- Click on
Run the workflow
.
You should see the value somevalue
displayed in the Use Config
step of the workflow under
both Jobs Read configuration from KeyVault
and Reuses the config value from previous job
.
The configuration key and value was created within the IaC script.
To set up Azure Pipelines we have to do some prep work.
-
Please create a new project within your Azure DevOps Organization. You can follow this guide.
-
Create a GitHub PAT with scope
admin:repo_hook
. You can follow this guide. -
Execute the following script with your values for the env variables:
export AZURE_DEVOPS_EXT_GITHUB_PAT='' export YOUR_GIT_HUB_ID='' export YOUR_AZURE_DEV_OPS_ORG='' export YOUR_AZURE_DEV_OPS_PROJECT_NAME='' service_connection_id=$(az devops service-endpoint github create \ --github-url https://github.com/$YOUR_GIT_HUB_ID/azure-managed-identity-pipelines/ \ --name github \ --organization https://dev.azure.com/$YOUR_AZURE_DEV_OPS_ORG \ --project $YOUR_AZURE_DEV_OPS_PROJECT_NAME \ -o tsv --query 'id') az pipelines create \ --name 'Create Agent Image' \ --description 'Builds and Pushes the agent image' \ --repository https://github.com/$YOUR_GIT_HUB_ID/azure-managed-identity-pipelines/ \ --branch main \ --yml-path pipelines/create-agent-image.yml \ --organization https://dev.azure.com/$YOUR_AZURE_DEV_OPS_ORG \ --project $YOUR_AZURE_DEV_OPS_PROJECT_NAME \ --service-connection $service_connection_id az pipelines create \ --name 'Retrieve Configuration without providing a secret' \ --description 'Retrieve Configuration without providing a secret' \ --repository https://github.com/$YOUR_GIT_HUB_ID/azure-managed-identity-pipelines/ \ --branch main \ --yml-path pipelines/retrieve-settings-from-key-vault.yml \ --organization https://dev.azure.com/$YOUR_AZURE_DEV_OPS_ORG \ --project azure-managed-identity-pipelines \ --service-connection $service_connection_id
- Navigate to your Azure DevOps Project.
- Click on
Pipelines
in the menu. - Click on
All
. - Click on
Create Agent Image
. - Click on
Run Pipeline
. - Leave
Commit
parameter empty - Enter the Agent Pool Name you created for the IaC script (eventually you did use
Default
) - Enter the name of your Azure Container Registry.
- Click on
Run
.
Wait for the Pipeline Run to finish. Now you can execute the second pipeline which showcases the retrieval of a configuration item from KeyVault.
- Click on
Pipelines
in the menu. - Click on
All
. - Click on
Retrieve Configuration without providing a secret
. - Click on
Run Pipeline
. - Leave
Commit
parameter empty - Enter the Agent Pool Name you created for the IaC script (eventually you did use
Default
) - Enter the name of your Azure Container Registry.
- Enter the name of your KeyVault.
- Click on
Run
.
You should see the value somevalue
displayed in the Use Config
step of the workflow under both Stages/Jobs Read configuration from KeyVault
and Reuse config from previous stage
.
The configuration key and value was created within the IaC script.
To clean up your resources just delete the resource group:
az group delete --resource-group self-hosted-agent-rg