Azure代理与docker ./start.sh没有此文件或目录。

6 浏览
0 Comments

Azure代理与docker ./start.sh没有此文件或目录。

我正在按照Microsoft的文档进行操作,链接如下:https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/docker?view=azure-devops#add-tools-and-customize-the-container

这是我的dockerfile:

FROM ubuntu:20.04
RUN DEBIAN_FRONTEND=noninteractive apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get upgrade -y
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \
    apt-transport-https \
    apt-utils \
    ca-certificates \
    curl \
    git \
    iputils-ping \
    jq \
    lsb-release \
    software-properties-common
RUN curl -sL https://aka.ms/InstallAzureCLIDeb | bash
# 可以是 'linux-x64', 'linux-arm64', 'linux-arm', 'rhel.6-x64'.
ENV TARGETARCH=linux-x64
WORKDIR /azp
COPY ./start.sh .
RUN chmod +x start.sh
ENTRYPOINT [ "./start.sh" ]

这是我的start.sh:

#!/bin/bash
set -e
if [ -z "$AZP_URL" ]; then
  echo 1>&2 "error: missing AZP_URL environment variable"
  exit 1
fi
if [ -z "$AZP_TOKEN_FILE" ]; then
  if [ -z "$AZP_TOKEN" ]; then
    echo 1>&2 "error: missing AZP_TOKEN environment variable"
    exit 1
  fi
  AZP_TOKEN_FILE=/azp/.token
  echo -n $AZP_TOKEN > "$AZP_TOKEN_FILE"
fi
unset AZP_TOKEN
if [ -n "$AZP_WORK" ]; then
  mkdir -p "$AZP_WORK"
fi
export AGENT_ALLOW_RUNASROOT="1"
cleanup() {
  if [ -e config.sh ]; then
    print_header "Cleanup. Removing Azure Pipelines agent..."
    # If the agent has some running jobs, the configuration removal process will fail.
    # So, give it some time to finish the job.
    while true; do
      ./config.sh remove --unattended --auth PAT --token $(cat "$AZP_TOKEN_FILE") && break
      echo "Retrying in 30 seconds..."
      sleep 30
    done
  fi
}
print_header() {
  lightcyan='\033[1;36m'
  nocolor='\033[0m'
  echo -e "${lightcyan}$1${nocolor}"
}
# Let the agent ignore the token env variables
export VSO_AGENT_IGNORE=AZP_TOKEN,AZP_TOKEN_FILE
print_header "1. Determining matching Azure Pipelines agent..."
AZP_AGENT_PACKAGES=$(curl -LsS \
    -u user:$(cat "$AZP_TOKEN_FILE") \
    -H 'Accept:application/json;' \
    "$AZP_URL/_apis/distributedtask/packages/agent?platform=$TARGETARCH&top=1")
AZP_AGENT_PACKAGE_LATEST_URL=$(echo "$AZP_AGENT_PACKAGES" | jq -r '.value[0].downloadUrl')
if [ -z "$AZP_AGENT_PACKAGE_LATEST_URL" -o "$AZP_AGENT_PACKAGE_LATEST_URL" == "null" ]; then
  echo 1>&2 "error: could not determine a matching Azure Pipelines agent"
  echo 1>&2 "check that account '$AZP_URL' is correct and the token is valid for that account"
  exit 1
fi
print_header "2. Downloading and extracting Azure Pipelines agent..."
curl -LsS $AZP_AGENT_PACKAGE_LATEST_URL | tar -xz & wait $!
source ./env.sh
print_header "3. Configuring Azure Pipelines agent..."
./config.sh --unattended \
  --agent "${AZP_AGENT_NAME:-$(hostname)}" \
  --url "$AZP_URL" \
  --auth PAT \
  --token $(cat "$AZP_TOKEN_FILE") \
  --pool "${AZP_POOL:-Default}" \
  --work "${AZP_WORK:-_work}" \
  --replace \
  --acceptTeeEula & wait $!
print_header "4. Running Azure Pipelines agent..."
trap 'cleanup; exit 0' EXIT
trap 'cleanup; exit 130' INT
trap 'cleanup; exit 143' TERM
chmod +x ./run-docker.sh
# To be aware of TERM and INT signals call run.sh
# Running it with the --once flag at the end will shut down the agent after the build is executed
./run-docker.sh "$@" & wait $!

我可以正常构建一切,这是输出结果:

[+] Building 185.9s (13/13) FINISHED
 => [internal] load build definition from Dockerfile                                                               0.1s
 => => transferring dockerfile: 32B                                                                                0.0s
 => [internal] load .dockerignore                                                                                  0.1s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/library/ubuntu:20.04                                                    0.6s
 => CACHED [1/8] FROM docker.io/library/ubuntu:20.04@sha256:fd92c36d3cb9b1d027c4d2a72c6bf0125da82425fc2ca37c414d4  0.0s
 => [internal] load build context                                                                                  0.1s
 => => transferring context: 30B                                                                                   0.0s
 => [2/8] RUN DEBIAN_FRONTEND=noninteractive apt-get update                                                       10.4s
 => [3/8] RUN DEBIAN_FRONTEND=noninteractive apt-get upgrade -y                                                    8.8s
 => [4/8] RUN DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends     apt-transport-ht  53.5s
 => [5/8] RUN curl -sL https://aka.ms/InstallAzureCLIDeb | bash                                                   54.7s
 => [6/8] WORKDIR /azp                                                                                             0.1s
 => [7/8] COPY ./start.sh .                                                                                        0.1s
 => [8/8] RUN chmod +x start.sh                                                                                    0.5s
 => exporting to image                                                                                            56.8s
 => => exporting layers                                                                                           56.8s
 => => writing image sha256:fadefaae070c65381941b5a17a063d2248ebaba97c10d8a131dac711f153ae50                       0.0s
 => => naming to docker.io/library/dockeragent:latest                                                              0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

但是当我运行以下命令来运行该镜像时:

docker run -e AZP_URL=https://myazureurl -e AZP_TOKEN=myTokenIGenerated -e AZP_POOL=myAgentPool -e AZP_AGENT_NAME=myAgentName dockeragent:latest

我收到以下错误:

exec ./start.sh: no such file or directory

但是start.sh与我的dockerfile在同一个文件夹中。

0
0 Comments

在使用Azure代理与Docker时,出现了"./start.sh: 没有那个文件或目录"的错误。这个问题的出现原因是在Windows中创建start.sh文件时,它会创建一个以CRLF行结束的文件。而Linux使用LF行结束符,因此需要将其转换为LF格式。解决方法是使用Notepad++打开文件,右键点击底部右侧显示的Windows CRLF,并将其切换为Linux LF。

0