Add deploy gitea task, add some gradle task

This commit is contained in:
Quentin Legot 2024-02-16 21:34:21 +01:00
parent 138cb1a2d0
commit d826d17c73
13 changed files with 237 additions and 32 deletions

View File

@ -0,0 +1,38 @@
name: deploy
description: Deploy artifact to maven repo
on:
push:
branches: [ master ]
env:
REPO_USERNAME: Altarik
REPO_PASSWORD: ${{ secrets.REPO_PASSWORD }}
DISCORD_PUB_ID: ${{ secrets.DISCORD_PUB_ID }}
DISCORD_PUB_TOKEN: ${{ secrets.DISCORD_PUB_TOKEN }}
jobs:
deploy:
runs-on: ubuntu-latest
# if: gitea.ref == 'refs/heads/main'
steps:
- name: checkout repository
uses: actions/checkout@v3
- name: validate gradle wrapper
uses: https://github.com/gradle/wrapper-validation-action@v1
- name: setup jdk
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'oracle'
- name: make gradle wrapper executable
if: ${{ runner.os != 'Windows' }}
run: |
chmod +x ./gradlew
touch local.properties
- name: deploy
run: ./gradlew publish
- name: create tag
run: ./gradlew createTag
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
- name: report to discord
run: ./gradlew reportToDiscord

View File

@ -1,25 +0,0 @@
name: AAAAHHHHHHHHHHHH!!!!!!!!!!
on: [push, pull_request, workflow_dispatch]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
- uses: gradle/wrapper-validation-action@v1
- uses: gradle/gradle-build-action@v2.4.2
with:
arguments: clean build
- uses: actions/upload-artifact@v3
with:
name: AHHHHHHHHHHH!!!!!!!!!!
path: build/libs

3
.gitignore vendored
View File

@ -1,7 +1,7 @@
# Compiled nonsense that does not belong in *source* control
/build
/bin
/.gradle
.gradle
/minecraft
/out
/run
@ -32,5 +32,4 @@ local.properties
*.png~
*-autosave.kra
*.kra~
/gradle/
/src/generated/

View File

@ -1,11 +1,33 @@
import fr.altarik.ReportDiscord
import fr.altarik.CreateTag
plugins {
id 'fabric-loom' version '1.5-SNAPSHOT'
id 'maven-publish'
}
Properties local = new Properties()
try {
local.load(new FileInputStream(rootProject.file("local.properties")))
} catch (IOException ignored) {}
version = project.mod_version
group = project.maven_group
String webhookId = getEnv("DISCORD_PUB_ID", local.getProperty("discord_pub_id"))
String webhookToken = getEnv("DISCORD_PUB_TOKEN", local.getProperty("discord_pub_token"))
String repoUrl = "https://repo.altarik.fr/#/" + (project.version.endsWith('SNAPSHOT') ? 'snapshots/' : 'releases/') + project.group.replace(".", "/") + "/" + project.rootProject.name + "/" + project.version
var reportConfig = new ReportDiscord.ReportData("https://discord.com/api/", webhookId, webhookToken, project.rootProject.name, "Update " + project.version + " has been published", repoUrl)
String giteaToken = getEnv("GITEA_TOKEN", local.getProperty("gitea_token"))
var releaseConfig = new CreateTag.CreateReleaseData("https://git.altarik.fr", project.git_owner, project.git_repo, "v" + project.version as String, giteaToken)
base {
archivesName = project.archives_base_name
}
@ -36,6 +58,16 @@ loom {
}
repositories {
maven {
name 'altarik-snapshots'
url 'https://repo.altarik.fr/snapshots/'
}
maven {
name 'altarik-releases'
url 'https://repo.altarik.fr/releases/'
}
mavenCentral()
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
@ -61,10 +93,7 @@ processResources {
filteringCharset "UTF-8"
filesMatching("fabric.mod.json") {
expand "version": project.version,
"mod_id": mod_id,
"minecraft_version": project.minecraft_version,
"loader_version": project.loader_version
expand inputs.properties
}
}
@ -81,6 +110,9 @@ tasks.withType(JavaCompile).configureEach {
}
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
def javaVersion = JavaVersion.toVersion(targetJavaVersion)
if (JavaVersion.current() < javaVersion) {
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
@ -111,5 +143,32 @@ publishing {
// Notice: This block does NOT have the same function as the block in the top level.
// The repositories here will be used for publishing your artifact, not for
// retrieving dependencies.
maven {
name 'altarik'
url 'https://repo.altarik.fr/'.concat(project.version.endsWith('SNAPSHOT') ? 'snapshots/' : 'releases/')
credentials {
username = getEnv("REPO_USERNAME", local.getProperty("repo_username"))
password = getEnv("REPO_PASSWORD", local.getProperty("repo_password"))
}
}
}
}
tasks.register("reportToDiscord", ReportDiscord) {
config.set(reportConfig)
}
tasks.register("createTag", CreateTag) {
config.set(releaseConfig)
}
static def getEnv(String envName, String defaultValue) {
String r = System.getenv(envName)
if(r != null) {
return r
} else {
return defaultValue
}
}

2
buildSrc/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.gradle
build

11
buildSrc/build.gradle Normal file
View File

@ -0,0 +1,11 @@
plugins {
id 'java'
}
dependencies {
implementation "com.squareup.okhttp3:okhttp:${project.okhttp_version}"
}
repositories {
mavenCentral()
}

View File

@ -0,0 +1 @@
okhttp_version=4.12.0

7
buildSrc/settings.gradle Normal file
View File

@ -0,0 +1,7 @@
pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}
}
rootProject.name = "buildSrc"

