天天看點

在Azure中建立build agent,并使用它編譯應用Build the application using your agent

前提:

建立一個Azure DevOps Project。

在個人環境(本地或者Azure)下載下傳好代碼,設定好遠端分支。

需要做如下工作:

  • Create an Ubuntu virtual machine on Azure to serve as your build agent.
  • Create an agent pool in Microsoft Azure DevOps.
  • Create an access token to authenticate your agent with Azure DevOps.
  • Configure your agent with the software that's required to build the Space Game website.
  • Configure your agent to connect to Azure DevOps so that it can receive build jobs.
  • Verify that the agent is connected to Azure DevOps and ready to receive build jobs.

翻譯如下

  • 建立一台Azure Ubuntu VM作為build agent
  • 在Azure DevOps中建立一個agent pool
  • 在Azure DevOps建立一個access token用于連接配接agent
  • 在agent中配置應用項目所需的軟體環境
  • 配置agent連接配接Azure DevOps以便agent能夠接收build jobs
  • 驗證agent已連接配接Azure DevOps并準備接收build jobs

Create the agent pool

Recall that an agent pool organizes build agents. In this section, you create the agent pool in Azure DevOps. Later, you'll specify the name of the agent pool when you configure your agent so that it can register itself to the correct pool.

  1. In Azure DevOps, go to the Space Game - web - Agent project.
  2. Select Project settings.
  3. Under Pipelines, select Agent pools.
    在Azure中建立build agent,并使用它編譯應用Build the application using your agent
  4. Select Add pool.
  5. In the Add pool window:
    1. Under Pool to link, select New.
    2. Under Pool type, select Self-hosted.
    3. Under Name, enter MyAgentPool.
    In practice, you would choose a more descriptive name for the purpose of your pool.
  6. Select Create. The new agent pool appears in the list.

Create a personal access token

For your build agent to register itself with Azure DevOps, you need a way for it to authenticate itself.

To do that, you create a personal access token. A personal access token, or PAT, is an alternative to a password. You can use the PAT to authenticate with services such as Azure DevOps.

 重要

As you would with a password, be sure to keep your access token in a safe place. In this section, you store your access token as an environment variable so that it doesn't appear in your shell script.

  1. In Azure DevOps, open your profile settings, and then select Personal access token.
    在Azure中建立build agent,并使用它編譯應用Build the application using your agent
  2. Select New Token.
  3. Enter a name for your token, such as Build agent.
  4. Under Scopes, select Show all scopes at the bottom.
  5. Look for Agent Pools, and then select Read & manage.
  6. Look for Build, and then select Read & execute.
  7. Select Create.
  8. Copy the token to a safe place.

    Shortly, you'll use your token to enable your build agent to authenticate access to Azure Pipelines.

Install agent software on your VM

Now it's time to install the agent software on your VM. This software enables the VM to act as a build agent and receive build jobs from Azure Pipelines.

The registration process checks for installed software before it registers the agent with Azure Pipelines. Therefore, it's important to set up the agent after you install all other software. In practice, you can register the agent a second time if you need to install additional software.

The documentation explains how to manually set up self-hosted Linux agents  as well as macOS and Windows agents. You run a shell script to configure your agent, much like the way you set up build tools in the preceding section.

