Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Committing and pushing a workflow within the runner is not triggering a new workflow on another branch. #32615

Open
renatohberg opened this issue Nov 22, 2024 · 9 comments
Labels
topic/gitea-actions related to the actions of Gitea type/bug

Comments

@renatohberg
Copy link

Description

I have a release process in which the code merged via pull request to the "develop" or "hotfix" branch will perform all the testing and build steps, and if everything goes well, it will create a new branch of the "release/" type with the correct version in some files.
Up to this point, my workflow is working correctly, but when I push the new "release/
" branch, it is being created but is not starting the workflow automatically.

Gitea Version

1.22.3

Can you reproduce the bug on the Gitea demo site?

Yes

Log Gist

No response

Screenshots

No response

Git Version

No response

Operating System

Debian 12

How are you running Gitea?

I'm running Gitea on my Virtual Machine with Debian 12, and the Runners on Docker.

Database

PostgreSQL

@lunny
Copy link
Member

lunny commented Nov 22, 2024

How did you push the branch? You need another token to do that. Action user cannot trigger a new workflow which is by design to avoid recycle triggering.

@kemzeb kemzeb added the topic/gitea-actions related to the actions of Gitea label Nov 23, 2024
@renatohberg
Copy link
Author

renatohberg commented Nov 23, 2024

After you commented on the user, I made some changes to see if it would solve the problem. Here are the changes I made in the Gitea action

  1. I changed the email and name of the user before the commit.

git config --global user.name "myotheruser"
git config --global user.email "[email protected]"

  1. I created the new release branch

git checkout -b release/1.25.0

  1. I committed the changes

git commit -m "Automatic Release Commit"

  1. I changed the remote URL of the repository, to force the push with another user.

git remote set-url origin http://myotheruser:[email protected]/myfirstuser/java-spring-template.git

  1. I pushed

git push --set-upstream origin

Even forcing the push with another user, the workflow is not being started on the new branch.

@renatohberg
Copy link
Author

image

@lunny
Copy link
Member

lunny commented Nov 23, 2024

Can you also post your workflow in the default branch?

@renatohberg
Copy link
Author

renatohberg commented Nov 23, 2024

Sure.

name: "Java Pipeline"

on:
  create:
    branches:
      - '*'
  push:
    branches:
      - '*'

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
      - name: "Repository checkout"
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: "Extract Java version"
        run: |
          echo "JAVA_VERSION=21" >> $GITEA_ENV

      - name: "Set up JDK"
        uses: actions/setup-java@v4
        with:
          java-version: '${{ env.JAVA_VERSION }}'
          distribution: 'temurin'

      - name: Run Load Change Version
        if: github.ref_name != 'main'
        uses: http://${{ vars.OWNER_USERNAME }}:${{ secrets.OWNER_TOKEN }}@gitea.myDomain.com/MyDomain-Actions/load-change-version-action@main
        with:
          user: "${{ vars.OWNER_USERNAME }}"
          token: "${{ secrets.OWNER_TOKEN }}"
          repository: "${{ github.event.repository.name }}"
          branch: "${{ github.ref_name }}"
          language: "java"

      - name: "Run unit tests"
        run: |
          chmod +x ./gradlew
          ./gradlew test

      # - name: "Dependency check"
        # env:
          # NVD_API_KEY: ${{ secrets.NVD_API_KEY }}
        # run: |
          # ./gradlew dependencyCheckAnalyze


      - name: "Sonarqube scan"
        uses: http://${{ vars.OWNER_USERNAME }}:${{ secrets.OWNER_TOKEN }}@gitea.myDomain.com/MyDomain-Actions/sonar-action@main
        with:
          engine: "gradle"
          token: ${{ secrets.SONAR_TOKEN }}

      - name: "Zap scan"
        run: |
          echo "Executando ação para qualquer branch!"

      - name: "Build"
        run: "./gradlew build"


      - name: Run Release
        if: github.ref_name == 'develop' || github.ref_name == 'hotfix' || startsWith(github.ref_name, 'release/')
        uses: http://${{ vars.OWNER_USERNAME }}:${{ secrets.OWNER_TOKEN }}@gitea.myDomain.com/MyDomain-Actions/release-action@main
        with:
          user: "${{ vars.OWNER_USERNAME }}"
          email: "${{ vars.OWNER_EMAIL }}"
          token: "${{ secrets.OWNER_TOKEN }}"
          repository-owner: "MyDomain"
          repository: "${{ github.event.repository.name }}"
          branch: "${{ github.ref_name }}"
          current-version: "${{ env.CURRENT_VERSION }}"
          new-version: "${{ env.NEW_VERSION }}"

      - name: "Publish Maven Image"
        if: github.ref_name != 'main' && github.ref_name != 'develop' && github.ref_name != 'hotfix'
        run: |
          echo "Executando ação para qualquer branch!"

      - name: "Build Docker Image"
        if: github.ref_name != 'main' && github.ref_name != 'develop' && github.ref_name != 'hotfix'
        run: |
          echo "Executando ação para qualquer branch!"

      - name: "Publish Docker Image"
        if: github.ref_name != 'main' && github.ref_name != 'develop' && github.ref_name != 'hotfix'
        run: |
          echo "Executando ação para qualquer branch!"

  deploy:
    needs: build
    runs-on: ubuntu-latest
    
    steps:
      - name: "Deploy image"
        run: |
          echo "Type: k8s"
          echo "Image: ---"

