github action for updating package.json - github

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.

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.

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/

Is it possible to update files as part of a merge via GitHub Actions without creating a second commit?

Currently, I'm trying to automate the bump versions for the applications as part of the PR Merge using the GitHub Actions. There is a custom script that would identify the current version of the application and the label attached with the PR and bump the major|minor|patch version accordingly on the file where the version numbers are stored. It is important that the version bump happens only at the time when PR is merged and as part of GitHub Actions because it helps in avoiding the merge conflict in the version file and takes away the manual way of bumping the version.
The GitHub Actions code snippet is given below.
jobs:
release:
# Skip on Pull Request Close event.
if: "!(github.event_name == 'pull_request' && !github.event.pull_request.merged)"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: ${{ github.ref }}
- id: bumpversion
if: github.event.pull_request.merged
env:
PR_LABELS: ${{ toJson(github.event.pull_request.labels) }}
run: bash .github/scripts/bump_version.sh -l "${PR_LABELS}"
The bump_version.sh script has a function given below that makes the version changes and then pushes them to the main branch.
git config user.email "${GITHUB_ACTOR}#users.noreply.github.com"
echo "commiting the version bump changes..."
git add .
git commit -m "Bump Version v${current_version} -> v${incremented_version}"
echo "pushing the version bump changes..."
git push origin dev
This runs fine but the problem is that it makes 2 commits on the main branch when a PR is merged to it. There is a CI/CD pipeline that listens to this main branch for changes and it gets triggered when there is a new commit to it. Since this PR merge makes 2 commits, it is triggered twice.
2 commits for single PR merge image
The question: Is it possible to update files as part of a merge via GitHub Actions without creating a second commit? Is there any other way to achieve a solution to this problem that will help me to bump the version as part of the PR Merge?
My immediate thought is that you could alter the trigger ("on:") in CI/CD to ignore the first of the 2 commits, and only run CI/CD on the second one.
There's a number of ways you could do that, utilizing any of the metadata that comes with a commit. Here's an idea that utilizes the tag. You'd have to refactor the code that defines incremented_version probably, but shouldn't be hard to do in bash.
Here's a CI/CD that would ignore something tagged *.0.0, but would trigger on anything like *.1 or *.0.1:
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- !v*.0.0
- !v*.0 # Do not push events to tags v1.0.0, v1.0, v2.0.0, and v2.0, etc.
Personally I like the idea of making the 1st of the 2 commits have an extra .0 and ignoring those. It seems cleaner. After configuring the bash script to do that,:
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- !v*.0.0 # Do not push events to tags v1.0.0, v2.0.0, etc.

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