The script that you run here is for learning purposes. In practice, you should first understand how each command in the scripts you build affect the overall system. At the end of the module, we'll point to documentation that more completely describes your options.

  1. To download a shell script named build-agent.sh from GitHub, run the following 

    curl

     command:
    curl https://raw.githubusercontent.com/MicrosoftDocs/mslearn-azure-pipelines-build-agent/master/build-agent.sh > build-agent.sh
               
  2. Print the script to the terminal so that you can examine its contents.
    cat build-agent.sh
               
    The following result is displayed:
    #!/bin/bash
    set -e
    
    # Select a default agent version if one is not specified
    if [ -z "$AZP_AGENT_VERSION" ]; then
      AZP_AGENT_VERSION=2.164.1
    fi
    
    # Verify Azure Pipelines token is set
    if [ -z "$AZP_TOKEN" ]; then
      echo 1>&2 "error: missing AZP_TOKEN environment variable"
      exit 1
    fi
    
    # Verify Azure DevOps URL is set
    if [ -z "$AZP_URL" ]; then
      echo 1>&2 "error: missing AZP_URL environment variable"
      exit 1
    fi
    
    # If a working directory was specified, create that directory
    if [ -n "$AZP_WORK" ]; then
      mkdir -p "$AZP_WORK"
    fi
    
    # Create the Downloads directory under the user's home directory
    if [ -n "$HOME/Downloads" ]; then
      mkdir -p "$HOME/Downloads"
    fi
    
    # Download the agent package
    curl https://vstsagentpackage.azureedge.net/agent/$AZP_AGENT_VERSION/vsts-agent-linux-x64-$AZP_AGENT_VERSION.tar.gz > $HOME/Downloads/vsts-agent-linux-x64-$AZP_AGENT_VERSION.tar.gz
    
    # Create the working directory for the agent service to run jobs under
    if [ -n "$AZP_WORK" ]; then
      mkdir -p "$AZP_WORK"
    fi
    
    # Create a working directory to extract the agent package to
    mkdir -p $HOME/azp/agent
    
    # Move to the working directory
    cd $HOME/azp/agent
    
    # Extract the agent package to the working directory
    tar zxvf $HOME/Downloads/vsts-agent-linux-x64-$AZP_AGENT_VERSION.tar.gz
    
    # Install the agent software
    ./bin/installdependencies.sh
    
    # Configure the agent as the sudo (non-root) user
    chown $SUDO_USER $HOME/azp/agent
    sudo -u $SUDO_USER ./config.sh --unattended \
      --agent "${AZP_AGENT_NAME:-$(hostname)}" \
      --url "$AZP_URL" \
      --auth PAT \
      --token "$AZP_TOKEN" \
      --pool "${AZP_POOL:-Default}" \
      --work "${AZP_WORK:-_work}" \
      --replace \
      --acceptTeeEula
    
    # Install and start the agent service
    ./svc.sh install
    ./svc.sh start
               
    You don't need to understand how each line works, but here's a brief summary of what this script does:
    • It downloads the agent package as a .tar.gz file and extracts its contents.
    • In the extracted files, the script:
      • Runs a shell script named installdependencies.sh to install the agent software.
      • Runs a shell script named config.sh to configure the agent and register the agent with Azure Pipelines.
      • Runs a shell script named svc.sh to install and start the agent service.
    The script uses environment variables to enable you to provide details about your Azure DevOps organization. Here's a summary:
    表 1
    Bash variable Description Default

    AZP_AGENT_VERSION

    The version of the agent software  to install The version we last used to test this module

    AZP_URL

    The URL of your Azure DevOps organization (None)

    AZP_TOKEN

    Your personal access token (None)

    AZP_AGENT_NAME

    Your agent's name as it appears in Azure DevOps The system's hostname

    AZP_POOL

    The name of your agent pool Default

    AZP_WORK

    The working directory for the agent to perform build tasks _work

    If the script doesn't provide a default value for a variable that's not set, the script prints an error message and immediately exits.

    In the steps that follow, set these environment variables:

    • AZP_AGENT_VERSION

    • AZP_URL

    • AZP_TOKEN

    • AZP_AGENT_NAME

    • AZP_POOL

    For now, we recommend that you leave the other variables unset.
  3. Set the 

    AZP_AGENT_NAME

     environment variable to specify your agent's name. We recommend MyLinuxAgent.
    export AZP_AGENT_NAME=MyLinuxAgent
               
  4. Set the 

    AZP_URL

     environment variable to specify the URL to your Azure DevOps organization.

    Replace <organization> with yours. You can get the name from the browser tab that displays Azure DevOps.

    export AZP_URL=https://dev.azure.com/organization
               
  5. Set the 

    AZP_TOKEN

     environment variable to specify your personal access token.

    Replace <token> with your token.

    export AZP_TOKEN=token
               
  6. Set the 

    AZP_POOL

     environment variable to specify the name of your agent pool. Earlier, you created a pool named MyAgentPool.
    export AZP_POOL=MyAgentPool
               
  7. Set the 

    AZP_AGENT_VERSION

     environment variable to specify the latest version of the agent.
    export AZP_AGENT_VERSION=$(curl -s https://api.github.com/repos/microsoft/azure-pipelines-agent/releases | jq -r '.[0].tag_name' | cut -d "v" -f 2)
               
    A YAML pipeline on a Linux machine must be using the latest version of the agent, even if it is pre-release. The agent software is constantly updating, so you 

    curl

     the version information from the GitHub repo . The command uses 

    jq

     to read the latest version from the JSON string that's returned.
  8. Print the agent version to the console. Optionally, check  to make sure this is the latest version.
    echo $AZP_AGENT_VERSION
               
  9. Make the script executable, and then run it.
    chmod u+x build-agent.sh
    sudo -E ./build-agent.sh
               

    sudo

     enables the script to run as the root user. The 

    -E

     argument preserves the current environment variables, including the ones you set, so that they are available to the script.

    As the script runs, you can see the agent connect to Azure DevOps, see it added to the agent pool, and see the agent connection be tested.

Verify that the agent is running

You've successfully installed the build tools and the agent software on your VM. As a verification step, go to Azure DevOps and see your agent in the agent pool.

  1. In Azure DevOps, go to the Space Game - web - Agent project.
  2. Select Project settings.
  3. Under Pipelines, select Agent pools.
    在Azure中建立build agent,并使用它編譯應用Build the application using your agent
  4. Select MyAgentPool.
  5. Select the Agents tab.

    You can see that your agent is online and ready to accept build jobs.

    在Azure中建立build agent,并使用它編譯應用Build the application using your agent

     提示

    If your build agent shows as Offline, try waiting a few moments and then refreshing the page.

  6. Select your agent, MyLinuxAgent.
  7. Select the Capabilities tab.

    During setup, the configuration process scanned your build agent for tool capabilities. You see that 

    npm

     is listed as one of them. Recall that your original build configuration specified that 

    npm

     must be installed on the agent.
    在Azure中建立build agent,并使用它編譯應用Build the application using your agent
    When you specify which agent pool to use, you can include any of these entries in your 

    demands

     section. Including them ensures that Azure Pipelines chooses a build agent that has the software you need to build your application. It also enables you to create agent pools with various software configurations. Azure Pipelines will select the correct configuration based on your requirements.

Modify the build configuration

In this section, you modify the build configuration to switch from using a Microsoft-hosted agent to using the agent from your build pool.

  1. In Visual Studio Code, open the azure-pipelines.yml file, and then look for the 

    pool

     section.
    pool:
      vmImage: 'ubuntu-18.04'
      demands:
      - npm
               
  2. Modify the 

    pool

     section, as shown here:
    pool:
      name: 'MyAgentPool'
      demands:
      - npm
               
    This version uses 

    name

     to specify your agent pool, MyAgentPool. It maintains the 

    demands

     section to specify that the build agent must have npm, the Node.js package manager, installed.
  3. In the integrated terminal, add azure-pipelines.yml to the index, commit the changes, and push the branch up to GitHub.
    git add azure-pipelines.yml
    git commit -m "Use private agent pool"
    git push origin build-agent
               

Watch Azure Pipelines use your build agent

Watch the build run in the pipeline by using your build agent.

  1. In Azure DevOps, go to the Space Game - web - Agent project.
  2. On the project page or in the left pane, select Pipelines.
  3. Select Builds, and then select the running build.
  4. Trace the build through each of the steps.

    From the Initialize job task, you see that the build uses your build agent.

    在Azure中建立build agent,并使用它編譯應用Build the application using your agent

Optional: Remove your build pool

For future reference, you can keep the build pool configuration in your Azure DevOps organization. But keep in mind that the VM we provide will no longer be available to you after your sandbox session ends.

In fact, Azure DevOps will detect that the agent is offline. Azure Pipelines will check for an available agent the next time a build is queued by using the MyAgentPool pool.

在Azure中建立build agent,并使用它編譯應用Build the application using your agent

As an optional step, you can remove the build pool configuration from Azure DevOps. Here's how:

  1. In Azure DevOps, go to the Space Game - web - Agent project.
  2. Select Project settings.
  3. Under Pipelines, select Agent pools.
    在Azure中建立build agent,并使用它編譯應用Build the application using your agent
  4. Under MyAgentPool, select the trash can icon, and then select Delete.
    在Azure中建立build agent,并使用它編譯應用Build the application using your agent

參見:

  • Create a build agent that runs on Azure

https://docs.microsoft.com/zh-cn/learn/modules/host-build-agent/4-create-build-agent

Build the application using your agent

https://docs.microsoft.com/zh-cn/learn/modules/host-build-agent/5-build-application

繼續閱讀