@renatohberg
Copy link
Author

The "Run Release" task is a composite action that basically executes the script below.

#!/bin/bash

WORKSPACE_PATH="/workspace/MyDomain/release-action"

source ${WORKSPACE_PATH}/integrations/utils/logging.sh

git_config() {
    git config --global user.name "myanotheruser"
    git config --global user.email "[email protected]"
}

git_checkout_release() {
    NEW_RELEASE_BRANCH="release/${NEW_VERSION}"

    # Create a new Branch release/
    git checkout -b ${NEW_RELEASE_BRANCH}
}

git_commit_with_release_tag() {
    # Commit files
    git commit -m "Automatic Release Commit"

	# git remote set-url origin ${_GIT_SSH_ORIGIN}
	git remote set-url origin http://myanotheruser:[email protected]/MyDomain/java-spring-template.git

    # Push commit to origin
    git push --set-upstream origin ${NEW_RELEASE_BRANCH}
    # git push -u origin ${NEW_RELEASE_BRANCH}
}

main() {
    if [[ "${BRANCH}" == "develop" ]] || [[ "${BRANCH}" == "hotfix" ]]; then
        git_config "${OWNER_USERNAME}" "${OWNER_EMAIL}"

        git_checkout_release

        git_commit_with_release_tag
    else
        utils_logging_warning "Skip release generation"
    fi
}

main

@lunny
Copy link
Member

lunny commented Nov 23, 2024

composite action hasn't been supported yet.

@renatohberg
Copy link
Author

renatohberg commented Nov 23, 2024

I created a workflow without composite actions, and the problem persists. I will post the workflow code below.
To test this workflow you need to push to the develop branch.

name: "Java Pipeline"

