{"id":41970,"date":"2026-11-25T22:08:23","date_gmt":"2026-11-25T22:08:23","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=41970"},"modified":"2026-11-25T22:08:23","modified_gmt":"2026-11-25T22:08:23","slug":"azure-devops-pipeline-yaml-example","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/azure-devops-pipeline-yaml-example\/","title":{"rendered":"Azure Devops Pipeline YAML Example"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This page shows a minimal Azure Pipelines YAML file that builds a Docker image with the <code>Docker@2<\/code> task on a Microsoft-hosted Ubuntu agent each time code is pushed to <code>main<\/code>. The sections below the example explain each line and show how to run the pipeline. They also cover how to push the image to a container registry, how to fix the most common errors, and which practices keep the pipeline secure.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Build a Docker image<\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/0578576b6d63a027277aa1d55e86016d.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What the example pipeline does, line by line<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>azure-pipelines.yml<\/code> above is the starter template Azure DevOps offers when you choose the <strong>Docker: Build an image<\/strong> option. It has one stage, one job and one step. It builds the image but does not publish it anywhere.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Line<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>trigger: - main<\/code><\/td><td>Starts a run on every push to the <code>main<\/code> branch. If your default branch is <code>master<\/code>, change this or the pipeline will never trigger.<\/td><\/tr><tr><td><code>resources: - repo: self<\/code><\/td><td>Uses the repository that holds the YAML file. Pipelines check out <code>self<\/code> anyway, so you can remove this block.<\/td><\/tr><tr><td><code>variables: tag: &#039;$(Build.BuildId)&#039;<\/code><\/td><td>Sets the image tag to the numeric run ID, which is unique for every run in the project.<\/td><\/tr><tr><td><code>stages<\/code> \/ <code>stage: Build<\/code><\/td><td>Groups jobs. A single stage is enough here, but stages let you add test or deploy stages later.<\/td><\/tr><tr><td><code>pool: vmImage: ubuntu-latest<\/code><\/td><td>Runs the job on a Microsoft-hosted Ubuntu agent that already has Docker installed.<\/td><\/tr><tr><td><code>task: Docker@2<\/code><\/td><td>Version 2 of the built-in Docker task.<\/td><\/tr><tr><td><code>command: build<\/code><\/td><td>Runs <code>docker build<\/code> only. Nothing is pushed.<\/td><\/tr><tr><td><code>dockerfile: &#039;**\/Dockerfile&#039;<\/code><\/td><td>A glob that finds a file named <code>Dockerfile<\/code> anywhere in the repository.<\/td><\/tr><tr><td><code>tags: | $(tag)<\/code><\/td><td>One tag per line. Here the only tag is the build ID.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> With <code>command: build<\/code> and no registry, the image only exists on the hosted agent. It is deleted when the job finishes. That is fine for checking that the Dockerfile builds, but you need the push version further down to deploy anything.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An Azure DevOps organization and project, with the code in Azure Repos or GitHub.<\/li>\n\n\n<li>A <code>Dockerfile<\/code> in the repository that builds locally. Run <code>docker build -t myapp:test .<\/code> before you commit.<\/li>\n\n\n<li>Parallel jobs available for Microsoft-hosted agents. New organizations may need to request the free grant or register a self-hosted agent.<\/li>\n\n\n<li>If you want to push the image: a container registry such as Azure Container Registry (ACR) or Docker Hub, and permission to create service connections in the project.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Create and run the pipeline<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Commit the YAML above as <code>azure-pipelines.yml<\/code> in the root of the repository.<\/li>\n\n\n<li>In Azure DevOps, go to <strong>Pipelines &gt; New pipeline<\/strong> and select where the code is hosted.<\/li>\n\n\n<li>Select the repository, then choose <strong>Existing Azure Pipelines YAML file<\/strong> and point it at <code>\/azure-pipelines.yml<\/code>.<\/li>\n\n\n<li>Click <strong>Run<\/strong>. Later pushes to <code>main<\/code> start new runs automatically.<\/li>\n\n\n<li>Open the run and the <strong>Build an image<\/strong> step. The log shows the <code>docker build<\/code> command and each Dockerfile layer, and ends with the step succeeding.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Build and push the image to a container registry<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To produce an image you can deploy, create a Docker Registry service connection under <strong>Project settings &gt; Service connections &gt; New service connection &gt; Docker Registry<\/strong>. For ACR, choose <strong>Azure Container Registry<\/strong> and select your subscription and registry. Note the connection name, then use it in the pipeline below.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">trigger:\n  branches:\n    <span class=\"hljs-keyword\">include<\/span>:\n    - main\n\npr:\n  branches:\n    <span class=\"hljs-keyword\">include<\/span>:\n    - main\n\nvariables:\n  dockerRegistryServiceConnection: <span class=\"hljs-string\">'acr-connection'<\/span>   <span class=\"hljs-comment\"># service connection name<\/span>\n  imageRepository: <span class=\"hljs-string\">'myapp'<\/span>                            <span class=\"hljs-comment\"># repository inside the registry<\/span>\n  dockerfilePath: <span class=\"hljs-string\">'$(Build.SourcesDirectory)\/Dockerfile'<\/span>\n  tag: <span class=\"hljs-string\">'$(Build.BuildId)'<\/span>\n\nstages:\n- stage: Build\n  displayName: Build <span class=\"hljs-keyword\">and<\/span> push image\n  jobs:\n  - job: Build\n    pool:\n      vmImage: ubuntu-latest\n    steps:\n    - task: Docker@<span class=\"hljs-number\">2<\/span>\n      displayName: Build image\n      inputs:\n        command: build\n        containerRegistry: $(dockerRegistryServiceConnection)\n        repository: $(imageRepository)\n        dockerfile: $(dockerfilePath)\n        buildContext: <span class=\"hljs-string\">'$(Build.SourcesDirectory)'<\/span>\n        tags: |\n          $(tag)\n\n    - task: Docker@<span class=\"hljs-number\">2<\/span>\n      displayName: Push image\n      condition: <span class=\"hljs-keyword\">and<\/span>(succeeded(), ne(variables&#91;<span class=\"hljs-string\">'Build.Reason'<\/span>], <span class=\"hljs-string\">'PullRequest'<\/span>))\n      inputs:\n        command: push\n        containerRegistry: $(dockerRegistryServiceConnection)\n        repository: $(imageRepository)\n        tags: |\n          $(tag)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><code>containerRegistry<\/code> and <code>repository<\/code> together give the full image name, for example <code>myregistry.azurecr.io\/myapp:1234<\/code>. Set both on the build step so the push step finds the same image.<\/li>\n\n\n<li><code>pr<\/code> runs the build for pull requests into <code>main<\/code>, so a broken Dockerfile is caught before merge. In Azure Repos Git, PR builds come from a branch policy rather than the <code>pr<\/code> keyword.<\/li>\n\n\n<li>The <code>condition<\/code> skips the push for pull request builds, so unreviewed code never reaches the registry.<\/li>\n\n\n<li><code>buildContext<\/code> sets the directory sent to Docker. The default <code>**<\/code> means the folder that contains the Dockerfile.<\/li>\n\n\n<li>You can merge both steps into one with <code>command: buildAndPush<\/code> if you do not need PR builds. Note that <code>buildAndPush<\/code> ignores the <code>arguments<\/code> input. Use separate steps if you need options such as <code>--build-arg<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">After a run on <code>main<\/code>, confirm the tag reached ACR:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">az acr repository show-tags --name myregistry --repository myapp --output table<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Symptom<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>Pipeline never starts after a push<\/td><td>The branch in <code>trigger<\/code> does not match, for example <code>main<\/code> vs <code>master<\/code><\/td><td>Change <code>trigger<\/code> to your real default branch<\/td><\/tr><tr><td>No Dockerfile matching <code>**\/Dockerfile<\/code> was found<\/td><td>The file has a different name or case, such as <code>dockerfile<\/code> or <code>Dockerfile.prod<\/code><\/td><td>Set <code>dockerfile<\/code> to the exact path<\/td><\/tr><tr><td>The wrong Dockerfile is built<\/td><td>The glob matches several Dockerfiles in a monorepo<\/td><td>Use an explicit path such as <code>src\/api\/Dockerfile<\/code><\/td><\/tr><tr><td><code>COPY failed<\/code> \/ file not found during build<\/td><td>The build context is the Dockerfile&#039;s folder, not the repository root<\/td><td>Set <code>buildContext<\/code> to the directory your <code>COPY<\/code> paths are relative to<\/td><\/tr><tr><td>No hosted parallelism has been purchased or granted<\/td><td>The organization has no Microsoft-hosted parallel jobs<\/td><td>Request the free parallelism grant or use a self-hosted agent pool<\/td><\/tr><tr><td>Run waits with a permission request<\/td><td>First use of a service connection by this pipeline<\/td><td>Click <strong>Permit<\/strong> on the run, or grant pipeline access on the service connection<\/td><\/tr><tr><td><code>unauthorized<\/code> or <code>denied<\/code> on push<\/td><td>Service connection credentials expired or lack push rights<\/td><td>Recreate or update the service connection and check its role on the registry<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices and security notes<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Never put registry passwords in YAML.<\/strong> Use a service connection, or a secret variable for non-Docker credentials.<\/li>\n\n\n<li><strong>Use immutable tags for deployments.<\/strong> <code>$(Build.BuildId)<\/code> or the commit SHA <code>$(Build.SourceVersion)<\/code> lets you trace a running container back to a build. Treat <code>latest<\/code> as a convenience only.<\/li>\n\n\n<li><strong>Pin the agent image if builds must be reproducible.<\/strong> <code>ubuntu-latest<\/code> moves to newer Ubuntu releases over time. A specific image such as <code>ubuntu-22.04<\/code> changes only when you choose.<\/li>\n\n\n<li><strong>Add a <code>.dockerignore<\/code> file<\/strong> so <code>.git<\/code>, local build output and secrets are not sent in the build context.<\/li>\n\n\n<li><strong>Restrict service connections.<\/strong> Do not grant access to all pipelines, and add approvals on environments that deploy to production.<\/li>\n\n\n<li><strong>Scan images<\/strong> for vulnerabilities before they reach production, either with your registry&#039;s scanning feature or with a scanner step in the pipeline.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Correction to the original example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The YAML above is valid and runs as written. The documentation link in its header comment uses the old <code>docs.microsoft.com<\/code> domain, which now redirects to <code>learn.microsoft.com<\/code>. The <code>resources: - repo: self<\/code> block is a leftover from older templates and is not needed. Most importantly, readers should know that <code>command: build<\/code> never publishes the image. Use the build-and-push version in this article when the image needs to be deployed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between build and buildAndPush in the Docker@2 task?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>build<\/code> runs only <code>docker build<\/code>, so the image stays on the agent. <code>buildAndPush<\/code> builds the image and pushes every listed tag to the registry in the service connection. Unlike <code>build<\/code>, it ignores the <code>arguments<\/code> input.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Where does the image go if I only use command: build?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It stays in the local Docker cache of the hosted agent, and that cache is deleted when the job ends. Add a <code>push<\/code> or <code>buildAndPush<\/code> step with a <code>containerRegistry<\/code> service connection to keep the image.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I tag the image with the Git commit SHA?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add <code>$(Build.SourceVersion)<\/code> as a line under <code>tags<\/code>. You can list it next to <code>$(Build.BuildId)<\/code> so each image has both tags.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use docker commands directly instead of the Docker@2 task?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Microsoft-hosted Ubuntu agents have Docker installed, so a <code>script<\/code> step running <code>docker build<\/code> works. You then have to log in to the registry yourself. The Docker@2 task handles login through the service connection and adds pipeline metadata labels.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does my Azure Pipelines YAML not trigger on push?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Usually the branch named under <code>trigger<\/code> does not match the branch you pushed to. Also check that the pipeline points at the right YAML file, and that triggers are not overridden in the pipeline&#039;s UI settings.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/cloud-computing\/azure\/\">More Azure tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/devops\/ci-cd\/\">More CI\/CD tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/ci-cd-engineer-role-blueprint-responsibilities-skills-kpis-and-career-path\/\">CI\/CD Engineer: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/how-to-prepare-self-paced-for-microsoft-azure-certifications\/\">How to Prepare Self-Paced for Microsoft Azure Certifications?<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This page shows a minimal Azure Pipelines YAML file that builds a Docker image with the Docker@2 task on a Microsoft-hosted Ubuntu agent each time code is&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[26139,7853],"tags":[26197,1314,5458],"class_list":["post-41970","post","type-post","status-publish","format-standard","hentry","category-ci-cd","category-azure","tag-azure-pipelines","tag-docker-images","tag-yaml"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/41970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=41970"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/41970\/revisions"}],"predecessor-version":[{"id":80244,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/41970\/revisions\/80244"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=41970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=41970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=41970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}