skip to Main Content

I am doing an GitHub Actions workflow that triggers the build in Jenkins. Below are the files are created that triggers the build.

config.json:

{
    "buildConfig": {
        "jobName": "subbu_project"
    }
}

trigger_build.yml in GitHub workflows:

name: Trigger Jenkins Build

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  trigger-jenkins:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Get Commit Message
        id: get-commit-message
        run: |
          COMMIT_MESSAGE=$(git log -1 --pretty=format:%s)
          echo "Commit Message: ${COMMIT_MESSAGE}"
          echo "COMMIT_MESSAGE=${COMMIT_MESSAGE}" >> $GITHUB_ENV

      - name: Read JSON Configuration
        id: read-json
        run: |
          if [ -f config.json ]; then
            JOB_NAME=$(jq -r '.buildConfig.jobName' config.json)
            if [ "$JOB_NAME" == "null" ] || [ -z "$JOB_NAME" ]; then
              echo "Error: 'jobName' not found in config.json"
              exit 1
            fi
            echo "JOB_NAME=${JOB_NAME}" >> $GITHUB_ENV
          else
            echo "Error: config.json file not found"
            exit 1
          fi

      - name: Extract Job Name and Increment Build Number
        id: extract-and-increment
        run: |
          EXTRACTED_JOB_NAME=$(echo "${{ env.COMMIT_MESSAGE }}" | grep -oP '(?<=Activity: )S+' || echo "")
          if [ -z "$EXTRACTED_JOB_NAME" ]; then
            EXTRACTED_JOB_NAME=${{ env.JOB_NAME }}
          fi
          echo "JOB_NAME=${EXTRACTED_JOB_NAME}" >> $GITHUB_ENV

          BUILD_NUMBER_FILE=".github/build-number.txt"
          if [ -f "$BUILD_NUMBER_FILE" ]; then
            BUILD_NUMBER=$(cat "$BUILD_NUMBER_FILE")
          else
            BUILD_NUMBER="00"
          fi
          BUILD_NUMBER=$(printf "%02d" $((10#$BUILD_NUMBER + 1)))
          echo "BUILD_NUMBER=${BUILD_NUMBER}" >> $GITHUB_ENV

          echo "${BUILD_NUMBER}" > "$BUILD_NUMBER_FILE"
          git config user.name "github-actions"
          git config user.email "[email protected]"
          git stash
          git pull --rebase
          git stash pop
          git add "$BUILD_NUMBER_FILE"
          git commit -m "Update build number to ${BUILD_NUMBER}" || true
          git push

      - name: Trigger Jenkins Build
        env:
          JENKINS_URL: ${{ secrets.JENKINS_URL }}
          JENKINS_USER: ${{ secrets.JENKINS_USER }}
          JENKINS_API_TOKEN: ${{ secrets.JENKINS_API_TOKEN }}
          JOB_NAME: ${{ env.JOB_NAME }}
          BUILD_NUMBER: ${{ env.BUILD_NUMBER }}
        run: |
          TRIMMED_JENKINS_URL=$(echo "${{ secrets.JENKINS_URL }}" | sed 's:/*$::')
          CLEAN_JOB_NAME=$(echo "${{ env.JOB_NAME }}" | tr -d 'n' | jq -sRr @uri)
          FULL_URL="${TRIMMED_JENKINS_URL}/job/${CLEAN_JOB_NAME}/buildWithParameters"
          echo "Full URL: ${FULL_URL}"
          curl -v -X POST "${FULL_URL}" 
               --user "${{ secrets.JENKINS_USER }}:${{ secrets.JENKINS_API_TOKEN }}" 
               --data-urlencode "BUILD_NUMBER=${{ env.BUILD_NUMBER }}" 
               --data-urlencode "CUSTOM_PARAM=custom-value"

In the step "Trigger Jenkins Build", I am getting the below Error:

Run TRIMMED_JENKINS_URL=$(echo "***" | sed 's:/*$::')
  TRIMMED_JENKINS_URL=$(echo "***" | sed 's:/*$::')
  CLEAN_JOB_NAME=$(echo "subbu_project" | tr -d 'n' | jq -sRr @uri)
  FULL_URL="${TRIMMED_JENKINS_URL}/job/${CLEAN_JOB_NAME}/buildWithParameters"
  echo "Full URL: ${FULL_URL}"
  curl -v -X POST "${FULL_URL}" 
       --user "***:***" 
       --data-urlencode "BUILD_NUMBER=07" 
       --data-urlencode "CUSTOM_PARAM=custom-value"
  shell: /usr/bin/bash -e {0}
  env:
    COMMIT_MESSAGE: Updated the yml file
    JOB_NAME: subbu_project
    BUILD_NUMBER: 07
    JENKINS_URL: ***
    JENKINS_USER: ***
    JENKINS_API_TOKEN: ***
/home/runner/work/_temp/f4da7773-0a28-410f-92ee-413a3c923494.sh: line 8: unexpected EOF while looking for matching `"'
Error: Process completed with exit code 2.

What and where is the error that causing? I’m not able to find out I have debug and cross verify all the above files. Still I am unable to find out the root cause for this issue.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for commenting the answer. I tried the solution suggested by you as in below

    - name: Trigger Jenkins Build
            env:
              JENKINS_URL: ${{ secrets.JENKINS_URL }}
              JENKINS_USER: ${{ secrets.JENKINS_USER }}
              JENKINS_API_TOKEN: ${{ secrets.JENKINS_API_TOKEN }}
              JOB_NAME: ${{ env.JOB_NAME }}
              BUILD_NUMBER: ${{ env.BUILD_NUMBER }}
            run: |
              TRIMMED_JENKINS_URL=$(echo "${{ secrets.JENKINS_URL }}" | sed 's:/*$::')
              CLEAN_JOB_NAME=$(echo "${{ env.JOB_NAME }}" | tr -d 'n' | jq -sRr @uri)
              FULL_URL="${TRIMMED_JENKINS_URL}/job/${CLEAN_JOB_NAME}/buildWithParameters"
              echo "Full URL: ${FULL_URL}"
              curl -v -X POST '${FULL_URL}' 
                   --user '${{ secrets.JENKINS_USER }}:${{ secrets.JENKINS_API_TOKEN }}' 
                   --data-urlencode 'BUILD_NUMBER=${{ env.BUILD_NUMBER }}' 
                   --data-urlencode 'CUSTOM_PARAM=custom-value'
    
              echo "Jenkins build trigger completed."
            shell: bash
    

    But the same error as below

     shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
      env:
       
        JOB_NAME: subbu_project
        JENKINS_URL: ***
        JENKINS_USER: ***
        JENKINS_API_TOKEN: ***
        BUILD_NUMBER: 
    /home/runner/work/_temp/df7164b2-3289-43c3-95b3-346cdfb2a9c7.sh: line 10: unexpected EOF while looking for matching `"'
    Error: Process completed with exit code 2.
    

  2. Looking at the code, I can infer that the only source of unexpected double quote is ${{ secrets.JENKINS_USER }} or ${{ secrets.JENKINS_API_TOKEN }}

    To support a double quote in either of these values, try enclosing –user argument in single quotes:

    --user '${{ secrets.JENKINS_USER }}:${{ secrets.JENKINS_API_TOKEN }}'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search