51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

如何使用 GitHub Actions 构建 DataX 包并发布到 GitHub Release?

起因是同事需要使用 DataX,但官方并未提供直接可用的预编译包,网上搜索一圈没有找到,所以我决定基于源码自行编译 DataX,并利用 GitHub Actions 实现自动化构建和发布。

本地编译环境准备 {#本地编译环境准备}

如果你需要本地编译,请确保您的编译环境满足以下要求:

  • Java 版本:使用 Java 8。
  • Maven 版本:使用 Maven 3.5。

注意:Maven 3.6 及以上版本会导致编译失败。

步骤一:Fork 官方仓库并下载源码 {#步骤一fork-官方仓库并下载源码}

首先,前往 DataX 的官方 GitHub 仓库 https://github.com/alibaba/DataX.git,点击 "Fork" 将项目复制到您的账户下。然后,将 Fork 后的仓库克隆到本地:

|-------------|-----------------------------------------------------------------------| | 1 2 | git clone https://github.com/your-username/DataX.git cd DataX |

步骤二:添加 Maven Wrapper 支持 {#步骤二添加-maven-wrapper-支持}

为了确保在任何环境下都能使用指定版本的 Maven,我们为项目添加 Maven Wrapper。执行以下命令:

|-----------|---------------------------| | 1 | mvn -Dmaven=3.5.4 |

这将在项目中生成 .mvn 目录和 mvnw 脚本,确保使用 Maven 3.5.4 进行构建。

步骤三:配置 GitHub Actions 实现自动化构建和发布 {#步骤三配置-github-actions-实现自动化构建和发布}

在项目根目录下,创建 .github/workflows 目录,并在其中添加 release.yml 文件,内容如下:

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | name: Build and Release DataX on: push: branches: - master create: tags: - 'v*' jobs: build: runs-on: ubuntu-latest `<span class="na">steps</span><span class="pi">:</span> <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">检出代码</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">actions/checkout@v4</span> <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">设置 JDK</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">actions/setup-java@v4</span> <span class="na">with</span><span class="pi">:</span> <span class="na">distribution</span><span class="pi">:</span> <span class="s1">'</span><span class="s">temurin'</span> <span class="na">java-version</span><span class="pi">:</span> <span class="s1">'</span><span class="s">8'</span> <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">构建 DataX</span> <span class="na">run</span><span class="pi">:</span> <span class="pi">|</span> <span class="s">./mvnw -U clean package assembly:assembly -Dmaven.test.skip=true</span> <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">创建发布 Artifact</span> <span class="na">run</span><span class="pi">:</span> <span class="pi">|</span> <span class="s">tar -cvf datax.tar target/datax</span> <span class="s">xz -9e datax.tar</span> <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">上传构建产物</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">actions/upload-artifact@v3</span> <span class="na">with</span><span class="pi">:</span> <span class="na">name</span><span class="pi">:</span> <span class="s">datax-package</span> <span class="na">path</span><span class="pi">:</span> <span class="s">datax.tar.xz</span> ` release: needs: build runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/tags/') steps: - name: 下载构建的 Artifact uses: actions/download-artifact@v3 with: name: datax-package ` <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">创建 GitHub Release</span> <span class="na">id</span><span class="pi">:</span> <span class="s">create_release</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">actions/create-release@v1</span> <span class="na">env</span><span class="pi">:</span> <span class="na">GITHUB_TOKEN</span><span class="pi">:</span> <span class="s">${{ secrets.GITHUB_TOKEN }}</span> <span class="na">with</span><span class="pi">:</span> <span class="na">tag_name</span><span class="pi">:</span> <span class="s">${{ github.ref_name }}</span> <span class="na">release_name</span><span class="pi">:</span> <span class="s2">"</span><span class="s">Release</span><span class="nv"> </span><span class="s">${{</span><span class="nv"> </span><span class="s">github.ref_name</span><span class="nv"> </span><span class="s">}}"</span> <span class="na">body</span><span class="pi">:</span> <span class="pi">|</span> <span class="s">此版本包含最新构建的 DataX 包。</span> <span class="na">draft</span><span class="pi">:</span> <span class="kc">false</span> <span class="na">prerelease</span><span class="pi">:</span> <span class="kc">false</span> <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">上传 Release 资产到 GitHub</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">svenstaro/upload-release-action@v2</span> <span class="na">with</span><span class="pi">:</span> <span class="na">repo_token</span><span class="pi">:</span> <span class="s">${{ secrets.GITHUB_TOKEN }}</span> <span class="na">file</span><span class="pi">:</span> <span class="s">datax.tar.xz</span> <span class="na">asset_name</span><span class="pi">:</span> <span class="s">datax.tar.xz</span> <span class="na">tag</span><span class="pi">:</span> <span class="s">${{ github.ref_name }}</span> ` |


注意 :选择 tar.xz 格式是因为 GitHub 对每个 Release 中的单个文件大小限制为 2 GiB,而 tar.gz 压缩后的文件约为 2.2G,超过限制。使用 tar.xz 压缩后,文件大小约为 1.64G,符合要求。

步骤四:创建标签并推送 {#步骤四创建标签并推送}

在本地创建一个新的标签,并推送到远程仓库:

|-------------|-----------------------------------------------| | 1 2 | git tag v1.0.0 git push origin v1.0.0 |

推送标签后,GitHub Actions 将自动触发构建和发布流程。

等待构建完成并下载 {#等待构建完成并下载}

大约半小时后,构建过程应已完成。前往 Fork 后的仓库的 Releases 页面,下载最新的构建包,如下图所示:

GitHub Releases

如果不想自行构建,可以直接下载我已构建好的包:https://github.com/harrisonwang/DataX/releases/download/v1.0.0/datax.tar.xz

本地编译(可选) {#本地编译可选}

如果要本地进行编译,可以跳过配置 GitHub Actions 的步骤三,直接在本地执行:

|-----------|------------------------------------------| | 1 | ./mvnw clean package -DskipTests |

构建好的包位于本地 target/datax 目录。

通过上述步骤,你就实现了 DataX 的自动化构建和发布。

赞(0)
未经允许不得转载:工具盒子 » 如何使用 GitHub Actions 构建 DataX 包并发布到 GitHub Release?