HHR的小站
享受代码带来的快乐吧
首页
最新评论
使用 GitHub Actions 实现自动生成Release文件
2021-02-24 |HHR | 杂谈, 代码使人快乐

什么是 GitHub Actions

GitHub Actions是GitHub提供的一个持续集成,持续部署工具。您可以直接在 GitHub 仓库中通过 GitHub Actions 创建自定义持续集成 (CI) 和持续部署 (CD) 工作流程。

如何使用 GitHub Actions

在git项目中开启Actions功能

打开GitHub项目的主页,找到这个按钮,点击,即可进入Actions页面。
屏幕截图 2021-02-24 194455.jpg
选择一个合适的配置文件,将其加入你的项目中,即完成了持续集成的配置工作。

配置文件的样例

这是一个配置文件的样例。使用该配置文件,可以用于gradle构建的Spring Boot项目。在项目进行更新时,自动生成Spring Bootjar文件,并发布release

# This workflow will build a Java project with Gradle
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle

name: Java CI with Gradle

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11
      - name: Modify gradle config file
        run: |
          sed -e '/maven.aliyun.com/d' build.gradle >> build.gradle.1
          sed -e '/maven.aliyun.com/d' settings.gradle >> settings.gradle.1
          mv build.gradle.1 build.gradle
          mv settings.gradle.1 settings.gradle
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew
      - name: Build with Gradle
        run: ./gradlew bootjar
      - name: Get Release Info
        id: get_info
        run: |
          VERSION=`grep version build.gradle | grep '=' | grep -Eo \'.*\' | grep -Eo '[a-z|A-Z|0-9|.|_]*'`
          NAME=`grep "rootProject.name" settings.gradle | grep -Eo \'.*\' | grep -Eo '[a-z|A-Z|0-9|.|_]*'`
          FILE_PATH="./build/libs/"$NAME"-"$VERSION".jar"
          FILE_NAME=$NAME"-"$VERSION".jar"
          VERSION=$VERSION.`git rev-parse --short HEAD`
          echo ::set-output name=file_path::$FILE_PATH
          echo ::set-output name=file_name::$FILE_NAME
          echo ::set-output name=version::$VERSION
      - name: Create Release
        id: create_release
        uses: actions/create-release@master
        env:
          GITHUB_TOKEN: ${{secrets.TOKEN}}
        with:
          tag_name: Release_${{steps.get_info.outputs.version}}
          release_name: Release of version ${{steps.get_info.outputs.version}}
          draft: false # 是否是草稿
          prerelease: false # 是否是预发布
      - name: Upload Release Asset
        id: upload-release-asset
        uses: actions/upload-release-asset@master
        env:
          GITHUB_TOKEN: ${{secrets.TOKEN}}
        with:
          upload_url: ${{steps.create_release.outputs.upload_url}}
          asset_path: ${{steps.get_info.outputs.file_path}}
          asset_name: ${{steps.get_info.outputs.file_name}}
          asset_content_type: application/java-archive
      - name: rename file
        run: |
          mv ./build/libs/${{steps.get_info.outputs.file_name}} ./build/libs/${{steps.get_info.outputs.file_name}}.`git rev-parse --short HEAD`
      - name: deploy file
        uses: wlixcc/SFTP-Deploy-Action@v1.0
        with:
          username: 'server_runner'
          server: 'huhaorui.com'
          ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
          local_path: './build/libs/*'
          remote_path: '/www/wwwroot/fridge.huhaorui.com'
          args: '-o ConnectTimeout=5'
      - name: restart server
        run: |
          mkdir ~/.ssh
          ssh-keyscan huhaorui.com >> ~/.ssh/known_hosts
          echo "${{ secrets.SSH_PRIVATE_KEY }}" >> ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh 'server_runner@huhaorui.com' "kill -9 \`ps -x | grep fridge_server | sed -n '1p' | grep -Eo [0-9]{4}[0-9]+\`"
          ssh 'server_runner@huhaorui.com' "nohup java -jar /www/wwwroot/fridge.huhaorui.com/${{steps.get_info.outputs.file_name}}.`git rev-parse --short HEAD` >/dev/null 2>&1 &"

如何写一个配置文件

通过研究上面的配置,可以发现,我们可以在配置中使用已有的action,如actions/upload-release-asset@master,也可以自己编写bash脚本,用于实现特定的功能。
更多使用方法可以参阅GitHub提供的文档

respond-post-529

添加新评论

请填写称呼
请填写合法的电子邮箱地址
请填写合法的网站地址
请填写内容