Skip to content

Commit

Permalink
fix(ci): Refactor CI and fix notifications (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
leviem1 authored Sep 29, 2024
1 parent d2d3d45 commit b50b0b0
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 99 deletions.
125 changes: 30 additions & 95 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
name: Build, Test, and Release
name: Test and Release

on:
push:
branches:
- 'main'
tags:
- 'v*' # For v1.0, v0.1.0, etc
pull_request:
branches:
- 'main'
schedule:
# PaperMC doesn't change version numbers for latest releases meaning the build may break
# unexpectedly. Build every so often so that we know if a breaking change has been published
- cron: '0 0 * * 6'
merge_group:
types:
- checks_requested
workflow_dispatch:

concurrency:
group: ${{ format('{0}-{1}', github.workflow, github.ref) }}
cancel-in-progress: true
cancel-in-progress: false

jobs:
build:
runs-on: ${{ matrix.os }}
if: github.ref_type != 'tag'
test:
name: Run unit tests
strategy:
matrix:
os: [ ubuntu-latest, windows-latest ]
java: [ 21 ]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -39,47 +30,15 @@ jobs:
with:
java-version: ${{ matrix.java }}

- name: Retrieve Project Name
run: echo "PROJECT_NAME=$(${{github.workspace}}/gradlew -q printProjectName)" >> $GITHUB_OUTPUT
id: project_name

- name: Get Project Name
run: echo "PROJECT_NAME=${{steps.project_name.outputs.PROJECT_NAME}}" >> $GITHUB_ENV

- name: Build with Gradle
run: ./gradlew assemble --info
run: ./gradlew build --info

- name: Upload build results
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.os }} Java ${{ matrix.java }} build results
path: ${{ github.workspace }}/build/libs/

outputs:
project_name: ${{ steps.project_name.outputs.PROJECT_NAME }}

test:
name: Run unit tests
runs-on: ${{ matrix.os }}
if: github.ref_type != 'tag'
strategy:
matrix:
os: [ ubuntu-latest, windows-latest ]
java: [ 21 ]
needs:
- build
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Common Setup
uses: ./.github/actions/common-setup
with:
java-version: ${{ matrix.java }}

- name: Test with Gradle
run: ./gradlew check --info

- name: Upload test results
if: ${{ always() }}
uses: actions/upload-artifact@v4
Expand All @@ -88,90 +47,66 @@ jobs:
path: ${{ github.workspace }}/build/reports/

release:
runs-on: ubuntu-latest
needs:
- build
- test
runs-on: ubuntu-latest
if: ${{ !failure() && (github.ref_type == 'tag' || (github.ref_name == 'main' && github.event_name != 'schedule')) }}
steps:
- name: Set snapshot environment
if: github.ref_name == 'main'
run: |
echo "RELEASE_TAG=0.0.0-RC-1" >> $GITHUB_ENV
- name: Set release environment
if: github.ref_type == 'tag'
run: |
echo "RELEASE_TAG=${{ github.ref_name }}" >> $GITHUB_ENV
- name: Checkout code
uses: actions/checkout@v4

- name: Common Setup
uses: ./.github/actions/common-setup
with:
java-version: 21

- name: Publish with Gradle
run: ./gradlew -Pver=${{ env.RELEASE_TAG }} release
run: ./gradlew -Pver="0.0.0-RC-1" release

