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

kq0g1dla  于 12个月前  发布在  Java
关注(0)|答案(1)|浏览(93)

有人能提供一个使用semantic-release的bitbucket的例子/指南吗?比如一个工作仓库?关于如何通过操作在github上实现这一点,有很多文档,但是对于bitbucket,我能找到的最好的文档是this guide which is directed for node projects,但是在我的情况下,我有一个java-maven项目,所以有很多变化。(就像我没有package.json一样),我会尝试将该指南适用于我的Java项目(如果能够使其工作,请使用工作配置回答这个问题),但同时我真的很感谢任何帮助。

编辑:我将提供我在github操作中工作的配置(我现在需要它做的就是用语义版本化,pom.xml,www.example.com更新和发行说明生成新的版本/标签changelog.md,这就是它目前所做的),所以如果有人知道如何将它“翻译”到bitbucket管道,也可以为我工作:
github actions workflow .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创建了一个工作配置,但它只生成了release/new标签和changelog,但我无法运行mvn命令,因为管道说mvn命令找不到,当然我也需要安装mvn,但我不知道如何告诉管道我需要maven和node,如果有人能帮助我解决这个细节,我很感激,下面是一些例子:
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命令(在BitReCmd),但这很重要,因为这是更新存储库的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"
  ]
}

yb3bgrhw

yb3bgrhw1#

最后得到了一个工作配置,我将尽可能一步一步地解释每件事:
1.首先,你需要一个已经安装了mavengitnodejava的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
RUN curl -sL https://deb.nodesource.com/setup_20.x|猛击
运行apt-get install -y nodejs
运行apt-get -y install git
1.从该Dockerfile创建镜像并将其推送到Docker hub
确保您使用命令docker login登录到您的Dockerhub帐户,然后站在您找到前一个Dockerfile的同一目录(例如D:\IntelliJ\projects-bitbucket\demo

docker build -t yourdockerhubuser/thenameyouwantforyourimage .  
docker push yourdockerhubuser/thenameyouwantforyourimage

字符串
1.现在你有了自己的Docker镜像,它包含了执行bitbucket管道所需的一切,让我们继续,现在让我们配置bitbucket-pipelines.yml文件,它将手动执行,这里你应该使用你在上一步中推送的镜像(这里有很多注解代码,因为我使用它是为了调试的目的,但我把它留在那里注解,也许你会发现它有用的时候):此外,在此配置中,您可能会想知道什么是CLIENT_IDCLIENT_SECRET,需要设置,这在本指南https://medium.com/coding-spaghetti/npm-version-control-using-semantic-release-and-bitbucket-cloud-5294ac6b324b中的一节中介绍Bitbucket Cloud Publishing to Own Repo using 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文件:此文件负责覆盖语义发布工具的默认配置,当您在bitbucket-pipelines.yml文件末尾调用命令npx semantic-release时,它将使用此配置来了解如何执行(请记住,在我的例子中,我有一个名为service的子模块,这就是为什么在这个文件的assets部分中,我把service/pom. xml,如果您不是这种情况,并且您正在使用单个模块,请删除这一行,只在assets中保留主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中执行管道,您应该看到它正在工作,请随时询问任何问题或反馈。

相关问题