在Azure中创建build 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.

    Locating Agent pools in the menu

  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.

    Locating SecurityPersonal Access Token in the menu

  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 variableDescriptionDefault
    AZP_AGENT_VERSIONThe version of the agent software  to installThe version we last used to test this module
    AZP_URLThe URL of your Azure DevOps organization(None)
    AZP_TOKENYour personal access token(None)
    AZP_AGENT_NAMEYour agent's name as it appears in Azure DevOpsThe system's hostname
    AZP_POOLThe name of your agent poolDefault
    AZP_WORKThe 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.

    Locating Agent pools in the menu

  4. Select MyAgentPool.

  5. Select the Agents tab.

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

    Azure DevOps showing the private agent's status

     提示

    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.

    Locating npm in the list of capabilities

    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 Pipelines showing the private agent running the build

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 DevOps showing the private agent as offline

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.

    Locating Agent pools in the menu

  4. Under MyAgentPool, select the trash can icon, and then select Delete.

    Azure DevOps showing where to delete the private agent

 

参见:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值