Skip to content

Commit 46328d1

Browse files
authored
chore: Several Improvements to Workflow. (#113)
* Update main.yml 1. Add new output and env for project_name 2. Cache Gradle Dependencies 3. Remove hardcoded values from Discord Action 4. Add deployed by 5. Add a action 'common-setup' to stop duplicating self * Create action.yml * Missed the new printProjectName task
1 parent 9596153 commit 46328d1

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: 'Common Setup'
2+
description: 'Set up JDK and grant execute permissions for gradlew'
3+
4+
inputs:
5+
java-version:
6+
description: 'Java version'
7+
required: true
8+
default: '17'
9+
10+
runs:
11+
using: 'composite'
12+
steps:
13+
- name: Set up JDK
14+
uses: actions/setup-java@v3
15+
with:
16+
java-version: ${{ inputs.java-version }}
17+
distribution: 'zulu'
18+
19+
- name: Grant execute permission for gradlew
20+
run: chmod +x gradlew
21+
shell: bash

.github/workflows/main.yml

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ on:
1414
workflow_dispatch:
1515

1616
concurrency:
17-
# Allow concurrent run for main, MR, and tag
18-
# Disallow concurrent runs on same MRs, tags, and main (triggered by dispatch, schedule, or push)
1917
group: ${{ format('{0}-{1}', github.job, github.ref) }}
2018
cancel-in-progress: true
2119

@@ -30,15 +28,27 @@ jobs:
3028
- name: Checkout code
3129
uses: actions/checkout@v3
3230

33-
- name: Set up JDK
34-
uses: actions/setup-java@v3
31+
- name: Cache Gradle dependencies
32+
uses: actions/cache@v2
33+
with:
34+
path: |
35+
~/.gradle/caches
36+
~/.gradle/wrapper
37+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
38+
restore-keys: |
39+
${{ runner.os }}-gradle-
40+
41+
- name: Retrieve Project Name
42+
run: echo "::set-output name=PROJECT_NAME::$(${{github.workspace}}/gradlew -q printProjectName)"
43+
id: project_name
44+
45+
- name: Get Project Name
46+
run: echo "PROJECT_NAME=${{steps.project_name.outputs.PROJECT_NAME}}" >> $GITHUB_ENV
47+
48+
- name: Common Setup
49+
uses: ./.github/actions/common-setup
3550
with:
3651
java-version: ${{ matrix.java }}
37-
distribution: 'zulu'
38-
39-
- name: Grant execute permission for gradlew
40-
if: runner.os == 'Linux'
41-
run: chmod +x gradlew
4252

4353
- name: Build with Gradle
4454
run: ./gradlew assemble --info
@@ -49,6 +59,9 @@ jobs:
4959
name: ${{ matrix.os }} Java ${{ matrix.java }} build results
5060
path: ${{ github.workspace }}/build/libs/
5161

62+
outputs:
63+
project_name: ${{ steps.project_name.outputs.PROJECT_NAME }}
64+
5265
test:
5366
name: Run unit tests
5467
runs-on: ${{ matrix.os }}
@@ -62,6 +75,16 @@ jobs:
6275
- name: Checkout code
6376
uses: actions/checkout@v3
6477

78+
- name: Cache Gradle dependencies
79+
uses: actions/cache@v2
80+
with:
81+
path: |
82+
~/.gradle/caches
83+
~/.gradle/wrapper
84+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
85+
restore-keys: |
86+
${{ runner.os }}-gradle-
87+
6588
- name: Set up JDK
6689
uses: actions/setup-java@v3
6790
with:
@@ -134,18 +157,20 @@ jobs:
134157
- release
135158
# Run if on main or tag
136159
if: always() && (github.ref_name == 'main' || github.ref_type == 'tag')
160+
env:
161+
PROJECT_NAME: ${{ needs.build.outputs.project_name }}
137162
steps:
138163
- name: Set snapshot environment
139164
if: github.ref_name == 'main'
140165
run: |
141166
echo "RELEASE_TYPE=snapshot" >> $GITHUB_ENV
142-
echo "RELEASE_ADDR=https://github.com/CrimsonWarpedcraft/plugin-template/actions/runs/${{ github.run_id }}" >> $GITHUB_ENV
167+
echo "RELEASE_ADDR=https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" >> $GITHUB_ENV
143168
144169
- name: Set release environment
145170
if: github.ref_type == 'tag'
146171
run: |
147172
echo "RELEASE_TYPE=release" >> $GITHUB_ENV
148-
echo "RELEASE_ADDR=https://github.com/CrimsonWarpedcraft/plugin-template/releases/tag/${{ github.ref_name }}" >> $GITHUB_ENV
173+
echo "RELEASE_ADDR=https://github.com/${{ github.repository }}/releases/tag/${{ github.ref_name }}" >> $GITHUB_ENV
149174
150175
- name: Notify on success
151176
if: needs.build.result == 'success' && needs.test.result == 'success' && (needs.release.result == 'success' || github.event_name == 'schedule')
@@ -154,9 +179,9 @@ jobs:
154179
webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
155180
webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
156181
color: "#00FF00"
157-
username: "ExamplePlugin Release Bot"
182+
username: "${{ env.PROJECT_NAME }} Release Bot"
158183
message: >
159-
An ExamplePlugin ${{ env.RELEASE_TYPE }} was deployed:
184+
An ${{ env.PROJECT_NAME }} ${{ env.RELEASE_TYPE }} was deployed by ${{ github.actor }}:
160185
${{ env.RELEASE_ADDR }}
161186
162187
- name: Notify on failure
@@ -166,7 +191,7 @@ jobs:
166191
webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
167192
webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
168193
color: "#FF0000"
169-
username: "ExamplePlugin Release Bot"
194+
username: "${{ env.PROJECT_NAME }} Release Bot"
170195
message: >
171-
An ExamplePlugin ${{ env.RELEASE_TYPE }} failed:
172-
https://github.com/CrimsonWarpedcraft/plugin-template/actions/runs/${{ github.run_id }}
196+
An ${{ env.PROJECT_NAME }} ${{ env.RELEASE_TYPE }} ran by ${{ github.actor }} failed:
197+
https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}

build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,20 @@ shadowJar {
109109
jar.enabled = false
110110
assemble.dependsOn(shadowJar)
111111

112+
tasks.register('printProjectName') {
113+
doLast {
114+
println rootProject.name
115+
}
116+
}
117+
112118
tasks.register('release') {
113119
dependsOn build
114120

115121
doLast {
116122
if (!version.endsWith("-SNAPSHOT")) {
117123
// Rename final JAR to trim off version information
118124
shadowJar.archiveFile.get().getAsFile()
119-
.renameTo(buildDir.toString() + File.separator + 'libs' + File.separator
125+
.renameTo(layout.buildDirectory.toString() + File.separator + 'libs' + File.separator
120126
+ rootProject.name + '.jar')
121127
}
122128
}

0 commit comments

Comments
 (0)