Skip to content

Commit

Permalink
feat: allow exporting Nexus repository IDs and publishing artifacts t…
Browse files Browse the repository at this point in the history
…o an open Nexus staging repository (#471)

* feat: use an existing staging repository defined as a gradle property, otherwise create a new one

* style: remove unused import

* feat: declare as step output the staging repository id

* docs: document the multi-stage upload feature

* feat: write to file all the staging repository ids

* ci: test if the file containing all the staging repo ids is created when a new staging repo is opened

* docs: update readme according to the new plugin usage

* docs: fix description

* test: test if file exists and match the regex

* test: fix bash quotes and other minor improvements
  • Loading branch information
nicolasfara authored Jan 31, 2023
1 parent 8aac450 commit 5faaaf8
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 7 deletions.
52 changes: 50 additions & 2 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
)
echo "Scripts update line: \"$USES\""
echo "Computed version: \"${USES#*@}\""
echo "::set-output name=version::${USES#*@}"
echo "version=${USES#*@}" >> $GITHUB_OUTPUT
- name: Checkout Alchemist ${{ steps.alchemist.outputs.version }}
uses: actions/[email protected]
with:
Expand Down Expand Up @@ -94,7 +94,7 @@ jobs:
)
echo "Scripts update line: \"$USES\""
echo "Computed version: \"${USES#*@}\""
echo "::set-output name=version::${USES#*@}"
echo "version=${USES#*@}" >> $GITHUB_OUTPUT
- name: Checkout Template-for-Kotlin-Multiplatform-Projects ${{ steps.versiontrick.outputs.version }}
uses: actions/[email protected]
with:
Expand All @@ -121,6 +121,54 @@ jobs:
maven-central-password: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}
signing-key: ${{ secrets.SIGNING_KEY }}
signing-password: ${{ secrets.SIGNING_PASSWORD }}
test-multi-stage-deployment:
runs-on: ubuntu-latest
if: >-
!github.event.repository.fork
&& (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
steps:
- name: Compute the version of the target test project
id: versiontrick
shell: bash
run: |
# Idea: the regex matcher of Renovate keeps this string up to date automatically
# The version is extracted and used to access the correct version of the scripts
USES=$(cat <<TRICK_RENOVATE
- uses: DanySK/[email protected]
TRICK_RENOVATE
)
echo "Scripts update line: \"$USES\""
echo "Computed version: \"${USES#*@}\""
echo "version=${USES#*@}" >> $GITHUB_OUTPUT
- name: Checkout Template-for-Kotlin-Multiplatform-Projects ${{ steps.versiontrick.outputs.version }}
uses: actions/[email protected]
with:
fetch-depth: '0'
path: 'kt-mp-multi-stage'
ref: "${{ steps.versiontrick.outputs.version }}"
repository: 'DanySK/Template-for-Kotlin-Multiplatform-Projects'
submodules: 'recursive'
- name: Checkout publish-on-central
uses: actions/[email protected]
with:
path: 'publish-on-central'
- name: Dry-deploy
uses: DanySK/[email protected]
with:
build-command: true
check-command: true
deploy-command: |
COMMAND='./gradlew --include-build ../publish-on-central createStagingRepositoryOnMavenCentral --parallel'
$(echo "$COMMAND") || $(echo "$COMMAND") || $(echo "$COMMAND")
[[ -e build/staging-repo-ids.properties ]]
[[ "$(wc -l build/staging-repo-ids.properties)" =~ "^1 .*" ]]
[[ "$(cat build/staging-repo-ids.properties)" =~ '^MavenCentral=\w+-\d+$' ]]
working-directory: kt-mp-multi-stage
should-run-codecov: false
should-deploy: true
maven-central-password: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}
signing-key: ${{ secrets.SIGNING_KEY }}
signing-password: ${{ secrets.SIGNING_PASSWORD }}
release:
needs:
- build
Expand Down
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,51 @@ flowchart LR
createStagingRepositoryOnRepositoryName --o uploadPublicationName2ToRepositoryNameNexus
```

## Multi-stage upload

This plugin, during the execution of the `createStagingRepositoryOn[Repo]` task, exports a file in
`build/staging-repo-ids.properties` containing the staging repository ID in the format `[Repo]=<repo-id>`.

This file can be used to export all the repository IDs to the environment, and then use them in other jobs.

An example below shows how to use this feature to upload artifacts to a staging repository from a different job.

```yaml
jobs:
build:
runs-on: ubuntu-latest
outputs:
repositoryId: ${{ steps.createStagingRepository.outputs.MavenCentralStagingRepositoryId }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: 11
- name: Build with Gradle
run: ./gradlew build
- name: Create staging repository
id: createStagingRepository
# This step creates a staging repository on Maven Central and exports the staging repository ID as an output
run: |
./gradlew createStagingRepositoryOnMavenCentral
cat build/staging-repo-ids.properties >> $GITHUB_OUTPUT
release:
needs: build
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: 11
- name: Use staging repository
# Use the staging repository ID exported by the previous job to upload artifacts to the same staging repository
run: ./gradlew -PstagingRepositoryId=${{ needs.build.outputs.MavenCentral }} uploadAllPublicationsToMavenCentralNexus

```

## Usage examples

If you use publish-on-central in your project, please consider providing a pull request with a link to your project:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ private fun Project.configureNexusRepository(repoToConfigure: Repository, nexusU
doLast {
rootProject.warnIfCredentialsAreMissing(repoToConfigure)
nexusClient.nexusClient.repoUrl // triggers the initialization of a repository
// Write the staging repository ID to build/staging-repo-ids.properties file
project.buildDir.resolve("staging-repo-ids.properties").appendText(
"${repoToConfigure.name}=${nexusClient.nexusClient.repoId}" + System.lineSeparator()
)
}
group = PublishingPlugin.PUBLISH_TASK_GROUP
description = "Creates a new Nexus staging repository on ${repoToConfigure.name}."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@ data class NexusStatefulOperation(
* Lazily computed staging repository descriptor.
*/
val stagingRepository: StagingRepositoryDescriptor by lazy {
project.logger.lifecycle("Creating repository for profile id {} on Nexus at {}", stagingProfile, nexusUrl)
client.createStagingRepository(
stagingProfile,
description,
)
project.properties["stagingRepositoryId"]?.let {
project.logger.lifecycle("Using existing staging repository {}", it)
val stagingRepo = client.getStagingRepositoryStateById(it as String)
return@lazy StagingRepositoryDescriptor(project.uri(nexusUrl), stagingRepo.id)
} ?: run {
project.logger.lifecycle("Creating repository for profile id {} on Nexus at {}", stagingProfile, nexusUrl)
client.createStagingRepository(
stagingProfile,
description
)
}
}

/**
Expand Down

0 comments on commit 5faaaf8

Please sign in to comment.