on:
  create:
    branches:
      - '*'
  push:
    branches:
      - '*'

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
      - name: "Repository checkout"
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: "Extract Java version"
        run: |
          echo "JAVA_VERSION=21" >> $GITEA_ENV

      - name: "Set up JDK"
        uses: actions/setup-java@v4
        with:
          java-version: '${{ env.JAVA_VERSION }}'
          distribution: 'temurin'

      - name: "Grant permission to gradle"
        run: |
          chmod +x ./gradlew

      - name: "Load Change Version"
        env:
          BRANCH: "${{ github.ref_name }}"
        run: |
          PATTERN_FROM_GRADLE="^([[:blank:]]+)?version([[:blank:]]+)?=([[:blank:]]+)?[\'\"][0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+\.[0-9a-zA-Z]+[-]{1}[0-9a-zA-Z]+)?(-SNAPSHOT)?[\'\"]([[:blank:]]+)?$"

          get_current_version() {
            if [[ -f ./build.gradle ]]; then
              CURRENT_VERSION_FULL="$(cat ./build.gradle | grep -E "${PATTERN_FROM_GRADLE}")"
              CURRENT_VERSION_FULL="$(echo ${CURRENT_VERSION_FULL} | grep -Eo "[\'\"]{1}.*[\'\"]{1}")"
              CURRENT_VERSION_FULL=${CURRENT_VERSION_FULL:1:-1}

              if [[ ${BRANCH} == "develop" ]] || [[ ${BRANCH} == "hotfix" ]]; then
                # Load version from TAG

                CURRENT_VERSION_FULL_FROM_TAG=$(git tag -l --sort -version:refname | head -n 1)
                if [[ -z ${CURRENT_VERSION_FULL_FROM_TAG} ]]; then
                  CURRENT_VERSION="0.0.0"
                else
                  CURRENT_VERSION=${CURRENT_VERSION_FULL_FROM_TAG}
                fi
                
                echo "CURRENT_VERSION_FULL_FROM_TAG: ${CURRENT_VERSION_FULL_FROM_TAG}"
                echo "CURRENT_VERSION: ${CURRENT_VERSION}"
              elif [[ ${BRANCH} == release/* ]]; then
                CHECKER=$(echo "${CURRENT_VERSION_FULL}" | awk -F'-SNAPSHOT' '{print NF-1}')

                if [  ${CHECKER} -eq 1 ]; then
                  CURRENT_VERSION="$(echo ${CURRENT_VERSION_FULL} | sed "s/-SNAPSHOT//")"
                else
                  CURRENT_VERSION=${CURRENT_VERSION_FULL}
                fi
                
                echo "CURRENT_VERSION: ${CURRENT_VERSION}"
              fi
            else
                echo "Not supported"
            fi
          }

          define_new_version() {
            if [[ ${BRANCH} == "develop" ]]; then
              MAJOR=$(echo ${CURRENT_VERSION} | cut -d'.' -f 1)
              MINOR=$(echo ${CURRENT_VERSION} | cut -d'.' -f 2)

              if [[ ${MAJOR} -eq 0 ]]; then
                NEW_VERSION="1.0.0"
              else
                NEW_VERSION="${MAJOR}.$((MINOR+1)).0"
              fi
            elif [[ ${BRANCH} == "hotfix" ]]; then
                MAJOR=$(echo ${CURRENT_VERSION} | cut -d'.' -f 1)
                MINOR=$(echo ${CURRENT_VERSION} | cut -d'.' -f 2)
                PATCH=$(echo ${CURRENT_VERSION} | cut -d'.' -f 3)

                if [[ ${MAJOR} -eq 0 ]]; then
                  NEW_VERSION="1.0.1"
                else
                  NEW_VERSION="${MAJOR}.${MINOR}.$((PATCH+1))"
                fi
            else
              MAJOR=$(echo ${CURRENT_VERSION} | cut -d'.' -f 1)
              MINOR=$(echo ${CURRENT_VERSION} | cut -d'.' -f 2)
              PATCH=$(echo ${CURRENT_VERSION} | cut -d'.' -f 3)

              NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}.${TICKET_NUMBER}-SNAPSHOT"
            fi

            echo "NEW_VERSION: ${NEW_VERSION}"
          }

          # $1: Path                                          REQUIRED
          # $2: File Name with Extension                      REQUIRED
          update_gradle_file() {
            echo "Update Gradle File"

            CURRENT_VERSION_LINE_NUMBER=$(grep -En "${PATTERN_FROM_GRADLE}" $1$2 | cut -d':' -f 1)

            # sed -i "${CURRENT_VERSION_LINE_NUMBER}s/${CURRENT_VERSION_FULL}/${NEW_VERSION}/g" $1$2
            sed -i "${CURRENT_VERSION_LINE_NUMBER}s|${CURRENT_VERSION_FULL}|${NEW_VERSION}|g" $1$2

            if [[ ${BRANCH} == "develop" ]] || [[ ${BRANCH} == "hotfix" ]]; then
              git add $1$2
            fi
          }

          update_application_file() {
            echo "Update Application File"

            # sed -i "s/${CURRENT_VERSION_FULL}/${NEW_VERSION}/g" $1$2
            sed -i "s|${CURRENT_VERSION_FULL}|${NEW_VERSION}|g" $1$2

            if [[ ${BRANCH} == "develop" ]] || [[ ${BRANCH} == "hotfix" ]]; then
                git add $1$2
            fi
          }

          if [[ "${BRANCH}" != "main" && ${BRANCH} != release/* ]]; then
            get_current_version
            define_new_version

            # Update Version
            if [[ -f ./build.gradle ]]; then
                update_gradle_file "./" "build.gradle"
            fi

            if [[ -f ./src/main/resources/application.yml ]]; then
                update_application_file "./src/main/resources/" "application.yml"
            fi

            echo "NEW_VERSION=${NEW_VERSION}" >> $GITEA_ENV
            echo "CURRENT_VERSION=${CURRENT_VERSION}" >> $GITEA_ENV
          elif [[ ${BRANCH} == release/* ]]; then
            get_current_version

            echo "CURRENT_VERSION=${CURRENT_VERSION}" >> $GITEA_ENV
          fi
        
      - name: "Build"
        run: "./gradlew build"

      - name: "Release"
        run: |
          git config --global user.name "mySecondUser"
          git config --global user.email "[email protected]"

          git checkout -b release/${NEW_VERSION}

          git commit -m "Automatic Release Commit"

          git remote set-url origin http://mySecondUser:[email protected]/MyCompany/java-spring-template.git

          git push --set-upstream origin release/${NEW_VERSION}

  deploy:
    needs: build
    runs-on: ubuntu-latest
    
    steps:
      - name: "Deploy image"
        run: |
          echo "Type: k8s"
          echo "Image: ---"

@renatohberg
Copy link
Author

I even managed to do the process via Gitea API using the call to "Modify multiple files in a repository", but it didn't work through commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic/gitea-actions related to the actions of Gitea type/bug
Projects
None yet
Development

No branches or pull requests

3 participants