skip to Main Content

I’m attempting to get the commit message from the merge of the DEV to MAIN branch, so I can insert in the body of the release.

I attempted using get-commits but did not work.

This is the commit message I mean:

enter image description here

This is my YAML file:

name: "Build Android app"

on:
  push:
    branches: ['main', 'dev']

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: 🗂️ Checkout repository
        uses: actions/checkout@v2

      - name: ☕ Instalando Java
        uses: actions/setup-java@v3
        with:
          java-version: 17
          distribution: adopt
          cache: gradle

      - name: 🔍 Validando Gradle
        uses: gradle/wrapper-validation-action@v1

      - name: 🖥️ Instalando Node  
        uses: actions/setup-node@v3
        with:
          node-version: '20'
          cache: 'yarn'

      - name: 📦 Instalando dependências
        run: yarn install

      - name: 🩹 Patch dependências
        run: yarn patch-package

      - name: 🔑 [DEV] Configura variáveis de ambiente de development
        if: github.ref == 'refs/heads/dev'
        run: |
          echo "BASE_URL=${{ secrets.BASE_URL_DEV }}" >> .env.development
          echo "BUILD_VARIANT=DEV" >> .env.development
          echo "KEYSTORE_PASSWORD=${{ secrets.KEYSTORE_PASSWORD }}" >> .env.development
          echo "KEYSTORE_ALIAS=${{ secrets.KEYSTORE_ALIAS }}" >> .env.development

      - name: 🔑 [PROD] Configura variáveis de ambiente de production
        if: github.ref == 'refs/heads/main'
        run: |
          echo "BASE_URL=${{ secrets.BASE_URL_PROD }}" >> .env.production
          echo "BUILD_VARIANT=PROD" >> .env.production
          echo "KEYSTORE_PASSWORD=${{ secrets.KEYSTORE_PASSWORD }}" >> .env.production
          echo "KEYSTORE_ALIAS=${{ secrets.KEYSTORE_ALIAS }}" >> .env.production

      - name: 🔓 Decodificar Keystore
        run: |
          echo $KEYSTORE_BASE_64 | base64 --decode > android/app/keystore.jks
        env:
          KEYSTORE_BASE_64: ${{ secrets.KEYSTORE_BASE_64 }}

      - name: 🏗️ [DEV] Buildando aplicação development
        if: github.ref == 'refs/heads/dev'
        run: |
             cd android
             ./gradlew assembleDevelopmentRelease

      - name: 🏗️ [PROD] Buildando aplicação production
        if: github.ref == 'refs/heads/main'
        run: |
             cd android
             ./gradlew assembleProductionRelease      

      - name: 📦 [PROD] Gerando AAB production
        if: github.ref == 'refs/heads/main'
        run: |
             cd android
             ./gradlew bundleProductionRelease       

      - name: ⬆️ [DEV] Upload development APK artifact
        if: github.ref == 'refs/heads/dev'
        uses: actions/upload-artifact@v4
        with:
          name: android-development-apk
          path: android/app/build/outputs/apk/development/release/app-development-release.apk

      - name: ⬆️ [PROD] Upload production APK/AAB artifact
        if: github.ref == 'refs/heads/main'        
        run: |
          mkdir -p android-prod-bundle
          cp android/app/build/outputs/apk/production/release/app-production-release.apk android-prod-bundle/
          cp android/app/build/outputs/bundle/productionRelease/app-production-release.aab android-prod-bundle/
          zip -r android-prod-bundle.zip android-prod-bundle/
      
      - name: 📝 [PROD] Pegar versão do package.json
        id: package-version
        if: github.ref == 'refs/heads/main'
        uses: martinbeentjes/[email protected]     

      - name: Pegar commits de Dev para Main
        id: get-commits
        uses: actions/github-script@v6
        with:
          script: |
            const commits = await github.repos.compareCommits({
              owner: context.repo.owner,
              repo: context.repo.repo,
              base: 'dev',
              head: 'main'
            });
            const commitMessages = commits.data.commits.map(commit => `- ${commit.commit.message}`).join('n');
            return { commits: commitMessages };

      - name: 🏷️ [PROD] Criar release se for branch main
        id: create-release
        if: github.ref == 'refs/heads/main'
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: v${{ steps.package-version.outputs.current-version }}
          release_name: ${{ steps.package-version.outputs.current-version }}
          body: |
            Changes in this release:
            ${{ steps.get-commits.outputs.commits }}
          draft: false
          prerelease: false   

      - name: ⬆️ [PROD] Upload APK asset para release
        id: upload-release-asset 
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create-release.outputs.upload_url }} 
          asset_path: ./bundle.zip
          asset_name: bundle.zip
          asset_content_type: application/zip

2

Answers


  1. Chosen as BEST ANSWER

    Yeah, so basically I wanted the commit message from dev to main to be content from the release. I could manage to do that with:

    - name: 📝 Pegar mensagem commit do merge
            if: github.ref == 'refs/heads/main'                        
            env:
              GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
            run: |
                response=$(curl -s -X POST 
                  -H "Authorization: bearer $GITHUB_TOKEN" 
                  -H "Content-Type: application/json" 
                  -d '{"query": "query { repository(owner: "repo", name: "repo-app") { pullRequest(number: ${{ steps.last_pr.outputs.last_pr_number }}) { body } } }"}' 
                  https://api.github.com/graphql)
    
                # Extract the body content using jq and store it in an environment variable
                body_content=$(echo "$response" | jq -r '.data.repository.pullRequest.body')
    
                echo "PR body content: ${{ steps.last_pr.outputs.last_pr_number }} $body_content"
    
                # Workaround for multi-line PR body content
                # Use printf with multiline input to preserve newlines
                echo "commit_message<<EOF" >> $GITHUB_ENV
                printf "%sn" "$body_content" >> $GITHUB_ENV
                echo "EOF" >> $GITHUB_ENV
    

  2. For what you’re trying to achieve, an approach that has worked for me in the past was using the GitHub API to fetch the commits and grabbing the message.

    So I’ll update your YAML file to capture the latest commit message after a merge like this:

    
    name: "Build Android app"
    
    on:
      push:
        branches: ['main', 'dev']
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - name: 🗂️ Checkout repository
            uses: actions/checkout@v2
    
          - name: ☕ Instalando Java
            uses: actions/setup-java@v3
            with:
              java-version: 17
              distribution: adopt
              cache: gradle
    
          - name: 🔍 Validando Gradle
            uses: gradle/wrapper-validation-action@v1
    
          - name: 🖥️ Instalando Node  
            uses: actions/setup-node@v3
            with:
              node-version: '20'
              cache: 'yarn'
    
          - name: 📦 Instalando dependências
            run: yarn install
    
          - name: 🩹 Patch dependências
            run: yarn patch-package
    
          - name: Get commit message from merge
            id: commit_message
            run: |
              COMMIT_MSG=$(git log -1 --pretty=%B)
              echo "commit_message=$COMMIT_MSG" >> $GITHUB_ENV
    
          - name: Use commit message in release
            run: |
              echo "Creating release with commit message: ${{ env.commit_message }}"
    
    #continue with your config
    
    

    The new step I added, Get commit message from merge runs a git log command to extract the latest commit message and stores it in a GitHub environment variable (commit_message).

    Then the environment variable is made available for other steps(such as creating a release or using it in other parts of your workflow).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search