jenkinscurlyamlgithub-actions

Throwing an Error while triggering the Jenkins build in GitHub Actions


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"

jobs:
  trigger-jenkins:
    runs-on: self-hosted

    steps:
      - name: Trigger Jenkins Build
        shell: bash
        env:
          JENKINS_URL: ${{ secrets.JENKINS_URL || 'http://localhost:8080' }}
          JENKINS_USER: ${{ secrets.JENKINS_USER || 'subbaramaiah_chevuru' }}
          JENKINS_API_TOKEN: ${{ secrets.JENKINS_API_TOKEN }}
          JOB_NAME: ${{ env.JOB_NAME || 'subbu_project' }}
          BUILD_NUMBER: ${{ env.BUILD_NUMBER || '001' }}
          DEPLOY_ENVIRONMENT: development
        run: |
          # Trim trailing slashes from JENKINS_URL
          TRIMMED_JENKINS_URL=$(echo "$JENKINS_URL" | sed 's:/*$::')
          # Clean job name (no need to modify)
          CLEAN_JOB_NAME="$JOB_NAME"
          # Construct the full URL
          FULL_URL="${TRIMMED_JENKINS_URL}/job/${CLEAN_JOB_NAME}/build"
          echo "Full URL: $FULL_URL"
          # Execute the curl command to trigger the Jenkins build
          echo "Executing curl command:"
          curl -v "$FULL_URL" \
               --user "$JENKINS_USER:$JENKINS_API_TOKEN" \
               --header "Content-Type: application/x-www-form-urlencoded" \
               --data-urlencode "BUILD_NUMBER=$BUILD_NUMBER" \
               --data-urlencode "DEPLOY_ENVIRONMENT=$DEPLOY_ENVIRONMENT"

Do you have a Jenkins instance running at localhost:8080 on that self-hosted runner? - Asked by @Azeem

I have installed and started the Jenkins and Action Runner in the PowerShell WSL and below is the output in the GitHub Action [trigger-jenkins][1] :

