使Azure devops管道不读取远程运行脚本的输出

mbzjlibv  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(105)

我已经创建了一些脚本来检查AWS ECR中是否有更新版本的镜像,如果它确实拉取了它,并删除了以前的镜像。
我已经创建了一个azure-pipeline.yml脚本,在将新版本推送到我的仓库后运行。
在最后一步,我正在对另一台机器执行SSH命令,并执行上面提到的脚本。

## Docker
# Build a Docker image
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
  - master

resources:
  - repo: self

variables:
  - group: AWS_ECR

stages:
  - stage: Build
    displayName: Build image
    jobs:
      - job: Build
        displayName: Build
        pool:
          vmImage: ubuntu-latest
        steps:
          - checkout: self
            fetchDepth: 0
          - task: gitversion/setup@0
            displayName: Install GitVersion
            inputs:
              versionSpec: "5.10.x"

          - task: gitversion/execute@0
            displayName: Determine Version
            inputs:
              useConfigFile: true
              configFilePath: ./GitVersion.yml
          - script: |
              aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com
            displayName: 'Login to AWS'
            env:
              AWS_ACCESS_KEY_ID: $(AWS_ACCESS_KEY_ID)
              AWS_SECRET_ACCESS_KEY: $(AWS_SECRET_ACCESS_KEY)
          - task: Docker@2
            displayName: Build an image
            inputs:
              repository: $(BACKEND_REPOSITORY_URI)
              command: buildAndPush
              dockerfile: '$(Build.SourcesDirectory)/Dockerfile'
              tags: |
                $(GitVersion.FullSemVer)
                latest
          - task: SSH@0
            displayName: 'Run updateImage script'
            inputs:
              sshEndpoint: 'ssh'
              runOptions: inline
              inline: |
                cd /home/ubuntu/composer
                nohup ./updateImage.sh $(AWS_ACCOUNT_ID) $(AWS_REGION) $(BACKEND_REPOSITORY_NAME) &
            continueOnError: true

我试着用nohup &运行它,让它在后台运行,但由于某种原因,管道从脚本中获取输出。
updateImage.sh

#!/bin/bash

# Check if required arguments are provided
if [ "$#" -lt 3 ]; then
  echo "Usage: $0 <AWS_ACCOUNT_ID> <AWS_REGION> <REPOSITORY_NAME>"
  exit 1
fi

# Assign script arguments to variables
AWS_ACCOUNT_ID=$1
AWS_REGION=$2
REPOSITORY_NAME=$3

# Navigate to the directory
cd /home/ubuntu/HighScaleComposer

# Pull the newest image
docker pull "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPOSITORY_NAME}:latest"

# Run Docker Compose with the newest image
docker compose -f docker-compose.yaml up -d && sleep 5

# Remove other images (excluding latest)
docker images --format "{{.ID}} {{.Repository}}:{{.Tag}}" | grep "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPOSITORY_NAME}" | grep -v latest | awk '{print $1}' | xargs -I {} docker rmi {}

这是来自失败的管道的日志:

542bd72cf3c7:  Verifying Checksum

542bd72cf3c7:  Download complete

542bd72cf3c7: Pull complete

b57d17fdbf95: Pull complete

Digest: sha256:57abc895af88a97389baf56321ff17c9ef7b55e605aceed002e2c3be87ad019e

Status: Downloaded newer image for
***.dkr.ecr.eu-central-1.amazonaws.com/highscale_backend:latest

***.dkr.ecr.eu-central-1.amazonaws.com/highscale_backend:latest

##[error] Container frontend  Running

##[error] Container mysql_container  Running

##[error] Container backend  Recreate

##[error] Container backend  Recreated

##[error] Container backend  Starting

##[error] Container backend  Started

Untagged:
***.dkr.ecr.eu-central-1.amazonaws.com/highscale_backend@sha256:cd50c51220508921088e2871c76b64464ef4a607c7457026439486e20ff255d1

Deleted: sha256:39b2f42e37ac24ecb06a637b346a6cb6dd3ef8eacb12dd2731fe58476c2573c1

Deleted: sha256:ffc8e4f5ddfb398588b2a792ce93853d15b877a588ae179e1d5c44c9906b49f3

Deleted: sha256:a6318b95525b5d1ed1ec6a61ab9ec1b48d236d7ef3e26a2bf5542e4160c01f25

Finishing: Run updateImage script

这是从机器执行脚本的输出:

ubuntu@ip-172-31-9-59:~/composer$ ./updateImage.sh 274129698771 eu-central-1 backend
latest: Pulling from backend
96526aa774ef: Already exists
872bc298c5d2: Already exists
2a645fa80061: Pull complete
6dd5299324c9: Pull complete
Digest: sha256:cc5331ad95b28bbcb72de4e0519c24b2e5537d174a48f7304c60e355c8ca74e4
Status: Downloaded newer image for *.dkr.ecr.eu-central-1.amazonaws.com/backend:latest
*.dkr.ecr.eu-central-1.amazonaws.com/_backend:latest
[+] Running 3/3
 ✔ Container frontend         Running                                                                                                                                                                                                                   0.0s
 ✔ Container mysql_container  Running                                                                                                                                                                                                                   0.0s
 ✔ Container backend          Started                                                                                                                                                                                                                  10.6s
Untagged: *.dkr.ecr.eu-central-1.amazonaws.com/backend@sha256:e12d269915ec79709e219c4bc9d3c83e64cccf00bce9833124f607a7915ff80a
Deleted: sha256:c09bedd861bcd75329a6dc42b13e39ff0966aeadac1c95647e9f40af99bc708d
Deleted: sha256:a7e3e4a081b86994a08d544dc404dc66e6f1abcf6882b36d92c4e32f71c01ad4
Deleted: sha256:32815dad1d9b147e30fe198808ebcc546b380737d02aaf7693cb532a681f42e5

我猜是找到了那个error字,这让我的管道失败了,我该怎么处理?

mcdcgff0

mcdcgff01#

您可以尝试使用**gnome-terminal**命令在Bash中打开一个新的终端会话来运行脚本。
举例来说:

gnome-terminal -x bash -c "./updateImage.sh $(AWS_ACCOUNT_ID) $(AWS_REGION) $(BACKEND_REPOSITORY_NAME); exec bash"

此命令末尾的“exec bash”将在完成命令后保持终端会话打开。如果你不需要它,你可以从命令中删除它。

相关问题