如何将semantic-release与bitbucket集成(java项目)

问题描述 投票:0回答:1

任何人都可以提供使用 semantic-release 的 bitbucket 示例/指南吗?例如,一个工作存储库?有很多关于如何通过操作使用 github 来完成此操作的文档,但是对于 bitbucket,我能找到的最好的文档是这个针对节点项目的指南,但就我而言,我有一个 java-maven 项目,所以很多更改(例如我没有

package.json
的事实),我会尝试将该指南适应我的 java 项目(如果设法使其工作,并使用工作配置回答这个问题),但同时我真的很感谢任何帮助。

编辑:我将提供在 github actions 中工作的配置(我现在需要做的就是生成带有语义版本控制、pom.xml、changelog.md 更新和发行说明的新版本/标签,这就是到目前为止它正在做的事情)所以如果有人知道如何将其“翻译”到对我也适用的 bitbucket 管道:

github 操作工作流程 .yml 文件:

name: TEST CONFIG 1

on:
  workflow_dispatch:
    branches:
      - main

jobs:
  semantic_release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: 'adopt'
          java-version: '11'
      - name: Install semantic-release
        run: npm install semantic-release @semantic-release/git @semantic-release/changelog @semantic-release/github @semantic-release/exec @semantic-release/commit-analyzer conventional-changelog-conventionalcommits
      - name: Semantic release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: npx semantic-release

.releaserc.json 配置文件:

{
  "plugins": [
    [
      "@semantic-release/commit-analyzer",
      {
        "preset": "conventionalcommits",
        "releaseRules": [
          {
            "breaking": true,
            "release": "major"
          },
          {
            "type": "refactoring",
            "release": "patch"
          }
        ]
      }
    ],
    [
      "@semantic-release/release-notes-generator",
      {
        "preset": "conventionalcommits",
        "presetConfig": {
          "types": [
            {
              "type": "feat",
              "section": "New Feature(s) 🚀"
            },
            {
              "type": "fix",
              "section": "Bug Fix(es) 🐛️"
            },
            {
              "type": "docs",
              "section": "Documentation Changes 📝"
            },
            {
              "type": "refactor",
              "section": "Code Refactor 💅"
            },
            {
              "type": "test",
              "section": "Tests 📡️"
            },
            {
              "type": "perf",
              "section": "Performance Improvement(s) 🚄️"
            },
            {
              "type": "build",
              "section": "Build system 🛠️"
            },
            {
              "type": "refactoring",
              "section": "Refactoring \uD83D\uDEE0"
            }
          ]
        }
      }
    ],
    [
      "@semantic-release/github"
    ],
    [
      "@semantic-release/exec",
      {
        "prepareCmd": "mvn versions:set -DnewVersion=\"${nextRelease.version}\" && echo \"NEXT_VERSION=${nextRelease.version}, verifyReleaseCmd can also be used instead of prepareCmd\" >> build.env"
      }
    ],
    [
      "@semantic-release/changelog",
      {
        "changelogFile": "CHANGELOG.md",
        "changelogTitle": "# Semantic Versioning Changelog"
      }
    ],
    [
      "@semantic-release/git",
      {
        "assets": [
          "pom.xml",
          "service/pom.xml",
          "CHANGELOG.md"
        ],
        "message": "chore(release): ${nextRelease.version}"
      }
    ]
  ],
  "branches": [
    "main"
  ]
}

编辑2:好的,我设法为bitbucket创建一个工作配置,但它只是生成发布/新标签以及更改日志,但我无法运行mvn命令,因为管道说mvn找不到命令,当然我还需要安装 mvn,但我不知道如何告诉管道我需要 maven 和节点,如果有人可以帮助我解决这个细节,我很欣赏,这里是配置:

bitbucket-pipelines.yml:

# This is an example Starter pipeline configuration
# Use a skeleton to build, test and deploy using manual and parallel steps
# -----
# You can specify a custom docker image from Docker Hub as your build environment.

image: node:latest

