Search code examples
githubdeploymentgithub-actionsversioning

github actions not getting the current git fetch value


I manage my automatic release and deployment processes with Github actions.

In my yml file, things increase by npm version and according to the label in the pull request, respectively. Make a new commit and do the deployment process and complete the actions.

My problem here is my version number in the development branch, for example, I am updating the 1.2.3 patch version. It becomes 1.2.4 and reflects on the development branch, but when deploying, it sends version 1.2.3. How can i solve this problem.

An example from my yml file

name: development deploy

on:
    pull_request:
        types:
            - closed
        branches:
            - development
    push:
        branches:
            - development

jobs:
    automatic-version-update:
        name: Automatic version update
        runs-on: ubuntu-latest
        if: github.event.pull_request.merged == true
        steps:
            - name: Check Out Repository
              uses: actions/checkout@v3
            - name: Setup Node.js
              uses: actions/setup-node@v2
              with:
                  node-version: '16.15'
            - name: Update Version
              run: |
                  git config --local user.email "action@github.com"
                  git config --local user.name "GitHub Action"
                  PR_LABELS=$(jq --raw-output '.pull_request.labels[].name' $GITHUB_EVENT_PATH)

                  if [[ $PR_LABELS == *"minor version"* ]]; then
                    npm version minor -m "Updated to minor version %s"

                  elif [[ $PR_LABELS == *"major version"* ]]; then
                    npm version major -m "Updated to major version %s"

                  elif [[ $PR_LABELS == *"patch version"* ]]; then
                    npm version patch -m "Updated to patch version %s"
                  fi

            - name: Push Changes
              uses: ad-m/github-push-action@master
              with:
                  github_token: ${{ secrets.GITHUB_TOKEN }}
                  branch: ${{ github.event.pull_request.base.ref }}
    build-deploy:
        needs: automatic-version-update
        name: Deployment jobs
        runs-on: ubuntu-latest
        steps:
            - name: Check Out Repository
              uses: actions/checkout@v3
            - name: Install Node.js
              uses: actions/setup-node@v1
              with:
                  node-version: '16.15'
            - name: Install packages
              run: yarn install
            - name: React build
              run: yarn run build:dev
            - name: Deploy to Server
              uses: easingthemes/ssh-deploy@main
              env:
                  SSH_PRIVATE_KEY: ${{ secrets.SSH_KEY }}
                  SOURCE: '.'
                  REMOTE_HOST: ${{ secrets.SERVER_HOST }}
                  REMOTE_USER: ${{ secrets.SERVER_USERNAME }}
                  TARGET: ${{ secrets.SERVER_ROOT_FOLDER }}


Solution

  • TLDR; Checkout the correct branch.

    Microsoft Github Actions just adds a couple layers of abstractions around git(1). When something unexpected happens, double-check the real git(1) workflow is clear (e.g. works locally), then verify the Microsoft Github Action Workflow uses the correct actions with the correct configuration step by step.


    Thank you for sharing your question, you asked:

    When deploying, it sends version 1.2.3. How can i [sic!] solve this problem. [sic!]

    The solution may not be directly visible, so let's uncover what happens here first.

    The commit you're creating is in the git repository at the HEAD commanded per uses: actions/checkout@v3 (Cf. GitHub - jobs : what is : use actions/checkout. IIRC in a pull_request event, this is a merge reference, like as-if already merged (refs/pull/...¹); this is not a branch reference (refs/heads/...).

    Now when you command Microsoft Github Actions to push with the following instructions:

                - name: Push Changes
                  uses: ad-m/github-push-action@master
                  with:
                      github_token: ${{ secrets.GITHUB_TOKEN }}
                      branch: ${{ github.event.pull_request.base.ref }}
    

    You're specifically instructing to not push any committed changes, but the (unchanged) pull request target branch ${{ github.event.pull_request.base.ref }}.


    Now the fix is easy, you may already think so: checkout the correct branch with the ref property:

            steps:
                - name: Check Out Repository
                  uses: actions/checkout@v3
                  with:
                    ref: ${{ github.event.pull_request.base.ref }}
                  
    

    TLDR; Checkout the correct branch.

    Microsoft Github Actions just adds a couple layers of abstractions around git(1). When something unexpected happens, double-check the real git(1) workflow is clear (e.g. like your local testing), then verify the Microsoft Github Action Workflow uses the correct actions with the correct configuration step by step.


    Cf. How to get the current branch within Github Actions?