From 32d82b1c68d4d914f83dc0aef70e731c53a07cf7 Mon Sep 17 00:00:00 2001 From: Victor Carlos Erenu Date: Tue, 14 Apr 2026 02:46:00 +0700 Subject: [PATCH] Add set_as_main option --- .github/workflows/4_bumper_repository.yml | 6 +- .github/workflows/5_bumper_repository.yml | 169 ++++++++++++++++++++++ tools/repository_bumper.sh | 61 +++++++- 3 files changed, 229 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/5_bumper_repository.yml diff --git a/.github/workflows/4_bumper_repository.yml b/.github/workflows/4_bumper_repository.yml index a215726c..65981a8f 100644 --- a/.github/workflows/4_bumper_repository.yml +++ b/.github/workflows/4_bumper_repository.yml @@ -1,4 +1,4 @@ -name: Repository bumper +name: Repository bumper 4.x run-name: Bump ${{ github.ref_name }} (${{ inputs.id }}) on: @@ -30,8 +30,8 @@ on: jobs: bump: - name: Repository bumper - runs-on: ubuntu-22.04 + name: Repository bumper 4.x + runs-on: ubuntu-24.04 permissions: contents: write pull-requests: write diff --git a/.github/workflows/5_bumper_repository.yml b/.github/workflows/5_bumper_repository.yml new file mode 100644 index 00000000..055edfc1 --- /dev/null +++ b/.github/workflows/5_bumper_repository.yml @@ -0,0 +1,169 @@ +name: Repository bumper 5.x +run-name: Bump ${{ github.ref_name }} (${{ inputs.id }}) + +on: + workflow_dispatch: + inputs: + version: + description: 'Target version (e.g. 1.2.3)' + default: '' + required: false + type: string + stage: + description: 'Version stage (e.g. alpha0)' + default: '' + required: false + type: string + tag: + description: 'Change branches references to tag-like references (e.g. v4.12.0-alpha7)' + default: false + required: false + type: boolean + set_as_main: + description: "Enable main branch mode: bump version values only, keep branch references pointing to main" + required: false + type: boolean + default: false + issue-link: + description: 'Issue link in format https://github.com/wazuh//issues/' + required: true + type: string + id: + description: 'Optional identifier for the run' + required: false + type: string + +jobs: + bump: + name: Repository bumper 5.x + runs-on: ubuntu-24.04 + permissions: + contents: write + pull-requests: write + + env: + CI_COMMIT_AUTHOR: wazuhci + CI_COMMIT_EMAIL: 22834044+wazuhci@users.noreply.github.com + CI_GPG_PRIVATE_KEY: ${{ secrets.CI_WAZUHCI_GPG_PRIVATE }} + GH_TOKEN: ${{ secrets.CI_WAZUHCI_BUMPER_TOKEN }} + BUMP_SCRIPT_PATH: tools/repository_bumper.sh + BUMP_LOG_PATH: tools + + steps: + - name: Dump event payload + run: | + cat $GITHUB_EVENT_PATH | jq '.inputs' + + - name: Set up GPG key + id: signing_setup + run: | + echo "${{ env.CI_GPG_PRIVATE_KEY }}" | gpg --batch --import + KEY_ID=$(gpg --list-secret-keys --with-colons | awk -F: '/^sec/ {print $5; exit}') + echo "gpg_key_id=$KEY_ID" >> $GITHUB_OUTPUT + + - name: Set up git + run: | + git config --global user.name "${{ env.CI_COMMIT_AUTHOR }}" + git config --global user.email "${{ env.CI_COMMIT_EMAIL }}" + git config --global commit.gpgsign true + git config --global user.signingkey "${{ steps.signing_setup.outputs.gpg_key_id }}" + echo "use-agent" >> ~/.gnupg/gpg.conf + echo "pinentry-mode loopback" >> ~/.gnupg/gpg.conf + echo "allow-loopback-pinentry" >> ~/.gnupg/gpg-agent.conf + echo RELOADAGENT | gpg-connect-agent + export DEBIAN_FRONTEND=noninteractive + export GPG_TTY=$(tty) + + - name: Checkout repository + uses: actions/checkout@v6 + with: + # Using workflow-specific GITHUB_TOKEN because currently CI_WAZUHCI_BUMPER_TOKEN + # doesn't have all the necessary permissions + token: ${{ env.GH_TOKEN }} + + - name: Determine branch name and script params + id: vars + env: + VERSION: ${{ inputs.version }} + STAGE: ${{ inputs.stage }} + TAG: ${{ inputs.tag }} + run: | + script_params="" + version=${{ env.VERSION }} + stage=${{ env.STAGE }} + tag=${{ env.TAG }} + set_as_main=${{ inputs.set_as_main }} + + # Both version and stage provided + if [[ -n "$version" && -n "$stage" && "$tag" != "true" ]]; then + script_params="--version ${version} --stage ${stage}" + elif [[ -n "$version" && -n "$stage" && "$tag" == "true" ]]; then + script_params="--version ${version} --stage ${stage} --tag ${tag}" + fi + + if [[ "$set_as_main" == "true" ]]; then + script_params="${script_params} --set-as-main" + fi + + issue_number=$(echo "${{ inputs.issue-link }}" | awk -F'/' '{print $NF}') + BRANCH_NAME="enhancement/wqa${issue_number}-bump-${{ github.ref_name }}" + echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT + echo "script_params=${script_params}" >> $GITHUB_OUTPUT + + - name: Create and switch to bump branch + run: | + git checkout -b ${{ steps.vars.outputs.branch_name }} + + - name: Make version bump changes + run: | + echo "Running bump script" + bash ${{ env.BUMP_SCRIPT_PATH }} ${{ steps.vars.outputs.script_params }} + + - name: Check for file changes + id: changes + run: | + if git diff --quiet && git diff --cached --quiet; then + echo "No changes detected. Exiting cleanly." + echo "has_changes=false" >> $GITHUB_OUTPUT + else + echo "has_changes=true" >> $GITHUB_OUTPUT + fi + + - name: Commit and push changes + if: steps.changes.outputs.has_changes == 'true' + run: | + git add . + git commit -m "feat: bump ${{ github.ref_name }}" + git push origin ${{ steps.vars.outputs.branch_name }} + + - name: Create pull request + id: create_pr + if: steps.changes.outputs.has_changes == 'true' + run: | + gh auth setup-git + PR_URL=$(gh pr create \ + --title "Bump ${{ github.ref_name }} branch" \ + --body "Issue: ${{ inputs.issue-link }}" \ + --base ${{ github.ref_name }} \ + --head ${{ steps.vars.outputs.branch_name }}) + + echo "Pull request created: ${PR_URL}" + echo "pull_request_url=${PR_URL}" >> $GITHUB_OUTPUT + + - name: Merge pull request + if: steps.changes.outputs.has_changes == 'true' + run: | + # Any checks for the PR are bypassed since the branch is expected to be functional (i.e. the bump process does not introduce any bugs) + gh pr merge "${{ steps.create_pr.outputs.pull_request_url }}" --merge --admin + + - name: Show logs + run: | + echo "Bump complete." + if [[ "${{ steps.changes.outputs.has_changes }}" == "true" ]]; then + echo "Branch: ${{ steps.vars.outputs.branch_name }}" + echo "PR: ${{ steps.create_pr.outputs.pull_request_url }}" + else + echo "No changes were made (no-op run)." + fi + echo "Bumper scripts logs:" + cat ${BUMP_LOG_PATH}/repository_bumper*log \ No newline at end of file diff --git a/tools/repository_bumper.sh b/tools/repository_bumper.sh index abf70f42..fc12e75c 100644 --- a/tools/repository_bumper.sh +++ b/tools/repository_bumper.sh @@ -10,7 +10,7 @@ LOG_FILE="${DIR}/tools/repository_bumper_$(date +"%Y-%m-%d_%H-%M-%S-%3N").log" VERSION="" STAGE="" FILES_EDITED=() -FILES_EXCLUDED='--exclude="repository_bumper_*.log" --exclude="CHANGELOG.md" --exclude="repository_bumper.sh" --exclude="*_bumper_repository.yml"' +FILES_EXCLUDED='--exclude="repository_bumper_*.log" --exclude="CHANGELOG.md" --exclude="repository_bumper.sh" --exclude="*_bumper_repository.yml" --exclude="mermaid-init.js" --exclude="mermaid.min.js"' get_old_version_and_stage() { local VERSION_FILE="${DIR}/VERSION.json" @@ -25,7 +25,7 @@ grep_command() { # This function is used to search for a specific string in the specified directory. # It takes two arguments: the string to search for and the directory to search in. # Usage: grep_command - eval grep -Rl "${1}" "${2}" --exclude-dir=".git" $FILES_EXCLUDED "${3}" + eval grep -Rl \"${1}\" \"${2}\" --exclude-dir=".git" $FILES_EXCLUDED "${3}" } update_version_in_files() { @@ -74,6 +74,46 @@ update_stage_in_files() { FILES_EDITED+=("${file}") fi done + + if [ $STAGE != "alpha0" ]; then + version_tag_string="default: 'v${VERSION}'" + files_tag=( $(grep_command "${version_tag_string}" "${DIR}") ) + for file in "${files_tag[@]}"; do + sed -i "s/${version_tag_string}/default: 'v${VERSION}-${STAGE}'/g" "${file}" + if [[ $(git diff --name-only "${file}") ]]; then + FILES_EDITED+=("${file}") + fi + done + + version_number_string="default: '${VERSION}'" + files_version=( $(grep -RlE "default: '[0-9]\.[0-9]+\.[0-9]+'" "${DIR}") ) + for file in "${files_version[@]}"; do + sed -i "s/${version_number_string}/default: 'v${VERSION}-${STAGE}'/g" "${file}" + if [[ $(git diff --name-only "${file}") ]]; then + FILES_EDITED+=("${file}") + fi + done + fi +} + +update_main_in_files() { + set -x + if [[ $STAGE == "alpha0" ]]; then + bump_string="default: '${VERSION}'" + else + bump_string="default: 'v${VERSION}'" + fi + main_string="default: 'main'" + files=( $(grep_command "${main_string}" "${DIR}") ) + for file in "${files[@]}"; do + if [[ "$skip_urls" != "yes" ]]; then + sed -Ei "s/${main_string}/${bump_string}/g" ${file} + fi + if [[ $(git diff --name-only "${file}") ]]; then + FILES_EDITED+=("${file}") + fi + done + set +x } update_docker_images_tag() { @@ -106,6 +146,10 @@ main() { TAG="$2" shift 2 ;; + --set-as-main) + set_as_main="yes" + shift 1 + ;; *) echo "Unknown argument: $1" exit 1 @@ -137,6 +181,15 @@ main() { exit 1 fi + # Set skip_urls variable based on set_as_main flag + if [[ -n "$set_as_main" ]]; then + skip_urls="yes" + else + skip_urls="no" + fi + echo "Updating version from main to $VERSION" | tee -a "${LOG_FILE}" + update_main_in_files "$VERSION" "$STAGE" + # Validate if tag is true or false if [[ -n "${TAG}" && ! "${TAG}" =~ ^(true|false)$ ]]; then echo "Error: --tag must be either true or false." | tee -a "${LOG_FILE}" @@ -150,9 +203,9 @@ main() { echo "Updating version from ${OLD_VERSION} to ${VERSION}" | tee -a "${LOG_FILE}" update_version_in_files "${VERSION}" fi - if [[ "${OLD_STAGE}" != "${STAGE}" ]]; then + if [[ -n "$STAGE" ]]; then echo "Updating stage from ${OLD_STAGE} to ${STAGE}" | tee -a "${LOG_FILE}" - update_stage_in_files "${STAGE}" + update_stage_in_files "$VERSION" "$STAGE" fi # Update Docker images tag if tag is true