pipelines:
  default:
    - step:
        name: 'Deployment to Staging'
        deployment: staging
        script:
          - echo "Your deployment to staging script goes here..."
    - step:
        name: semantic_release
        trigger: 'manual'
        image: node:latest
        script:
          # Get an oauth access token using the client credentials, parsing out the token with jq.
          - apt-get update && apt-get install -y curl jq
          - >
            export BB_TOKEN=$(curl -s -X POST -u "${CLIENT_ID}:${CLIENT_SECRET}" \
              https://bitbucket.org/site/oauth2/access_token \
              -d grant_type=client_credentials -d scopes="repository"| jq --raw-output '.access_token')
          # Configure git to use the oauth token. This well happen when setting env variable BB_TOKEN
          - npm install semantic-release @semantic-release/git @semantic-release/changelog @semantic-release/exec @semantic-release/commit-analyzer conventional-changelog-conventionalcommits
          - npx semantic-release

.releaserc.json 文件(导致管道失败的原因是该文件的第 64 行,即 mvn 命令(在prepareCmd处),但这很重要,因为这是更新存储库的pom文件的内容,这在执行前一个文件 (bitbucket-pipelines.yml) 的

npx semantic-release
命令:

{
  "plugins": [
    [
      "@semantic-release/commit-analyzer",
      {
        "preset": "conventionalcommits",
        "releaseRules": [
          {
            "breaking": true,
            "release": "major"
          },
          {
            "type": "refactoring",
            "release": "patch"
          }
        ]
      }
    ],
    [
      "@semantic-release/release-notes-generator",
      {
        "preset": "conventionalcommits",
        "presetConfig": {
          "types": [
            {
              "type": "feat",
              "section": "New Feature(s) 🚀"
            },
            {
              "type": "fix",
              "section": "Bug Fix(es) 🐛️"
            },
            {
              "type": "docs",
              "section": "Documentation Changes 📝"
            },
            {
              "type": "refactor",
              "section": "Code Refactor 💅"
            },
            {
              "type": "test",
              "section": "Tests 📡️"
            },
            {
              "type": "perf",
              "section": "Performance Improvement(s) 🚄️"
            },
            {
              "type": "build",
              "section": "Build system 🛠️"
            },
            {
              "type": "refactoring",
              "section": "Refactoring \uD83D\uDEE0"
            }
          ]
        }
      }
    ],
    [
      "@semantic-release/exec",
      {
        "prepareCmd": "mvn versions:set -DnewVersion=\"${nextRelease.version}\" && echo \"NEXT_VERSION=${nextRelease.version}, verifyReleaseCmd can also be used instead of prepareCmd\" >> build.env"
      }
    ],
    [
      "@semantic-release/changelog",
      {
        "changelogFile": "CHANGELOG.md",
        "changelogTitle": "# Semantic Versioning Changelog"
      }
    ],
    [
      "@semantic-release/git",
      {
        "assets": [
          "pom.xml",
          "service/pom.xml",
          "CHANGELOG.md"
        ],
        "message": "chore(release): ${nextRelease.version}"
      }
    ]
  ],
  "branches": [
    "main"
  ]
}
java maven bitbucket semantic-release
1个回答
0
投票

终于得到了一个有效的配置,将尽可能详细地解释每件事:

  1. 首先,您需要一个已经安装了

    maven
    git
    node
    java
    的 Docker 映像,我创建了自己的 docker 映像,其中包含所有这些(在我的情况下,我需要确保它是java 11和node 20,其他版本没有指定),这是我用来创建此图像的
    Dockerfile

    来自 ubuntu:20.04
    运行 apt-get update && apt-get install -y maven curl
    运行 apt-get install -y openjdk-11-jdk
    运行curl -sL https://deb.nodesource.com/setup_20.x |重击-
    运行 apt-get install -y nodejs
    运行 apt-get -y 安装 git

  2. 从该 Dockerfile 创建镜像并将其推送到 Docker hub

确保您已使用命令登录到 Dockerhub 帐户

docker login
然后站在同一个目录中,您是否找到了上一个
Dockerfile
(例如
D:\IntelliJ\projects-bitbucket\demo

docker build -t yourdockerhubuser/thenameyouwantforyourimage .  
docker push yourdockerhubuser/thenameyouwantforyourimage
  1. 现在您拥有了自己的 Docker 映像,其中包含执行 bitbucket 管道所需的一切,让我们继续,现在让我们配置
    bitbucket-pipelines.yml
    文件,该文件将手动执行,这里您应该使用之前推送的映像步骤(有很多带注释的代码,因为我将其用于调试目的,但我将其保留在那里注释,也许您有时会发现它有用):此外,在此配置中,您可能想知道什么是
    CLIENT_ID
    CLIENT_SECRET
    ,需要进行设置,本指南 https://medium.com/coding-spaghetti/npm-version-control-using-semantic-release-and-bitbucket-cloud-5294ac6b324b 部分中对此进行了介绍 Bitbucket Cloud 使用 Semantic-Release 发布到自己的存储库 只需按照该部分的步骤操作即可返回。
# You have to use an image that has everything you need installed, that is why we created the image that we are using here in above steps
image: yourdockerhubuser/thenameyouwantforyourimage

pipelines:
  custom:
    manual:
      #- step:
          #name: build and test
          #script:
            #- mvn clean install
      - step:
          name: semantic_release
          #trigger: 'manual'
          script:
            - apt-get update
            #- node -v
            #- npm -v
            #- java -version
            #- mvn -version
            #- git --version
            # Get an oauth access token using the client credentials, parsing out the token with jq.
            - apt-get update && apt-get install -y curl jq
            - >
              export BB_TOKEN=$(curl -s -X POST -u "${CLIENT_ID}:${CLIENT_SECRET}" \
                https://bitbucket.org/site/oauth2/access_token \
                -d grant_type=client_credentials -d scopes="repository"| jq --raw-output '.access_token')
            # Configure git to use the oauth token. This well happen when setting env variable BB_TOKEN
            - npm install semantic-release @semantic-release/git @semantic-release/changelog @semantic-release/exec @semantic-release/commit-analyzer conventional-changelog-conventionalcommits
            - npx semantic-release
  1. 在项目根目录下设置
    .releaserc.json
    文件: 该文件负责覆盖语义发布工具的默认配置,当您在
    npx semantic-release
    文件末尾调用命令
    bitbucket-pipelines.yml
    时,它将使用此配置来了解如何执行(请记住,在我的例子中)我有一个名为 service 的子模块,这就是为什么在资产部分的这个文件中我放置了 service/pom.xml,如果这不是你的情况并且你正在使用单个模块,只需删除该行,只留下你的 main pom.xml 文件和资产中的变更日志):
{
  "plugins": [
    [
      "@semantic-release/commit-analyzer",
      {
        "preset": "conventionalcommits",
        "releaseRules": [
          {
            "breaking": true,
            "release": "major"
          },
          {
            "type": "refactoring",
            "release": "patch"
          },
          {
            "type": "refactor",
            "release": "patch"
          }
        ]
      }
    ],
    [
      "@semantic-release/release-notes-generator",
      {
        "preset": "conventionalcommits",
        "presetConfig": {
          "types": [
            {
              "type": "feat",
              "section": "New Feature(s) 🚀"
            },
            {
              "type": "fix",
              "section": "Bug Fix(es) 🐛️"
            },
            {
              "type": "docs",
              "section": "Documentation Changes 📝"
            },
            {
              "type": "refactor",
              "section": "Code Refactor 💅"
            },
            {
              "type": "test",
              "section": "Tests 📡️"
            },
            {
              "type": "perf",
              "section": "Performance Improvement(s) 🚄️"
            },
            {
              "type": "build",
              "section": "Build system 🛠️"
            },
            {
              "type": "refactoring",
              "section": "Refactoring \uD83D\uDEE0"
            }
          ]
        }
      }
    ],
    [
      "@semantic-release/exec",
      {
        "prepareCmd": "mvn versions:set -DnewVersion=\"${nextRelease.version}\" && mvn clean install"
      }
    ],
    [
      "@semantic-release/changelog",
      {
        "changelogFile": "CHANGELOG.md",
        "changelogTitle": "# Semantic Versioning Changelog"
      }
    ],
    [
      "@semantic-release/git",
      {
        "assets": [
          "pom.xml",
          "service/pom.xml",
          "CHANGELOG.md"
        ],
        "message": "chore(release): ${nextRelease.version}"
      }
    ]
  ],
  "branches": [
    "main"
  ]
}

  1. 此时,您只需要在 bitbucket 中执行管道,您应该会看到它正在运行,请随时提出任何问题或反馈。
© www.soinside.com 2019 - 2024. All rights reserved.