sequence_increment - robjcook/sync GitHub Wiki

pipeline:
  name: Trigger Another Pipeline to Increment SequenceID
  identifier: trigger_pipeline_sequence_increment
  projectIdentifier: your_project_id # Replace with your project ID
  orgIdentifier: your_org_id       # Replace with your organization ID
  tags: {}
  properties:
    ci:
      # Codebase is optional if your script is entirely inline.
      # If you manage the script in Git, configure codebase here.
      codebase:
        connectorRef: YOUR_GIT_CONNECTOR_REF # e.g., github_connector
        repoName: your_repo_name             # e.g., my-automation-scripts
        build: <+input>

  stages:
    - stage:
        name: Increment Target Pipeline SequenceID
        identifier: increment_target_sequence_id
        description: "Executes a curl script to trigger another pipeline multiple times."
        type: CI
        spec:
          # Define the infrastructure where this triggering pipeline will run.
          # This should be where your Delegate is located and has network access to Harness Manager.
          infrastructure:
            type: KubernetesDirect # Or Docker, depending on your Delegate setup
            spec:
              connectorRef: your_k8s_cluster_connector # Or your Docker connector
              namespace: harness-ci

          execution:
            steps:
              - step:
                  type: Run
                  name: Trigger Target Pipeline
                  identifier: trigger_target_pipeline
                  spec:
                    image: curlimages/curl:latest # Use a Docker image that has curl installed
                    shell: Bash
                    # Define environment variables for the script, pulling API key from secret
                    envVariables:
                      HARNESS_API_KEY: <+secrets.getValue("harness_api_key")> # IMPORTANT: Reference your secret
                      HARNESS_BASE_URL: "https://app.harness.io/gateway" # Adjust if you have a self-hosted Harness Manager URL
                      TARGET_ACCOUNT_ID: "your_account_id" # Replace with target pipeline's account ID
                      TARGET_ORG_ID: "your_org_id"         # Replace with target pipeline's org ID
                      TARGET_PROJECT_ID: "your_project_id" # Replace with target pipeline's project ID
                      TARGET_PIPELINE_ID: "your_target_pipeline_identifier" # Replace with the actual ID of Pipeline B
                      NUM_EXECUTIONS: "3" # The number of times to trigger Pipeline B

                    command: |
                      #!/bin/bash
                      echo "Starting to trigger the target pipeline: $TARGET_PIPELINE_ID"
                      echo "Will execute $NUM_EXECUTIONS times."

                      # Construct the common request body JSON for the target pipeline
                      # This assumes the target pipeline doesn't require complex runtime inputs.
                      # If it does, you'd need to construct this JSON more carefully.
                      REQUEST_BODY_JSON="{
                        \"pipelineIdentifier\": \"$TARGET_PIPELINE_ID\",
                        \"accountIdentifier\": \"$TARGET_ACCOUNT_ID\",
                        \"orgIdentifier\": \"$TARGET_ORG_ID\",
                        \"projectIdentifier\": \"$TARGET_PROJECT_ID\",
                        \"input\": {}
                      }"

                      for i in $(seq 1 $NUM_EXECUTIONS); do
                        echo "Triggering target pipeline execution #$i..."
                        curl -X POST \
                          "${HARNESS_BASE_URL}/pipeline/api/pipeline/execute/${TARGET_PIPELINE_ID}" \
                          -H "X-Api-Key: ${HARNESS_API_KEY}" \
                          -H "Content-Type: application/json" \
                          -d "${REQUEST_BODY_JSON}" \
                          --fail --show-error # Added --fail and --show-error for better debugging

                        if [ $? -ne 0 ]; then
                          echo "Error triggering pipeline. Exiting."
                          exit 1
                        fi

                        echo "Trigger request sent for execution #$i."
                        sleep 5 # Add a delay to avoid rate limits and allow Harness to process
                      done

                      echo "Successfully triggered $TARGET_PIPELINE_ID $NUM_EXECUTIONS times."
⚠️ **GitHub.com Fallback** ⚠️