正解:
See explanation below
Explanation:
Task 7: Create a Pipeline to Deploy a Docker Image in a New Branch
Step 1: Create a New Branch
* In your Azure DevOps Project (Project1), navigate to Repos.
* In the left menu, click on Branches.
* Click New branch.
* Enter:
* Branch name: azure-pipelines
* Based on: main (or your default branch)
* Click Create.
This creates a new branch named azure-pipelines.
Step 2: Switch to the New Branch
* In Repos, click on Files.
* In the top-left branch selector, choose azure-pipelines.
Step 3: Use the Predefined Docker Pipeline Template
Azure Pipelines has predefined YAML templates for common tasks, including Docker.
Here's how to add it:
* In the azure-pipelines branch, click New file.
* Name the file: azure-pipelines.yml.
* Click the Show templates button in the YAML editor (if available).
* Search for the Docker template in the list of predefined templates.
* Select the Docker template to auto-populate the YAML file.
If the editor doesn't show the template picker, you can manually add the following basic Docker pipeline template:
yaml
Copy
# Predefined Docker pipeline template
trigger:
- main
resources:
- repo: self
variables:
imageName: 'mydockerimage'
stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
pool:
vmImage: 'ubuntu-latest'
steps:
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(imageName)
dockerfile: '**/Dockerfile'
containerRegistry: '<your-container-registry-service-connection>'
tags: |
latest
Replace <your-container-registry-service-connection> with your actual Azure Container Registry (ACR) or DockerHub service connection name.
Step 4: Commit the Pipeline to the New Branch
* In the YAML editor, review and adjust if needed (like setting the correct container registry service connection).
* In the bottom commit message box:
* Message: e.g., Add Docker deployment pipeline
* Ensure the branch is set to azure-pipelines.
* Click Commit (not commit to main).
This creates the pipeline YAML file in the new azure-pipelines branch.
Step 5: Create the Pipeline in Azure DevOps
* In the left menu, click Pipelines.
* Click New pipeline.
* Choose:
* Azure Repos Git.
* Select your repo.
* Select Existing Azure Pipelines YAML file.
* In the branch selector, choose the azure-pipelines branch.
* Select the YAML file path (azure-pipelines.yml).
* Click Continue and then Run to validate the pipeline.