diff --git a/.github/workflows/halo.yaml b/.github/workflows/halo.yaml index 660a9a65f..be5e97e41 100644 --- a/.github/workflows/halo.yaml +++ b/.github/workflows/halo.yaml @@ -80,6 +80,32 @@ jobs: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: gh release upload ${{ github.event.release.tag_name }} application/build/libs/* + build-and-publish-container-image-with-buildpacks: + if: always() && (github.event_name == 'push' || github.event_name == 'release') + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Setup Environment + uses: ./.github/actions/setup-env + - name: Reset version of Halo + if: github.event_name == 'release' + shell: bash + run: | + # Set the version with tag name when releasing + version=${{ github.event.release.tag_name }} + version=${version#v} + sed -i "s/version=.*-SNAPSHOT$/version=$version/1" gradle.properties + - name: Publish To Container Registries + env: + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} + DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }} + F2C_USERNAME: ${{ secrets.F2C_REGISTRY_USER }} + F2C_TOKEN: ${{ secrets.F2C_REGISTRY_TOKEN }} + GHCR_USERNAME: ${{ github.repository_owner }} + GHCR_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: + ./gradlew publishToAllRegistries -Prelease=${{ github.event_name == 'release' && 'true' || 'false' }} + docker-build-and-push: if: always() && needs.build.result == 'success' && (github.event_name == 'push' || github.event_name == 'release') runs-on: ubuntu-latest diff --git a/application/build.gradle b/application/build.gradle index 20a341891..46e16a643 100644 --- a/application/build.gradle +++ b/application/build.gradle @@ -1,5 +1,8 @@ import de.undercouch.gradle.tasks.download.Download import org.gradle.crypto.checksum.Checksum +import org.springframework.boot.gradle.tasks.bundling.BootBuildImage +import org.springframework.boot.gradle.tasks.bundling.BootJar +import org.springframework.util.StringUtils plugins { id "checkstyle" @@ -14,6 +17,7 @@ plugins { alias(libs.plugins.checksum) alias(libs.plugins.springdoc.openapi) alias(libs.plugins.versions) + alias(libs.plugins.grgit) } group = 'run.halo.app' @@ -144,10 +148,10 @@ tasks.named('jacocoTestReport', JacocoReport) { } ext.presetPluginUrls = [ - 'https://github.com/halo-dev/plugin-comment-widget/releases/download/v3.0.0/plugin-comment-widget-3.0.0.jar' : 'plugin-comment-widget.jar', - 'https://github.com/halo-dev/plugin-search-widget/releases/download/v1.7.0/plugin-search-widget-1.7.0.jar' : 'plugin-search-widget.jar', - 'https://github.com/halo-dev/plugin-sitemap/releases/download/v1.1.2/plugin-sitemap-1.1.2.jar' : 'plugin-sitemap.jar', - 'https://github.com/halo-dev/plugin-feed/releases/download/v1.5.0/plugin-feed-1.5.0.jar' : 'plugin-feed.jar', + 'https://github.com/halo-dev/plugin-comment-widget/releases/download/v3.0.0/plugin-comment-widget-3.0.0.jar' : 'plugin-comment-widget.jar', + 'https://github.com/halo-dev/plugin-search-widget/releases/download/v1.7.0/plugin-search-widget-1.7.0.jar' : 'plugin-search-widget.jar', + 'https://github.com/halo-dev/plugin-sitemap/releases/download/v1.1.2/plugin-sitemap-1.1.2.jar' : 'plugin-sitemap.jar', + 'https://github.com/halo-dev/plugin-feed/releases/download/v1.5.0/plugin-feed-1.5.0.jar' : 'plugin-feed.jar', // Currently, plugin-app-store is not open source, so we need to download it from the official website. // Please see https://github.com/halo-dev/plugin-app-store/issues/55 @@ -194,3 +198,67 @@ tasks.named('generateOpenApiDocs') { false } } + +def name = 'halo' +def tagName = "${project.version}-cnb" +def isRelease = Objects.equals(project.findProperty('release'), 'true') +if (!isRelease) { + name = 'halo-dev' + tagName = "cnb-sha-${grgit.head().abbreviatedId}" +} + +tasks.register('publishToGhcr', BootBuildImage) { + group = 'publishing' + description = 'Build and publish the Docker image to GitHub Container Registry.' + imageName = "ghcr.io/halo/${name}:${tagName}" + publish = StringUtils.hasText(System.getenv('GHCR_TOKEN')) + if (!isRelease) { + tags = ["ghcr.io/halo/${name}:main".toString()] + } + docker { + publishRegistry { + username = System.getenv('GHCR_USERNAME') + password = System.getenv('GHCR_TOKEN') + } + } + archiveFile = tasks.named('bootJar', BootJar).get().archiveFile +} + +tasks.register('publishToDockerHub', BootBuildImage) { + group = 'publishing' + description = 'Build and publish the Docker image to Docker Hub.' + imageName = "halohub/${name}:${tagName}" + publish = StringUtils.hasText(System.getenv('DOCKER_TOKEN')) + if (!isRelease) { + tags = ["halohub/${name}:main".toString()] + } + docker { + publishRegistry { + username = System.getenv("DOCKER_USERNAME") + password = System.getenv("DOCKER_TOKEN") + } + } + archiveFile = tasks.named('bootJar', BootJar).get().archiveFile +} + +tasks.register('publishToFit2Cloud', BootBuildImage) { + imageName = "registry.fit2cloud.com/${name}:${tagName}" + description = 'Build and publish the Docker image to Fit2Cloud Container Registry.' + publish = StringUtils.hasText(System.getenv('F2C_TOKEN')) + if (!isRelease) { + tags = ["registry.fit2cloud.com/${name}:main".toString()] + } + docker { + publishRegistry { + username = System.getenv("F2C_USERNAME") + password = System.getenv("F2C_TOKEN") + } + } + archiveFile = tasks.named('bootJar', BootJar).get().archiveFile +} + +tasks.register('publishToAllRegistries') { + group = 'publishing' + description = 'Build and publish the Docker image to all configured registries.' + dependsOn tasks.named('publishToGhcr'), tasks.named('publishToDockerHub'), tasks.named('publishToFit2Cloud') +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 09fbb4746..110c00d42 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -49,3 +49,4 @@ node = 'com.github.node-gradle.node:7.1.0' openapi-generator = 'org.openapi.generator:7.12.0' springdoc-openapi = 'org.springdoc.openapi-gradle-plugin:1.9.0' versions = 'com.github.ben-manes.versions:0.52.0' +grgit = 'org.ajoberstar.grgit:5.3.3'