skip to Main Content

My main idea is that every time I make a commit it looks to see if the phrase NEW_VERSION:5.0.01 (5.0.01 is just an example in this case) exists somewhere in the commit message and replaces it with a constant with this new version.
Here is my code in .yml:

name: Update Version

on:
  push:
    branches:
      - main

jobs:
  update-version:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Configure SSH
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan github.com >> ~/.ssh/known_hosts
          git config --global user.email "[email protected]"
          git config --global user.name "joaovitorkc"

      - name: Start SSH agent
        run: eval `ssh-agent -s`

      - name: Install Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'

      - name: Get last commit message
        id: last_commit
        run: echo "::set-output name=message::$(git log -1 --pretty=%B)"

      - name: Extract version from commit message
        id: extract_version
        run: echo "::set-output name=version::$(echo ${{ steps.last_commit.outputs.message }} | grep -oP 'NEW_VERSION:Kd+.d+.d+')"

      - name: Update version constant in code
        run: |
          if [ -n "${{ steps.extract_version.outputs.version }}" ]; then
            sed -i "s/const VERSION = ".*";/const VERSION = "${{ steps.extract_version.outputs.version }}";/" src/libs/version.ts
            echo "Version updated to ${{ steps.extract_version.outputs.version }}"
          else
            echo "No new version found in commit message."
          fi

      - name: Commit and push updated version
        env:
          SSH_AUTH_SOCK: /tmp/ssh_agent.sock
        run: |
          ssh-add ~/.ssh/id_rsa
          git commit -am "Update version to ${{ steps.extract_version.outputs.version }}"
          git push

But when this code is run in github actions it gives the following error:

Image with the error

Error details

2

Answers


  1. Chosen as BEST ANSWER

    I made the change mentioned by Delta George, my code looked like this:

    name: Update Version
    
    on:
      push:
        branches:
          - main
    
    jobs:
      update-version:
        runs-on: ubuntu-latest
    
        steps:
          - name: Checkout repository
            uses: actions/checkout@v2
            with:
              fetch-depth: 0
    
          - name: Configure SSH
            run: |
              mkdir -p ~/.ssh
              echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
              chmod 600 ~/.ssh/id_rsa
              ssh-keyscan github.com >> ~/.ssh/known_hosts
              git config --global user.email "[email protected]"
              git config --global user.name "joaovitorkc"
    
          - name: Start SSH agent
            run: eval `ssh-agent -s`
    
          - name: Install Node.js
            uses: actions/setup-node@v2
            with:
              node-version: '14'
    
          - name: Get last commit message
            id: last_commit
            run: echo "::set-output name=message::$(git log -1 --pretty=%B)"
    
          - name: Extract version from commit message
            id: extract_version
            run: echo "::set-output name=version::$(echo ${{ steps.last_commit.outputs.message }} | grep -oP 'NEW_VERSION:Kd+.d+.d+')"
    
          - name: Update version constant in code
            run: |
              if [ -n "${{ steps.extract_version.outputs.version }}" ]; then
                echo "export const VERSION = '${{ steps.extract_version.outputs.version }}';" > src/libs/version.ts
                echo "Version updated to ${{ steps.extract_version.outputs.version }}"
              else
                echo "No new version found in commit message."
              fi
    
          - name: Commit and push updated version
            run: |
              git commit -am "Update version to ${{ steps.extract_version.outputs.version }}"
              git push 
    

    He managed to identify the commit with the message, update the file with the constant, but when it was time to commit, apparently the repository returned that it did not have permissions.

    Error details


  2. You do not need to configure SSH. You checked out your repo using HTTPS. Try this:

         run: |
          git commit -am "Update version to ${{ steps.extract_version.outputs.version }}"
          git push
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search