- name: Create Release
uses: softprops/action-gh-release@v2
id: release
if: github.ref_type == 'tag'
- name: Upload build results
uses: actions/upload-artifact@v4
with:
files: ${{ github.workspace }}/build/libs/*
generate_release_notes: true
name: ${{ format('Release {0}', github.ref_name) }}
prerelease: ${{ contains(github.ref_name, '-rc-') }}
fail_on_unmatched_files: true
draft: true

outputs:
url: ${{ steps.release.outputs.url }}
name: Release Build
path: ${{ github.workspace }}/build/libs/

notify:
name: Send job complete notification
runs-on: ubuntu-latest
needs:
- build
- test
- release
# Run if on main or tag
if: always() && (github.ref_name == 'main' || github.ref_type == 'tag')
env:
PROJECT_NAME: ${{ needs.build.outputs.project_name }}
if: always()
steps:
- name: Set snapshot environment
if: github.ref_name == 'main'
run: |
echo "RELEASE_TYPE=snapshot" >> $GITHUB_ENV
echo "RELEASE_ADDR=https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" >> $GITHUB_ENV
- name: Set release draft environment
if: github.ref_type == 'tag'
run: |
echo "RELEASE_TYPE=release draft" >> $GITHUB_ENV
echo "RELEASE_ADDR=${{ needs.release.outputs.url }}" >> $GITHUB_ENV
- name: Checkout code
uses: actions/checkout@v4

- name: Common Setup
uses: ./.github/actions/common-setup
with:
java-version: 21

- name: Retrieve Project Name
run: echo "PROJECT_NAME=$(${{github.workspace}}/gradlew -q printProjectName)" >> $GITHUB_ENV

- name: Notify on success
if: needs.build.result == 'success' && needs.test.result == 'success' && (needs.release.result == 'success' || github.event_name == 'schedule')
if: success()
uses: appleboy/discord-action@v1.0.0
with:
webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
color: "#00FF00"
username: "${{ env.PROJECT_NAME }} Release Bot"
message: >
An ${{ env.PROJECT_NAME }} ${{ env.RELEASE_TYPE }} was deployed by ${{ github.actor }}:
${{ env.RELEASE_ADDR }}
An ${{ env.PROJECT_NAME }} snapshot was deployed by ${{ github.actor }}:
https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
- name: Notify on failure
if: needs.build.result == 'failure' || needs.test.result == 'failure' || needs.release.result == 'failure'
if: failure()
uses: appleboy/discord-action@v1.0.0
with:
webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
color: "#FF0000"
username: "${{ env.PROJECT_NAME }} Release Bot"
message: >
An ${{ env.PROJECT_NAME }} ${{ env.RELEASE_TYPE }} ran by ${{ github.actor }} failed:
An ${{ env.PROJECT_NAME }} snapshot deployment ran by ${{ github.actor }} failed:
https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
46 changes: 46 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Test

on:
pull_request:
branches:
- 'main'
merge_group:
types:
- checks_requested

concurrency:
group: ${{ format('{0}-{1}', github.workflow, github.ref) }}
cancel-in-progress: true

jobs:
test:
name: Run unit tests
strategy:
matrix:
os: [ ubuntu-latest, windows-latest ]
java: [ 21 ]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Common Setup
uses: ./.github/actions/common-setup
with:
java-version: ${{ matrix.java }}

- name: Build with Gradle
run: ./gradlew build --info

- name: Upload build results
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.os }} Java ${{ matrix.java }} build results
path: ${{ github.workspace }}/build/libs/

- name: Upload test results
if: ${{ always() }}
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.os }} Java ${{ matrix.java }} test results
path: ${{ github.workspace }}/build/reports/
13 changes: 9 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Release Notify
name: Notify on Release

on:
release:
Expand All @@ -21,18 +21,23 @@ jobs:
run: echo "PROJECT_NAME=$(${{github.workspace}}/gradlew -q printProjectName)" >> $GITHUB_ENV
id: project_name

- name: Set pre-release environment
if: github.event.prerelease == 'true'
run: |
echo "RELEASE_TYPE=pre-release" >> $GITHUB_ENV
- name: Set release environment
if: github.event.prerelease == 'false'
run: |
echo "RELEASE_TYPE=release" >> $GITHUB_ENV
echo "RELEASE_ADDR=https://github.com/${{ github.repository }}/releases/tag/${{ github.ref_name }}" >> $GITHUB_ENV
- name: Notify
uses: appleboy/discord-action@v1.0.0
with:
webhook_id: ${{ github.event.prerelease == 'false' && secrets.DISCORD_RELEASE_WEBHOOK_ID || secrets.DISCORD_WEBHOOK_ID }}
webhook_token: ${{ secrets.DISCORD_RELEASE_WEBHOOK_TOKEN || secrets.DISCORD_WEBHOOK_TOKEN }}
webhook_token: ${{ github.event.prerelease == 'false' && secrets.DISCORD_RELEASE_WEBHOOK_TOKEN || secrets.DISCORD_WEBHOOK_TOKEN }}
color: "#00FF00"
username: "${{ env.PROJECT_NAME }} Release Bot"
message: >
An ${{ env.PROJECT_NAME }} ${{ env.RELEASE_TYPE }} was deployed by ${{ github.actor }}:
${{ env.RELEASE_ADDR }}
https://github.com/${{ github.repository }}/releases/tag/${{ github.ref_name }}" >> $GITHUB_ENV
85 changes: 85 additions & 0 deletions .github/workflows/tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Release

on:
push:
tags:
- 'v*' # For v1.0, v0.1.0, etc

concurrency:
group: ${{ format('{0}-{1}', github.workflow, github.ref) }}
cancel-in-progress: true

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Common Setup
uses: ./.github/actions/common-setup

- name: Publish with Gradle
run: ./gradlew -Pver=${{ github.ref_name }} release

- name: Upload build results
uses: actions/upload-artifact@v4
with:
name: Release Build
path: ${{ github.workspace }}/build/libs/

- name: Create Release
uses: softprops/action-gh-release@v2
id: release
with:
files: ${{ github.workspace }}/build/libs/*
generate_release_notes: true
name: ${{ format('Release {0}', github.ref_name) }}
prerelease: ${{ contains(github.ref_name, '-rc-') }}
fail_on_unmatched_files: true
draft: true
outputs:
url: ${{ steps.release.outputs.url }}

notify:
name: Send job complete notification
runs-on: ubuntu-latest
needs:
- release
# Run if on main or tag
if: always()
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Common Setup
uses: ./.github/actions/common-setup
with:
java-version: 21

- name: Retrieve Project Name
run: echo "PROJECT_NAME=$(${{github.workspace}}/gradlew -q printProjectName)" >> $GITHUB_ENV

- name: Notify on success
if: success()
uses: appleboy/discord-action@v1.0.0
with:
webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
color: "#00FF00"
username: "${{ env.PROJECT_NAME }} Release Bot"
message: >
An ${{ env.PROJECT_NAME }} release draft was deployed by ${{ github.actor }}:
${{ needs.release.outputs.url }}
- name: Notify on failure
if: failure()
uses: appleboy/discord-action@v1.0.0
with:
webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
color: "#FF0000"
username: "${{ env.PROJECT_NAME }} Release Bot"
message: >
An ${{ env.PROJECT_NAME }} release draft ran by ${{ github.actor }} failed:
https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}

0 comments on commit b50b0b0

Please sign in to comment.