Full URL: ***/job/subbu_project/build
Executing curl command:
*   Trying 192.168.241.134:8080...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to 192.168.241.134 (192.168.241.134) port 8080 (#0)
* Server auth using Basic with user '***'
> POST /job/subbu_project/build HTTP/1.1
> Host: 192.168.241.134:8080
> Authorization: Basic Y2hldnVydV9zdWJiYXJhbWFpYWg6***
> User-Agent: curl/7.81.0
> Accept: */*
> Content-Type: application/x-www-form-urlencoded
> Content-Length: 47
> 
} [47 bytes data]
* Mark bundle as not supporting multiuse
< HTTP/1.1 401 Unauthorized
< Date: Fri, 30 Aug 2024 16:35:24 GMT
< X-Content-Type-Options: nosniff
< Set-Cookie: remember-me=; Path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0
* Authentication problem. Ignoring this.
< WWW-Authenticate: Basic realm="Jenkins"
< Cache-Control: must-revalidate,no-cache,no-store
< Content-Type: text/html;charset=iso-8859-1
< Content-Length: 473
< Server: Jetty(10.0.20)
< 
{ [473 bytes data]

100   520  100   473  100    47   2126    211 --:--:-- --:--:-- --:--:--  2407
* Connection #0 to host 192.168.241.134 left intact
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 401 Unauthorized</title>
</head>
<body><h2>HTTP ERROR 401 Unauthorized</h2>
<table>
<tr><th>URI:</th><td>/job/subbu_project/build</td></tr>
<tr><th>STATUS:</th><td>401</td></tr>
<tr><th>MESSAGE:</th><td>Unauthorized</td></tr>
<tr><th>SERVLET:</th><td>Stapler</td></tr>
</table>
<hr/><a href="https://eclipse.org/jetty">Powered by Jetty:// 10.0.20</a><hr/>

</body>
</html>
##[debug]Finishing: Trigger Jenkins Build

By executing the command in locally also I am getting the same error as Error :

$ curl -v "http://192.168.241.134:8080/job/subbu_project/build" \
        >                --user "subbaramaiah_chevuru:11514daba6c763a40b32e7123eef7e78f1" \
>                --header "Jenkins-Crumb:3756f301ef6d0c8d22026bf7c6ba198c3e9a71f63ef9366ea7d81d124a85905f" \
>                --header "Content-Type: application/x-www-form-urlencoded" \
>                --data-urlencode "BUILD_NUMBER=001" \
        >                --data-urlencode "DEPLOY_ENVIRONMENT=development"
*   Trying 192.168.241.134:8080...
* Connected to 192.168.241.134 (192.168.241.134) port 8080 (#0)
* Server auth using Basic with user 'subbaramaiah_chevuru'
> POST /job/subbu_project/build HTTP/1.1
> Host: 192.168.241.134:8080
> Authorization: Basic c3ViYmFyYW1haWFoX2NoZXZ1cnU6MTE1MTRkYWJhNmM3NjNhNDBiMzJlNzEyM2VlZjdlNzhmMQ==
> User-Agent: curl/7.81.0
> Accept: */*
> Jenkins-Crumb:3756f301ef6d0c8d22026bf7c6ba198c3e9a71f63ef9366ea7d81d124a85905f
> Content-Type: application/x-www-form-urlencoded
> Content-Length: 47
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Date: Sat, 31 Aug 2024 09:46:22 GMT
< X-Content-Type-Options: nosniff
< Cache-Control: no-cache,no-store,must-revalidate
< Set-Cookie: JSESSIONID.11122400=node01rcar0q76b0tu6bqi1ddd7tff18.node0; Path=/; HttpOnly
< Expires: 0
< Content-Type: text/html;charset=utf-8
< X-Hudson-Theme: default
< Referrer-Policy: same-origin
< Cross-Origin-Opener-Policy: same-origin
< X-Hudson: 1.395
< X-Jenkins: 2.462.1
< X-Jenkins-Session: d1c5dd84
< X-Frame-Options: sameorigin
< X-Instance-Identity: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwqhFMW++j5WAbGASVHTzI/xzPeWWXb/F6xfqeGQJCLFEZq7l0AtoAjiH9I4uPwDK94FtA+9orMy2VbJa/cZELSyZbfNHeeZVXEa4xgzeyLzQD+g1SWNpvvDWqpPbV8BvwWCSA21gZ3TqQ5/oqkcMfR3fmtLKp2aWn8YTxBuxRzirKz6o8VfwsiMDF5oU0bnxsYm4kJ8hdsQTwuvMA7kU5BlotTFA7hT5hbAKocpnBU9Ybi2pL48AwXSktWrBKbNnsUz9KA/gMgmc7OkxiLYjEo6TB2i90XS9f38plNjUuZ7b6D1oXbDj0FRw5SHteIchtwGoLh9bkAYH4FQkocSYMQIDAQAB
< Transfer-Encoding: chunked
< Server: Jetty(10.0.20)
<




  <!DOCTYPE html><html><head resURL="/static/d1c5dd84" data-rooturl="" data-resurl="/static/d1c5dd84" data-extensions-available="true" data-unit-test="false" data-imagesurl="/static/d1c5dd84/images" data-crumb-header="Jenkins-Crumb" data-crumb-value="ccf807f121e6e11d15597f94a9211501049538c9739b6bfe709c228a1d152534">



    <title>Not Found [Jenkins]</title>

Solution

  • To troubleshoot the error, I first tested the curl command, which included the Jenkins credentials (JENKINS_USER, JENKINS_API_TOKEN) and other parameters, using Git Bash on Windows. I then ran the same setup using the Windows Subsystem for Linux (WSL) in PowerShell, where I had installed Git, Jenkins, and the GitHub Actions runners.

    Here, below is the Error 401 Unauthorized I am getting whenever I triggered the Jenkins job using the GitHub Actions.

    Despite multiple checks of the .yml file to verify the curl command, Jenkins credentials, and other services, the Jenkins job was still not being triggered by the GitHub Actions workflow.

    Upon further investigation, I discovered that the issue was due to the Jenkins job not being created or configured beforehand. The curl command was trying to trigger a build for a job that didn’t exist, and as a result, the build couldn’t be triggered.

    To resolve this, I created a new job in Jenkins with the same name, subbu_project. After doing this, I triggered the GitHub Actions workflow again. This time, the workflow successfully connected to Jenkins using the provided credentials and triggered the new build, completing the process.

    Here is the GitHub Action output log after creating 201 Created the job in Jenkins:

    Output for the quick reference:

    Executing curl command:
    < HTTP/1.1 201 Created
    < Date: Sat, 31 Aug 2024 10:46:33 GMT
    < X-Content-Type-Options: nosniff
    < Location: ***/queue/item/6/
    < Content-Length: 0
    < Server: Jetty(10.0.20)