GitHub Actions : git pull on the server - github

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

Related

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?

Forking actions/checkout#v2 at organization level to be used repo's github actions

Problem:
Inherently, github actions has no information about the code within the repo it's being run in. To rid that problem, there is the actions/checkout workflow that is the defacto start of most workflows.
Our enterprise account got locked down to only local actions only:
Because of this, we are not able to use the actions/checkout#v2 at the start of our workflow, thus rendering our Github Actions useless.
Proposed Solution
Fork the actions/checkout repo as a submodule of a repo and use that reference in my code like so:
steps:
- uses: <enterprise_name>/<repo_name>/checkout#main
When running this action as a test, I get this error message:
Error: Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under
'/home/runner/work/_actions/<enterprise_name>/<repo_name>/main/checkout'.
Did you forget to run actions/checkout before running your local action?
So my question:
Is there a way to run a forked or local version of actions/checkout? The above example is telling me, I can't run a local version of actions/checkout because I have clone the repo which is ironic error.
You need to push the actions/checkout repo into an internal or public repo on your enterprise. Then update your workflow to reference organization/repo#2 instead.
After forking the actions/checkout repo to my jessehouwing-actions this would result in the following update of the YAML:
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v3
Would become:
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: jessehouwing-actions/checkout#v3
Don't use submodules.

GitHub Workflow - auto push to git repo after pull_request

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/

github action for updating package.json

is there any github actions for updating the packages version is package.json [the packages own version , not dependency] ?
Ideally for push action , it should update the version and check in the changes back to repo
I don't know of any actions that handle this complete flow. However, if you are able to update the version in a run script command it's fairly straightforward to commit back to the repository yourself in a workflow. See the following answer for how to prepare the checked out repository and git config in order to push to the remote.
Push to origin from GitHub action
Alternatively, you might find create-pull-request action useful for this use case. It will commit changes to the Actions workspace to a new branch and raise a pull request. So if you call create-pull-request action after npm version during a workflow, you can have that change raised as a PR for you to review and merge.
For example:
on: push
name: Update Version
jobs:
createPullRequest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Bump version
run: npm version patch
- name: Create Pull Request
uses: peter-evans/create-pull-request#v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: Bump version
title: Bump version
Note: Using on: push may not be the best trigger for this use case.