I am trying to fetch the last and latest build numbers or build job ids and save them into the text files like old_build_number.txt contains the last successful build number or id and new_build_number.txt contains the all build Ids as build_number - status
example as given below from the Jenkins after the Build Job successful by using the trigger-jenkins-build.yml
triggered through the GitHub Actions.
And the GitHub URL GitHub Project URL , YAML file trigger-jenkins-build.yml and GitHub Action Results.
trigger-jenkins-build.yml
:
-name: Trigger Jenkins Build
on:
push:
branches:
- "main"
jobs:
trigger-jenkins:
runs-on: self-hosted
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
persist-credentials: false
- 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
id: extract-job-name
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
- name: Fetch Local Build Number
id: fetch-local-build-number
run: |
BUILD_NUMBER_FILE=".github/build_numbers/old_build_number.txt"
echo "Checking for build number file at: $BUILD_NUMBER_FILE"
if [ -f "$BUILD_NUMBER_FILE" ]; then
LOCAL_BUILD_NUMBER=$(cat "$BUILD_NUMBER_FILE")
echo "Found local build number: $LOCAL_BUILD_NUMBER"
else
echo "No build number file found. Initializing build number to 0."
LOCAL_BUILD_NUMBER=0
fi
if [ -z "$LOCAL_BUILD_NUMBER" ] || ! [[ "$LOCAL_BUILD_NUMBER" =~ ^[0-9]+$ ]]; then
LOCAL_BUILD_NUMBER=0
fi
echo "LOCAL_BUILD_NUMBER=${LOCAL_BUILD_NUMBER}" >> $GITHUB_ENV
- name: Fetch Latest Jenkins Build Number
id: fetch-jenkins-build-number
run: |
LATEST_BUILD_NUMBER=$(curl -s -u "${{ secrets.JENKINS_USER }}:${{ secrets.JENKINS_API_TOKEN }}" \
"${{ secrets.JENKINS_URL }}/job/${{ env.JOB_NAME }}/lastBuild/api/json?tree=number" | jq -r '.number')
echo "Jenkins returned latest build number: $LATEST_BUILD_NUMBER"
if [ "$LATEST_BUILD_NUMBER" == "null" ] || [ -z "$LATEST_BUILD_NUMBER" ] || ! [[ "$LATEST_BUILD_NUMBER" =~ ^[0-9]+$ ]]; then
echo "Error: Unable to retrieve Jenkins build number. Exiting."
exit 1
fi
echo "LATEST_BUILD_NUMBER=${LATEST_BUILD_NUMBER}" >> $GITHUB_ENV
- name: Determine New Build Number
id: determine-build-number
run: |
if [ "${{ env.LOCAL_BUILD_NUMBER }}" -ge "${{ env.LATEST_BUILD_NUMBER }}" ]; then
BUILD_NUMBER=$((LOCAL_BUILD_NUMBER + 1))
else
BUILD_NUMBER=$((LATEST_BUILD_NUMBER + 1))
fi
BUILD_NUMBER=$(printf "%02d" "$BUILD_NUMBER")
echo "Determined new build number: $BUILD_NUMBER"
echo "BUILD_NUMBER=${BUILD_NUMBER}" >> $GITHUB_ENV
- name: Trigger Jenkins Build
id: trigger-build
shell: bash
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 }}
DEPLOY_ENVIRONMENT: development
run: |
TRIMMED_JENKINS_URL=$(echo "$JENKINS_URL" | sed 's:/*$::')
CRUMB=$(curl -s -u "$JENKINS_USER:$JENKINS_API_TOKEN" "$TRIMMED_JENKINS_URL/crumbIssuer/api/json" | jq -r '.crumb')
if [ -z "$CRUMB" ]; then
echo "Failed to obtain crumb. Exiting."
exit 1
fi
FULL_URL="${TRIMMED_JENKINS_URL}/job/${JOB_NAME}/build"
echo "Triggering Jenkins build with build number: $BUILD_NUMBER at URL: $FULL_URL"
response=$(curl -s -o /dev/null -w "%{http_code}" "$FULL_URL" \
--user "$JENKINS_USER:$JENKINS_API_TOKEN" \
--header "Jenkins-Crumb:$CRUMB" \
--header "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "BUILD_NUMBER=$BUILD_NUMBER" \
--data-urlencode "DEPLOY_ENVIRONMENT=$DEPLOY_ENVIRONMENT")
if [ "$response" -ne 201 ]; then
echo "Error: Jenkins build trigger failed with response code $response. Exiting."
exit 1
fi
echo "Jenkins build triggered successfully."
- name: Re-fetch Latest Jenkins Build Number & Status
id: refetch-jenkins-build-number-status
run: |
sleep 10 # Wait for Jenkins to update the build number and status
LATEST_BUILD_NUMBER=$(curl -s -u "${{ secrets.JENKINS_USER }}:${{ secrets.JENKINS_API_TOKEN }}" \
"${{ secrets.JENKINS_URL }}/job/${{ env.JOB_NAME }}/lastBuild/api/json?tree=number" | jq -r '.number')
echo "Refetched latest Jenkins build number: $LATEST_BUILD_NUMBER"
if [ "$LATEST_BUILD_NUMBER" == "null" ] || [ -z "$LATEST_BUILD_NUMBER" ] || ! [[ "$LATEST_BUILD_NUMBER" =~ ^[0-9]+$ ]]; then
echo "Error: Unable to retrieve Jenkins build number after triggering. Exiting."
exit 1
fi
BUILD_STATUS=$(curl -s -u "${{ secrets.JENKINS_USER }}:${{ secrets.JENKINS_API_TOKEN }}" \
"${{ secrets.JENKINS_URL }}/job/${{ env.JOB_NAME }}/$LATEST_BUILD_NUMBER/api/json?tree=result" | jq -r '.result')
if [ "$BUILD_STATUS" == "null" ] || [ -z "$BUILD_STATUS" ]; then
BUILD_STATUS="in progress"
elif [ "$BUILD_STATUS" == "SUCCESS" ]; then
BUILD_STATUS="success"
else
BUILD_STATUS="failed"
fi
echo "Refetched build status: $BUILD_STATUS"
echo "LATEST_BUILD_NUMBER=${LATEST_BUILD_NUMBER}" >> $GITHUB_ENV
echo "BUILD_STATUS=${BUILD_STATUS}" >> $GITHUB_ENV
- name: Update Old and New Build Numbers
id: update-build-numbers
run: |
OLD_BUILD_NUMBER_FILE=".github/build_numbers/old_build_number.txt"
NEW_BUILD_NUMBER_FILE=".github/build_numbers/new_build_number.txt"
mkdir -p .github/build_numbers
echo "Updating old build number file at: $OLD_BUILD_NUMBER_FILE"
echo "${{ env.LATEST_BUILD_NUMBER }}" > "$OLD_BUILD_NUMBER_FILE"
echo "Updating new build number file at: $NEW_BUILD_NUMBER_FILE"
echo "${{ env.LATEST_BUILD_NUMBER }} - ${{ env.BUILD_STATUS }}" >> "$NEW_BUILD_NUMBER_FILE"
# Configure Git and commit the updated build numbers
git config user.name "github-actions"
git config user.email "actions@github.com"
git fetch origin || { echo "git fetch failed"; exit 1; }
git reset --hard origin/${GITHUB_REF#refs/heads/} || { echo "git reset failed"; exit 1; }
git add "$OLD_BUILD_NUMBER_FILE" "$NEW_BUILD_NUMBER_FILE"
if ! git diff --cached --quiet; then
git commit -m "Update build numbers to ${{ env.LATEST_BUILD_NUMBER }} and status ${{ env.BUILD_STATUS }}" || { echo "git commit failed"; exit 1; }
git push origin HEAD:${GITHUB_REF#refs/heads/} || { echo "git push failed"; exit 1; }
else
echo "No changes to commit."
fi
- name: Post-job Cleanup
if: always() # Always run this step even if previous steps fail
run: |
echo "Starting post-job cleanup..."
if [ -x "/usr/bin/make" ]; then
echo "Make executable found at /usr/bin/make"
else
echo "Error: Make executable not found at /usr/bin/make. Exiting."
exit 1
fi
echo "Post-job cleanup completed successfully."
And the old_build_number.txt
, new_build_number.txt
files are created under the folder .github/build_numbers
as given the links above.
I am not sure what went wrong to update the text files after the successful Jenkins build job.
I have corrected the above mentioned yaml file trigger-jenkins-build.yml to save the build numbers in the JSON file as build_number.json whenever the Jenkins build job successfully triggered.
This way I solved the issue about saving the old and new build numbers in a single file rather than two separate files, more over it is easy to reading and updating the contents from the same file.
Thanks.