在 pom.xml 内部使用 azure devops 管道更新快照版本

问题描述 投票:0回答:1
stages:
- stage: Build
  jobs:
  - job: BuildJob
    steps:
    - checkout: self
      persistCredentials: true
    - script: |
        #!/bin/bash
        increment_version() {
            current_version=$1
            major=$(echo "$current_version" | cut -d '.' -f1)
            minor=$(echo "$current_version" | cut -d '.' -f2)
            minor=$((minor + 1))
            if [ $minor -gt 9 ]; then
                major=$((major + 1))
                minor=0
            fi
            echo "$major.$minor"
        }
        update_pom_xml() {
          pom_file="$1"
          ver=$(grep -oP '<version>\K[^<]*' "$pom_file" | grep 'SNAPSHOT' | sed 's/-SNAPSHOT//')
          if [ -n "$ver" ]; then
              echo "Updating version in $pom_file"
              new_version=$(increment_version "$ver")
              echo "ver: $ver"
              echo "new_version: $new_version"
              updated_pom_content=$(sed "s|<version>$ver-SNAPSHOT</version>|<version>$new_version-SNAPSHOT</version>|g" "$pom_file")
              echo "updated_pom_content: $updated_pom_content"
              echo "$updated_pom_content" > "$pom_file"
              echo "Version updated in $pom_file to $new_version-SNAPSHOT"
              echo "$pom_file" >> updated_poms.txt
          fi
        }
        updated_pom_count=0
        while IFS= read -r -d '' pom_file; do
            update_pom_xml "$pom_file"
            ((updated_pom_count++))
        done < <(find . -type f -name 'pom.xml' -print0)

        echo "Total updated pom.xml files: $updated_pom_count"

        # Set user information
        git config --global user.email "[email protected]"
        git config --global user.name "venturing digitally"

        # Add, commit, and push changes
        git branch -a
        git checkout investigation/unit_test_implementation_pipeline
        git add .
        git commit -m "Update pom.xml files"  
        git diff
        git push origin investigation/unit_test_implementation_pipeline  # Replace "master" with your branch name if different
      displayName: 'Update and Push pom.xml files'

这是我的版本更新代码。如何使用管道将该代码推送到我的 Azure DevOps 存储库?

github azure-devops snapshot
1个回答
0
投票

我在管道中测试了以下 yaml,并且您的“添加、提交和推送更改”代码是正确的。

您需要做的就是在项目设置 -> 存储库 -> 安全性中为您的 build 服务帐户授予 Git 'GenericContribute' 权限。您还可以搜索身份ID(错误消息中的xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx)来查找您正在使用的构建服务帐户。

否则,你可能会得到错误

远程:TF401027:您需要 Git 'GenericContribute' 权限才能执行此操作。详细信息:身份“Build\xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx”,范围“存储库”。

stages:
- stage: Build
  jobs:
  - job: BuildJob
    steps:
    - checkout: self
      persistCredentials: true
    - script: |
        # Generate a new pom.xml to pretend I updated the pom.xml
        echo "update pom.xml files" >>pom.xml

        # Set user information
        git config --global user.email "[email protected]"
        git config --global user.name "test"

        # Add, commit, and push changes
        git branch -a
        git checkout main
        git pull
        git add .
        git commit -m "Update pom.xml files"  
        git diff
        git push origin main # Replace your branch name if different
      displayName: 'Update and Push pom.xml files'

测试结果:

© www.soinside.com 2019 - 2024. All rights reserved.