devmasx / merge-branch

A GitHub Action that merge PR branch to other branchs
161 stars 58 forks source link

[Question] How do it works the env variable GITHUB_SHA? #3

Closed Hatzelencio closed 3 years ago

Hatzelencio commented 4 years ago

Hello there!

A few days ago, i started a personal project with github actions (because it's cool). After that, i wanted create a little post deploy routine (staging->master, master->develop, develop->staging), and i found this cool repo to do the job. But, i dont know if i use it correctly, because the env variable GITHUB_SHA dont be overwrited inside the container. I triying this snipet:

---
jobs:
  sync:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2
        with:
          ref: master
      - name: Sync
        uses: devmasx/merge-branch@v1.1.0
        with:
          type: now
          target_branch: 'my_target_branch'
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
          GITHUB_SHA: 'my_head_branch'

I've tried with other custom env variables. and they are overwrited. Is it possible to create an optional new input heads_branch?

If you wish, i can help you :)

MiguelSavignano commented 4 years ago

Hello, Github action set that env variable. default-environment-variables.

GITHUB_SHA: The commit SHA that triggered the workflow. For example, ffac537e6cbbf934b08745a378932722df287a53.

The correct use for "merge type: now" is filtering the branch name, following your example It should be like this:

staging->master

on:
  push:
    branches:
      - 'staging'
jobs:
  merge-branch:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - name: Sync
        uses: devmasx/merge-branch@v1.1.0
        with:
          type: now
          target_branch: 'master'
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

master->develop

on:
  push:
    branches:
      - 'master'
jobs:
  merge-branch:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - name: Sync
        uses: devmasx/merge-branch@v1.1.0
        with:
          type: now
          target_branch: 'develop'
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

Taking into account that we need to create 2 workflows files.

We can create an optional input, GITHUB_SHA or HEAD_TO_MERGE. But I'm not sure how it works if we try to sync multiple branches in the same workflow.

But the problem is If no filters are used, when finished workflow that will execute "merge staging->master", Github will trigger another workflow for the master branch. That means Github always triggers a workflow every time a branch has new changes.

I would really appreciate your help.

MiguelSavignano commented 4 years ago

I just tried and I surprise, at the end of a merge Github doesn't trigger another workflow. :laughing:

Maybe that behavior only happens in the beta version of GithubActions or something change in the Github API, anyway right now I can add the new optional input in this PR #4.

Thanks! @Hatzelencio