View File

@ -0,0 +1,47 @@
package fr.altarik;
import okhttp3.*;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;
import java.io.IOException;
public abstract class CreateTag extends DefaultTask {
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
private final OkHttpClient client = new OkHttpClient();
@Input
public abstract Property<CreateReleaseData> getConfig();
@TaskAction
public void create() throws IOException {
CreateReleaseData data = getConfig().get();
String postUrl = data.baseUrl() + "/api/v1/repos/" + data.owner() + "/" + data.repoName() + "/tags";
RequestBody body = RequestBody.create("""
{
"tag_name": \"""" + data.tagName() + "\"" + """
}
""", JSON);
Request request = new Request.Builder()
.url(postUrl)
.post(body)
.header("Authorization", "token " + data.giteaToken())
.build();
try(Response response = client.newCall(request).execute()) {
if(!response.isSuccessful()) {
if(response.code() != 409)
throw new GradleException("Cannot create tag, server answer with code " + response.code() + " and message : " + response.message());
}
}
}
public record CreateReleaseData(String baseUrl, String owner, String repoName, String tagName, String giteaToken) {
}
}

View File

@ -0,0 +1,56 @@
package fr.altarik;
import okhttp3.*;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;
import java.io.IOException;
public abstract class ReportDiscord extends DefaultTask {
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
@Input
public abstract Property<ReportData> getConfig();
private final OkHttpClient client = new OkHttpClient();
@TaskAction
public void report() throws IOException {
ReportData data = getConfig().get();
String message = data.message();
RequestBody body = RequestBody.create("""
{
"embeds": [
{
"title": "A new update of\s""" + data.projectName() + """
is available",
"description":\s""" + "\"" + message + "\"" + """
,
"url":\s""" + "\"" + data.url() + "\"" + """
}
]
}
""", JSON);
String url = data.baseUrl() + "/webhooks/" + data.webhookId + "/" + data.webhookToken;
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try(Response response = client.newCall(request).execute()) {
getLogger().info("report sent");
if(!response.isSuccessful()) {
throw new GradleException("Discord returned a " + response.code() + " code: " + response.message());
}
}
}
public record ReportData(String baseUrl, String webhookId, String webhookToken, String projectName, String message, String url) {
}
}

View File

@ -15,4 +15,7 @@ mod_id = reframed
# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.95.4+1.20.4
fabric_version=0.95.4+1.20.4
git_owner=Altarik
git_repo=ReFramed

BIN
gradle/wrapper/gradle-wrapper.jar vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists