如何在Release构建成功且Jar上传至Artifactory后自动触发Snapshot构建?
Hey there! Let’s walk through how to implement both of your build automation workflows—starting with the basic "trigger Snapshot on Release success" scenario, then moving to the more specific "trigger only after Jar is confirmed in Artifactory" case. I’ll use common CI/CD tools like Jenkins and GitHub Actions since those are the most widely used for this kind of work.
1. Trigger Snapshot Build When Release Build Succeeds
This is a straightforward CI pipeline dependency. The core idea is to add a post-build action to your Release job that fires off the Snapshot job only if the Release build finishes successfully.
Example with Jenkins (Freestyle Job)
- Open your Release job’s configuration
- Scroll to the Post-build Actions section
- Add Build other projects
- Enter the name of your Snapshot build job
- Check the box Trigger only if build is stable
- Save the configuration
Example with Jenkins Pipeline (Groovy)
If you’re using declarative pipelines, add a post block to your Release pipeline:
pipeline { agent any stages { stage('Build & Publish Release') { steps { // Your existing Release build steps (e.g., mvn clean deploy -Prelease) sh './mvnw clean deploy -DskipTests -Prelease' } } } post { success { // Trigger the Snapshot job without waiting for it to finish build job: 'My-Project-Snapshot-Build', wait: false } } }
Example with GitHub Actions
In your Release workflow YAML, add a job that triggers the Snapshot workflow using the repository_dispatch event:
name: Release Build on: push: tags: - 'v*' # Trigger on version tags jobs: release-build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Build & Deploy Release run: ./mvnw clean deploy -DskipTests -Prelease env: ARTIFACTORY_USER: ${{ secrets.ARTIFACTORY_USER }} ARTIFACTORY_PWD: ${{ secrets.ARTIFACTORY_PWD }} - name: Trigger Snapshot Build if: success() uses: peter-evans/repository-dispatch@v3 with: token: ${{ secrets.GITHUB_TOKEN }} event-type: trigger-snapshot-build
Then, update your Snapshot workflow to listen for this event:
name: Snapshot Build on: repository_dispatch: types: [trigger-snapshot-build] jobs: snapshot-build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Build & Deploy Snapshot run: ./mvnw clean deploy -DskipTests -Psnapshot
2. Trigger Snapshot Only After Release Jar is Confirmed in Artifactory
This adds a critical validation step: we need to make sure the Jar actually made it to Artifactory before triggering the Snapshot (since a build can succeed but fail to upload artifacts due to network issues, permissions, etc.).
Example with Jenkins Pipeline
We’ll add a verification stage to check if the artifact exists in Artifactory before triggering the Snapshot:
pipeline { agent any environment { RELEASE_VERSION = sh(script: './mvnw help:evaluate -Dexpression=project.version -q -DforceStdout', returnStdout: true).trim() ARTIFACT_PATH = "com/yourcompany/yourproject/${RELEASE_VERSION}/yourproject-${RELEASE_VERSION}.jar" ARTIFACTORY_URL = "https://your-artifactory-instance.com/artifactory/your-release-repo" } stages { stage('Build & Deploy Release') { steps { sh './mvnw clean deploy -DskipTests -Prelease' } } stage('Verify Artifact in Artifactory') { steps { script { // Check if the Jar exists using curl (authenticate with stored credentials) def artifactCheck = sh( script: "curl -s -u \${ARTIFACTORY_CREDS_USR}:\${ARTIFACTORY_CREDS_PSW} ${ARTIFACTORY_URL}/${ARTIFACT_PATH} -o /dev/null -w '%{http_code}'", returnStdout: true ).trim() if (artifactCheck != '200') { error("Release Jar not found in Artifactory (HTTP status: ${artifactCheck}) — aborting Snapshot trigger") } } } } } post { success { build job: 'My-Project-Snapshot-Build', wait: false } } }
Note: Store your Artifactory credentials as a Jenkins credential (username/password) and reference them via ${ARTIFACTORY_CREDS_USR} and ${ARTIFACTORY_CREDS_PSW}.
Example with GitHub Actions
Add a verification step using curl before triggering the Snapshot:
name: Release Build on: push: tags: - 'v*' jobs: release-build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Get Release Version id: get-version run: echo "VERSION=$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_OUTPUT - name: Build & Deploy Release run: ./mvnw clean deploy -DskipTests -Prelease env: ARTIFACTORY_USER: ${{ secrets.ARTIFACTORY_USER }} ARTIFACTORY_PWD: ${{ secrets.ARTIFACTORY_PWD }} - name: Verify Artifact in Artifactory run: | ARTIFACT_URL="https://your-artifactory-url/artifactory/repo/com/yourcompany/yourproject/${{ steps.get-version.outputs.VERSION }}/yourproject-${{ steps.get-version.outputs.VERSION }}.jar" HTTP_STATUS=$(curl -s -u "${{ secrets.ARTIFACTORY_USER }}:${{ secrets.ARTIFACTORY_PWD }}" -o /dev/null -w "%{http_code}" "$ARTIFACT_URL") if [ "$HTTP_STATUS" != "200" ]; then echo "Error: Artifact not found in Artifactory (HTTP status $HTTP_STATUS)" exit 1 fi - name: Trigger Snapshot Build uses: peter-evans/repository-dispatch@v3 with: token: ${{ secrets.GITHUB_TOKEN }} event-type: trigger-snapshot-build
Key Notes to Avoid Issues
- Prevent Loops: Make sure your Snapshot build job doesn’t trigger the Release job (add safeguards like only triggering Release on tag pushes).
- Credential Security: Never hardcode credentials—use your CI tool’s secret management system (Jenkins credentials, GitHub Secrets, etc.).
- Wait vs. No Wait: Use
wait: falsein Jenkins if you don’t need the Release job to wait for the Snapshot to finish (most cases you won’t).
内容的提问来源于stack exchange,提问作者Sandeep

