GitHub Workflow - auto push to git repo after pull_request - github

I host my git-repo on GitHub.com. Several developers contribute code to either the develop- or feature branch respectively. Once all the tests have passed, the changes are merged into master.
Now, once such a pull-request/merge has been done, the master brach shall be pushed automatically to another GitHub-Repo (where another team picks it up). This is when "Workflows" come into play.
So I created this yaml file to trigger a "git push" after a successfull pull-requrest:
name: push master to official repository
on:
pull_request:
types: [closed]
jobs:
gitHubPush:
runs-on: ubuntu-latest
steps:
- run: "git push https://github.com/OFFICIAL/MyProject master"
But this doesn't work. I get:
fatal: not a git repository (or any of the parent directories): .git
Error: Process completed with exit code 128.
The GitHub Account is registered as "Contributor" on OFFICIAL.
Can you help me out ?

You have to checkout your repository first:
- name: Checkout
uses: actions/checkout#v2
More information about checkout options to find the best fitting your needs here: https://github.com/actions/checkout/

Related

Trigger GitHub action from another branch when default branch doesn't have the workflow file

I am able to run workflows on GitHub fine. But I don't want my git history to contain anything unrelated to the development of the software, and nothing specific to any one code hosting site, e.g., GitHub. Basically I don't want to add and track .github/workflows/ in my git repo.
Thinking it would work, I created a separate branch, gh-actions, and added the .github/workflows/main.yml there.
on:
workflow_dispatch:
push:
branches:
- master
pull_request:
branches: [ master ]
jobs:
<snip>
But apparently this workflow won't even be registered by GH. So I added gh-actions branch to on: push: branches. After pushing this branch to GH, the workflow was registered (checked with gh workflow list).
Now I went to run it on the default branch and got error.
$ gh workflow run .github/workflows/main.yml --ref master
could not create workflow dispatch event: HTTP 422: Workflow does not have 'workflow_dispatch' trigger (https://api.github.com/repos/3N4N/TwitchRecover/actions/workflows/14900813/dispatches)
Apparently workflow_dispatch event only works if it's in the default branch.
I'm now out of ideas. One final way is to change the default branch to gh-actions so that the workflow can be run on master branch, but that would mean that cloning this repository would by default clone the gh-actions branch, and I would rather not deal with that.
If anyone else has any idea, I'd be glad to try it.

Cannot trigger GitHub Actions while pull request from a fork repo

There is a private repo and have a GitHub Actions.
If I make pull request between branches in this repo, GitHub Actions triggered correctly.
name: CI
on:
pull_request:
branches:
- pre-production
- production
jobs:
build:
runs-on: self-hosted
steps:
- uses: actions/checkout#v3
with:
fetch-depth: 2
...
Another developer who only has read premission fork this repo, make some commits, then pull request to the Upstream. The GitHub Actions doesn't been triggered. I have confirmed that he pull request to the correct branch.
Is there any setting let other developer who only has read premission trigger the action in Upstream?
Updated:
There is a option in repo settings called "Run workflows from fork pull requests" but I cannot enable it.
Finally, I found a setting called "Run workflows from fork pull requests". Enable it will solve the problem.
If the repo is under an organization, we should enabled it in the organization setting. After that, we can enable it in the repo setting.

How to get current branch name in push AND pull_request events?

In my Github Action, I need to clone another repo on the same branch as the one that triggered the workflow.
So if that was a push to master, then I need to clone the remote repo on the master branch.
And if it was a pull request from the feature/xxx-yyy branch, I need to symmetrically clone the remote repo on the feature/xxx-yyy branch.
I reckon I can use ${GITHUB_HEAD_REF} in the case of pull_request, but it seems it won't work with push.
- uses: actions/checkout#v3
with:
repository: 'my-org/e2e-tests'
ref: ${GITHUB_HEAD_REF}
token: '${{ secrets.CI_PAT }}'
path: 'e2e'
Any bits of advice on how to do that?

How to check the diff between the last 2 pushed commits

So, I have a GitHub workflow that runs on push and checks for changes in a specific directory and runs a script if there are changes. However, with the current implementation (git diff HEAD^ HEAD), it only checks the diff between the last 2 commits. It is entirely possible that more than 1 commit was pushed so I am looking for a way through which I can compare the last 2 pushed commits.
Would be awesome if anyone can help me with this!
You can use the following:
on:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
# checkout full tree
fetch-depth: 0
- run: |
git diff ${{github.event.before}} ${{github.sha}}
As per the docs on the github context and the docs on the push webhook event data {{github.event.before}} is replaced by the commit SHA before the push. {{github.sha}} or {{github.event.after}} is replaced by the SHA of the latest commit that was pushed.

GitHub Actions : git pull on the server

I have a personal website on a GitHub repo called personal-website and every-time I am making changes locally, I have to SSH into my server (DigitalOcean), go to var/www/personal-website and make a git pull.
I am trying to find a way to pull from the master every-time there is a push into the same branch. I read about GitHub actions and wrote a file on .github/workflows/devops.yml
name: Build and Deploy
on:
push:
branches: master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
GitHub Actions result of Build and Deploy job
On my GitHub Actions page, the job is successful. However, there is no automatic pull request that is done on the server side. How can I fix this?
Make a .sh script and do git pull / git commit / git push