Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow exporting Nexus repository IDs and publishing artifacts to an open Nexus staging repository #471

Merged
merged 10 commits into from
Jan 31, 2023
51 changes: 49 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,53 @@ 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 ]]
[[ $(cat build/staging-repo-ids.properties) =~ "^MavenCentral=(.*)$" ]]
DanySK marked this conversation as resolved.
Show resolved Hide resolved
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