{"id":3499,"date":"2024-12-04T00:43:31","date_gmt":"2024-12-04T06:43:31","guid":{"rendered":"https:\/\/microsoftgeek.com\/?p=3499"},"modified":"2024-12-04T00:43:31","modified_gmt":"2024-12-04T06:43:31","slug":"self-hosted-agent-on-linux-created-in-3-minutes","status":"publish","type":"post","link":"https:\/\/microsoftgeek.com\/?p=3499","title":{"rendered":"Self-hosted agent on Linux created in 3 minutes"},"content":{"rendered":"\n<p>Self-hosted agent on Linux In some cases is&nbsp;<strong>necessary<\/strong>, for example, you need to integrate with VNET \u2013 because the resources you want to access are not publicly accessible. This can be a storage account, container registry, or many others. Using my instruction you build and run a self-hosted agent in 3 minutes.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In that scenario you must:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create VNET in the same region as VNET you want to access<\/li>\n\n\n\n<li>Create a subnet in the newly created VNET,\u00a0<strong>and remember to delegate the subnet to Microsoft.ContainerInstance\/containerGroups<\/strong><\/li>\n\n\n\n<li>Create Azure Container Registry (ACR)<\/li>\n\n\n\n<li>Build agent image and push it to Azure Container Registry<\/li>\n\n\n\n<li>Deploy your agent into created subnet using Azure Container Instances<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"build-agent-image\">Build and push agent image<\/h4>\n\n\n\n<p>First, you must install Azure CLI and logging using PowerShell or bash, check the instructions here&nbsp;<a href=\"https:\/\/learn.microsoft.com\/en-us\/cli\/azure\/install-azure-cli\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/learn.microsoft.com\/en-us\/cli\/azure\/install-azure-cli&nbsp;<\/a>and&nbsp;<a href=\"https:\/\/learn.microsoft.com\/en-us\/cli\/azure\/authenticate-azure-cli\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/learn.microsoft.com\/en-us\/cli\/azure\/authenticate-azure-cli<\/a><\/p>\n\n\n\n<p>As you have logged you must select a proper subscription:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>az account list\naz account set --subscription ##SUBSCRIPTION##<\/code><\/pre>\n\n\n\n<p>In ##SUBSCRIPTION## put proper subscription guid, you can it in the \u201cid\u201d property.<\/p>\n\n\n\n<p>To build your image, navigate to the console to the dockerfile directory and run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>az acr build --registry ##REGISTRY_NAME## --image adobuildagent:v1 .<\/code><\/pre>\n\n\n\n<p><strong>Where<\/strong>:<\/p>\n\n\n\n<p><strong>##REGISTRY_NAME##&nbsp;<\/strong>is the name of your registry, without a domain. So if in portal URL for ACR is azurewayacr.azurecr.io, then the name will be just azurewayacr.<\/p>\n\n\n\n<p>This command builds your image using Azure Container Registry, you don\u2019t need docker installed on your machine. The image is also uploaded to ACR with tag&nbsp;<strong>v1<\/strong>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"run-agent\">Run agent in Azure Container Instances<\/h4>\n\n\n\n<p>Now its time to run Azure Container Instance:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>az container create -g ##RESROUCE_GROUP##--registry-login-server ##ACR_URL## --registry-username ##ACR_USER## --registry-password ##ACR_USER_PASS## --name ##ACI_CONTAINER_NAME## --image ##IMAGE_ADDRESS## --vnet ##VNET_NAME## --subnet ##SUBNET_NAME## --environment-variables AZP_TOKEN=##DEVOPS_PERSONAL_TOKEN## AZP_URL=##DEVOPS_ORGANIZATION_URL##<\/code><\/pre>\n\n\n\n<p><strong>Where<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>RESROUCE_GROUP \u2013 name of the resource group<\/li>\n\n\n\n<li>ACR_URL \u2013 full URL of ACR instance<\/li>\n\n\n\n<li>ACR_USER \u2013 admin name of the ACR, can be turn on \u201cAccess keys\u201d -> \u201cAdmin user\u201d<\/li>\n\n\n\n<li>ACR_USER_PASS \u2013 password for admin user<\/li>\n\n\n\n<li>ACI_CONTAINER_NAME \u2013 name of the created container, I prefer to put there e.g: ado-linux-agent-1<\/li>\n\n\n\n<li>IMAGE_ADDRESS \u2013 image address contains full ACR URL and image name with tag, e.g: azurewayacr.azurecr.io\/adobuildagent:v1<\/li>\n\n\n\n<li>VNET_NAME \u2013 name of the VNET<\/li>\n\n\n\n<li>SUBNET_NAME \u2013 subnet name, remember to choose one with proper delegation<\/li>\n\n\n\n<li>DEVOPS_PERSONAL_TOKEN \u2013 personal token for ADO, check how to generate\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/devops\/organizations\/accounts\/use-personal-access-tokens-to-authenticate?view=azure-devops&amp;tabs=Windows\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a><\/li>\n\n\n\n<li>DEVOPS_ORGANIZATION_URL \u2013 organizing URL, e.g:\u00a0<a href=\"https:\/\/dev.azure.com\/rhinoSMEv2\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/dev.azure.com\/<\/a>myOrganization<\/li>\n<\/ul>\n\n\n\n<p>After running the above command, an agent will be created and connected with the Default pool in your Azure DevOps. Remember to change the pool in your pipeline, and place it at the top of the yml file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pool: Default<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Reducing costs<\/h4>\n\n\n\n<p>If you don\u2019t need your agnet for 24\/7 then you can stop when you finish your work and run when you need it, use below commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>az container stop --name ##ACI_CONTAINER_NAME## --resource-group ##RESROUCE_GROUP##\n\naz container start --name ##ACI_CONTAINER_NAME## --resource-group ##RESROUCE_GROUP##<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Self-hosted agent on Linux In some cases is&nbsp;necessary, for example, you need to integrate with VNET \u2013 because the resources you want to access are not publicly accessible. This can be a storage account, container registry, or many others. Using my instruction you build and run a self-hosted agent in 3 minutes. In that scenario [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69,35,78,77],"tags":[],"class_list":["post-3499","post","type-post","status-publish","format-standard","hentry","category-azure","category-cloud-computing","category-containers","category-devops"],"_links":{"self":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3499"}],"version-history":[{"count":1,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3499\/revisions"}],"predecessor-version":[{"id":3500,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3499\/revisions\/3500"}],"wp:attachment":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}