You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This pipeline automates the Docker image build and deployment process for the main branch. It ensures a secure and efficient workflow with best practices like caching, tagging, and conditional cleanup. Here's what it does:
105
+
116
106
- Triggers on commits and pull requests targeting the `main` branch.
117
-
- Authenticates with Docker Hub (or another specified registry) using a secure Azure DevOps service connection for credential management.
118
-
- Builds and tags the Docker image with the Azure build ID and a latest tag, utilizing Docker BuildKit for efficient caching.
119
-
- Validates the built Docker image by running a simple command (e.g., version check) to ensure it functions as expected.
120
-
- Pushes the tagged Docker images to the specified Docker registry (e.g., Docker Hub).
107
+
- Authenticates securely with Docker Hub using an Azure DevOps service connection.
108
+
- Builds and tags the Docker image using Docker BuildKit for caching.
109
+
- Pushes both buildId and latest tags to Docker Hub.
110
+
- Logs out from Docker if running on a self-hosted Linux agent.
121
111
122
-
---
123
112
124
-
## Summary
113
+
## Detailed Step-by-Step Explanation
114
+
115
+
### Step 1: Define Pipeline Triggers
116
+
117
+
```yaml
118
+
trigger:
119
+
- main
120
+
121
+
pr:
122
+
- main
123
+
```
124
+
125
+
This pipeline is triggered automatically on:
126
+
- Commits pushed to the `main` branch
127
+
- Pull requests targeting `main` main branch
128
+
129
+
> [!NOTE]
130
+
> Learn more: [Define pipeline triggers in Azure Pipelines](https://learn.microsoft.com/en-us/azure/devops/pipelines/build/triggers?view=azure-devops)
131
+
132
+
133
+
### Step 2: Define Common Variables
134
+
135
+
```yaml
136
+
variables:
137
+
imageName: 'docker.io/$(dockerUsername)/my-image'
138
+
dockerUsername: 'your-dockerhub-username' # Replace with your actual Docker Hub username
139
+
buildTag: '$(Build.BuildId)'
140
+
latestTag: 'latest'
141
+
```
142
+
143
+
These variables ensure consistent naming, versioning, and reuse throughout the pipeline steps:
144
+
145
+
- `imageName`: your image path on Docker Hub
146
+
- `buildTag`: a unique tag for each pipeline run
147
+
- `latestTag`: a stable alias for your most recent image
148
+
149
+
> [!NOTE]
150
+
> Learn more: [Define and use variables in Azure Pipelines](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch)
> Learn more: [Stage conditions in Azure Pipelines](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/stages?view=azure-devops&tabs=yaml)
169
+
170
+
### Step 4: Job Configuration
125
171
126
-
With a streamlined configuration, this Azure Pipelines CI workflow:
172
+
```yaml
173
+
jobs:
174
+
- job: DockerJob
175
+
displayName: Build and Push
176
+
pool:
177
+
vmImage: ubuntu-latest
178
+
```
179
+
180
+
This job uses the latest Ubuntu VM image provided by Microsoft-hosted agents. It can be swapped with a custom pool for self-hosted agents if needed.
181
+
182
+
> [!NOTE]
183
+
> Learn more: [Specify a pool in your pipeline](https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/pools-queues?view=azure-devops&tabs=yaml%2Cbrowser)
184
+
185
+
#### Step 4.1 Checkout Code
186
+
187
+
```yaml
188
+
steps:
189
+
- checkout: self
190
+
displayName: Checkout Code
191
+
192
+
```
193
+
194
+
This step pulls your repository code into the build agent, so the pipeline can access the Dockerfile and application files.
- Automatically triggers on commits and pull requests to the main branch, building and pushing Docker images.
129
-
- Authenticates securely with Docker Hub (or another registry) using an Azure DevOps service connection for credential management.
130
-
- Builds and tags Docker images with the Azure build ID and a latest tag, leveraging Docker BuildKit for efficient caching.
131
-
- Validates the built image with a simple command (e.g., version check) to ensure functionality.
132
-
- Pushes the tagged images to the specified Docker registry (e.g., Docker Hub).
133
199
134
-
**You can extend this pipeline to support:**
200
+
#### Step 4.2 Authenticate to Docker Hub
201
+
202
+
```yaml
203
+
- task: Docker@2
204
+
displayName: Docker Login
205
+
inputs:
206
+
command: login
207
+
containerRegistry: 'my-docker-registry' # Replace with your service connection name
208
+
```
209
+
210
+
Uses a preconfigured Azure DevOps Docker registry service connection to authenticate securely without exposing credentials directly.
211
+
212
+
> [!NOTE]
213
+
> Learn more: [Use service connections for Docker Hub](https://learn.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops#docker-hub-or-others)
214
+
215
+
216
+
#### Step 4.3 Build the Docker Image
217
+
218
+
```yaml
219
+
- task: Docker@2
220
+
displayName: Build Docker Image
221
+
inputs:
222
+
command: build
223
+
repository: $(imageName)
224
+
tags: |
225
+
$(buildTag)
226
+
$(latestTag)
227
+
dockerfile: './Dockerfile'
228
+
arguments: '--cache-from $(imageName):latest'
229
+
env:
230
+
DOCKER_BUILDKIT: 1
231
+
```
232
+
233
+
This builds the image with:
234
+
235
+
- Two tags: one with the build ID and one as latest
236
+
- Docker BuildKit for faster builds and layer caching
237
+
- Cache pull from the last pushed latest tag
238
+
239
+
> [!NOTE]
240
+
> Learn more: [Docker task for Azure Pipelines](https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/docker-v2?view=azure-pipelines&tabs=yaml)
241
+
242
+
243
+
#### Step 4.4 Push the Docker Image
244
+
```yaml
245
+
- task: Docker@2
246
+
displayName: Push Docker Image
247
+
inputs:
248
+
command: push
249
+
repository: $(imageName)
250
+
tags: |
251
+
$(buildTag)
252
+
$(latestTag)
253
+
254
+
```
255
+
256
+
This uploads both tags to Docker Hub:
257
+
- `$(buildTag)`ensures traceability per run.
258
+
- `latest`is used for most recent image references.
0 commit comments