Automatically create merge-back pull request - github

How do I use the create-pull-request GitHub Action to automatically create a PR from my current branch to master?
I have the following workflow and I want to use GitHub Actions to make sure I'm tracking all release changes to master.
[bugfix/some-bug] -> PR -> Merge -> [release/v1.2.3] -> autogen PR to [master]
At the end, I would like a PR to be present in GitHub that is the following:
[master] <- [release/v1.2.3]
Autogenerated by Github Actions.
Mergeback of Release to ensure changes are persisted in Master.
I'm using the following action and actions file which will only trigger once a release/* branch has been merged into.
https://github.com/peter-evans/create-pull-request
on:
push:
branches:
- release/*
jobs:
build:
runs-on: self-hosted
steps:
- name: Extract Branch
shell: bash
id: extract_branch
run: |
echo "::set-output name=branch_type::$(echo ${GITHUB_REF#refs/heads/} | sed 's/\/.*//')"
echo "::set-output name=branch_name::$(echo ${GITHUB_REF#refs/heads/} | sed 's/.*\///')"
- uses: actions/checkout#v3
with:
submodules: recursive
- uses: peter-evans/create-pull-request#v4
with:
commit-message: Mergeback of Release ${{ steps.extract_branch.outputs.branch_name }}
title: Mergeback of Release ${{ steps.extract_branch.outputs.branch_name }}
branch: ${{ steps.extract_branch.outputs.branch_type }}/${{ steps.extract_branch.outputs.branch_name }}
base: master
body: |
Autogenerated by Github Actions.
Mergeback of Release ${{ steps.extract_branch.outputs.branch_name }} to ensure changes are persisted in Master.
The issue is this plugin takes a really strange journey in creating the PR. The following actions log shows the plugin checking out the branch, identifying the 11 changes, resetting the branch to master, and then declaring the branches no longer differ leading to no PR being created.
Create or update the pull request branch
/usr/bin/git symbolic-ref HEAD --short
release/v1.2.3
Working base is branch 'release/v1.2.3'
/usr/bin/git checkout --progress -B a8b75703-b510-4ced-9486-6b504883b514 HEAD --
Switched to a new branch 'a8b75703-b510-4ced-9486-6b504883b514'
/usr/bin/git status --porcelain -unormal --
/usr/bin/git diff --quiet --
/usr/bin/git diff --quiet --staged --
/usr/bin/git reset --hard
HEAD is now at b574c22 actions
/usr/bin/git clean -f -d
Resetting working base branch 'release/v1.2.3'
/usr/bin/git checkout --progress release/v1.2.3 --
Switched to branch 'release/v1.2.3'
Your branch is up to date with 'origin/release/v1.2.3'.
/usr/bin/git reset --hard origin/release/v1.2.3
HEAD is now at b574c22 actions
Rebasing commits made to branch 'release/v1.2.3' on to base branch 'master'
/usr/bin/git -c protocol.version=2 fetch --no-tags --progress --no-recurse-submodules --unshallow --force origin master:master
...
From https://github.com/myorg/myrepo
* [new branch] master -> master
* [new branch] master -> origin/master
/usr/bin/git checkout --progress master --
Switched to branch 'master'
/usr/bin/git rev-list --reverse release/v1.2.3..a8b75703-b510-4ced-9486-6b504883b514 .
/usr/bin/git checkout --progress -B a8b75703-b510-4ced-9486-6b504883b514 HEAD --
Switched to and reset branch 'a8b75703-b510-4ced-9486-6b504883b514'
/usr/bin/git -c protocol.version=2 fetch --no-tags --progress --no-recurse-submodules --force origin master:master
/usr/bin/git -c protocol.version=2 fetch --no-tags --progress --no-recurse-submodules --force origin release/v1.2.3:refs/remotes/origin/release/v1.2.3
Pull request branch 'release/v1.2.3' already exists as remote branch 'origin/release/v1.2.3'
/usr/bin/git checkout --progress release/v1.2.3 --
Switched to branch 'release/v1.2.3'
Your branch is up to date with 'origin/release/v1.2.3'.
/usr/bin/git diff --quiet release/v1.2.3..a8b75703-b510-4ced-9486-6b504883b514
Resetting 'release/v1.2.3'
/usr/bin/git checkout --progress -B release/v1.2.3 a8b75703-b510-4ced-9486-6b504883b514 --
Reset branch 'release/v1.2.3'
Your branch is behind 'origin/release/v1.2.3' by 11 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
/usr/bin/git rev-list --right-only --count origin/release/v1.2.3...release/v1.2.3
0
/usr/bin/git rev-list --left-only --count origin/release/v1.2.3...release/v1.2.3
11
Updated branch 'release/v1.2.3'
/usr/bin/git rev-list --right-only --count master...release/v1.2.3
0
/usr/bin/git rev-parse HEAD
4f4725ec39b24045ec08d2af4a717740a2669139
/usr/bin/git branch --delete --force a8b75703-b510-4ced-9486-6b504883b514
Deleted branch a8b75703-b510-4ced-9486-6b504883b514 (was 4f4725ec).
Pushing pull request branch to 'origin/release/v1.2.3'
/usr/bin/git push --force-with-lease origin HEAD:refs/heads/release/v1.2.3
To https://github.com/myorg/myrepo
+ b574c227...4f4725ec HEAD -> release/v1.2.3 (forced update)
Branch 'release/v1.2.3' no longer differs from base branch 'master'
Any idea how to configure this plugin to do what I'm looking for? I simply what to pop open a PR from my current actions branch (a release branch) to master.

Related

Github Actions Branch Name

I have a Github Actions workflow where I am trying to use the branch name in my commit.
- name: Commit files
run: |
git config --local user.email github.actions#domain.com
git config --local user.name "username"
git add -A
if ! git diff-index --quiet HEAD; then
git commit -am "${GITHUB_REF#refs/heads/} ${{ env.TAG_NAME }} on ${{ env.CLUSTER_ENV }}"
git push
fi
My commit will fail if I do not have the branch name there. For some reason the output is the same as TAG_NAME.
I have also tried ${{ github.ref_name }} and ${GITHUB_REF##*/}, but the result is the same. How can I display the branch name in my commit message here?
I am starting the workflow from the command line in my local. Not sure if anymore information is needed.

Github error: failed to push some refs to github.com

I have installed the git ,created repository , then I have tried to push from vs code to github repository with cmd
git push -u origin main
Git say error: failed to push some refs to 'github.com:loobj126/boredape-dapp-bj.git''
What's the problem?
https://github.com/loobj126/boredape-dapp-bj
i ve few approach yet still not resolve
https://komodor.com/learn/how-to-fix-failed-to-push-some-refs-to-git-errors/
Latest input :
after initialize
[2022-04-20T08:44:06.669Z] Validating found git in: C:\Program Files\Git\cmd\git.exe
[2022-04-20T08:44:06.796Z] Using git 2.35.3.windows.1 from C:\Program Files\Git\cmd\git.exe
[2022-04-20T10:45:43.723Z] > git init [128ms]
[2022-04-20T10:45:44.399Z] > git rev-parse --git-dir [72ms]
[2022-04-20T10:45:44.403Z] Open repository: c:\Users\loobj\boredape-dapp-bj
[2022-04-20T10:45:44.606Z] > git fetch [197ms]
[2022-04-20T10:45:44.676Z] > git symbolic-ref --short HEAD [119ms]
[2022-04-20T10:45:44.765Z] > git for-each-ref --format=%(refname)%00%(upstream:short)%00%(objectname)%00%(upstream:track) refs/heads/master refs/remotes/master [85ms]
[2022-04-20T10:45:44.872Z] > git remote --verbose [99ms]
[2022-04-20T10:45:44.888Z] > git for-each-ref --sort -committerdate --format %(refname) %(objectname) %(*objectname) [119ms]
[2022-04-20T10:45:45.019Z] > git config --get commit.template [122ms]
[2022-04-20T10:45:45.040Z] > git config --local branch.master.github-pr-owner-number [121ms]
[2022-04-20T10:45:45.300Z] > git symbolic-ref --short HEAD [101ms]
[2022-04-20T10:45:45.385Z] > git for-each-ref --format=%(refname)%00%(upstream:short)%00%(objectname)%00%(upstream:track) refs/heads/master refs/remotes/master [80ms]
[2022-04-20T10:45:45.490Z] > git for-each-ref --sort -committerdate --format %(refname) %(objectname) %(*objectname) [101ms]
[2022-04-20T10:45:45.507Z] > git remote --verbose [115ms]
[2022-04-20T10:45:45.589Z] > git config --get commit.template [76ms]
Click push
[2022-04-20T10:59:18.763Z] > git push boredape-dapp-bj master [153ms]
[2022-04-20T10:59:18.763Z] error: src refspec master does not match any
error: failed to push some refs to 'https://github.com/loobj126/boredape-dapp-bj.git'
[2022-04-20T11:00:41.028Z] > git fetch [141ms]
[2022-04-20T11:00:41.220Z] > git symbolic-ref --short HEAD [68ms]
[2022-04-20T11:00:41.286Z] > git for-each-ref --format=%(refname)%00%(upstream:short)%00%(objectname)%00%(upstream:track) refs/heads/master refs/remotes/master [63ms]
[2022-04-20T11:00:41.380Z] > git for-each-ref --sort -committerdate --format %(refname) %(objectname) %(*objectname) [91ms]
[2022-04-20T11:00:41.394Z] > git remote --verbose [102ms]
[2022-04-20T11:00:41.485Z] > git config --get commit.template [84ms]
Solution 1: for error: src refspec master does not match any.
All you need to perform is git commit with a proper message and then do git push to the remote origin to avoid any errors.
mkdir repo && cd repo
git remote add origin /path/to/origin.git
git add .
git commit -m "initial commit"
git push origin master
Solution 2 – First, check what refs you have, and once you find that, make a git push to the specific remote branch.
# To get all the ref
git show-ref
# replace with your branch name according to ref
git push origin HEAD:<branch>
this worked for me.
To fix the error, go on and run following commands:
git pull --rebase origin main
git push -u origin main
If the first command above runs successfully, you should get a response that says: Successfully rebased and updated refs/heads/main.
The second command pushes your local repo's current state to the remote branch.
ref: https://www.freecodecamp.org/news/error-failed-to-push-some-refs-to-how-to-fix-in-git/

github action push not adding new commit to the branch

I have a workflow like this, where myFile.js is updating one file
- name: Custom action
run: yarn myFile.js
- name: Commit diff
run: |
git add .
git status
git commit -m "Updating file"
git status
git push -u origin origin/${{ github.head_ref }}
Here is the output (successful)
git add .
git status
git commit -m "Updating file"
git status
git push -u origin origin/Feature/custom-patch
git status
shell: /usr/bin/bash -e {0}
env:
NODE_OPTIONS: --max_old_space_size=4096
HEAD detached at pull/95/merge
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: src/123.json
[detached HEAD 9c7a0fd55] Updating file
1 file changed, 2 insertions(+), 2 deletions(-)
HEAD detached from pull/95/merge
nothing to commit, working tree clean
To https://github.com/company/myRepo
35ae5b522..755d05e91 origin/Feature/custom-patch -> origin/Feature/custom-patch
HEAD detached from pull/95/merge
nothing to commit, working tree clean
You need to tell the checkout action to fetch the whole state of commits and tell it specifically to checkout the branch.
Otherwise by default it just checks out the single last commit (which is called a detached HEAD).
- uses: actions/checkout#v2
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}

Github Actions: [remote rejected] master -> master (shallow update not allowed), error: failed to push some refs

In my Github workflow, I am checking out two repositories. Subsequently I merge two directories of the workflow repo "repoA" with repo "repoB". When pushing to repoB, I get an error:
From ../repoA
* [new branch] master -> workspace/master
Automatic merge went well; stopped before committing as requested
[master cbd72fe] Update
To https://github.com/username/repoB.git
! [remote rejected] master -> master (shallow update not allowed)
error: failed to push some refs to 'https://username#github.com/username/repoB.git'
##[error]Process completed with exit code 1.
I don't understand why my repo is shallow and how to fix it. The Github workflow file:
name: test
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout current repo
uses: actions/checkout#v2
with:
path: repoA
- name: Checkout other repo
uses: actions/checkout#v2
with:
repository: username/repoB
path: repoB
token: ${{ secrets.ACCESS_TOKEN }}
- name: Update repo
run: |
cd repoB
git remote add repoa ../repoA
git fetch --unshallow repoa
git config --global user.email "asd#asd.com"
git config --global user.name "username"
git merge -s ours --no-commit --allow-unrelated-histories repoa/master
rm -rf webserver
rm -rf etl
git add .
git read-tree --prefix=webserver -u repoa/master:serv
git read-tree --prefix=etl -u repoa/master:misc_projects/etl
git add .
git commit -m "Update" -a
git push -f https://username#github.com/username/repoB.git
By default actions/checkout only checks out a single commit, making the checkout shallow. If you want all history you can set the fetch-depth input to 0.
See the documentation here.
- uses: actions/checkout#v2
with:
fetch-depth: 0

Rebase the second commit to master

I have edited some files in the repo and now it looks like the following:
history1 --> history2 --> ... --> master
\
\
my commit 1 --> my commit 2
But actually 'my commit 2' should not be on top of 'my commit 1', that's a mistake... How do I only rebase 'my commit 2' to master in mercury?
history1 --> history2 --> ... --> master
\ \
\ \
my commit 1 my commit 2
I am asking how to do this in Mercurial not git.
Make sure you have the rebase extension enabled in your hgrc file. Then:
$ hg rebase -r <commit ID of my commit 2> -d master
See "hg help rebase" for more details.
From a different post:
You can cherry-pick XX to master.
git checkout master
git cherry-pick <commit ID of XX>
And remove the last commit from the feature branch with git reset.
git checkout Feature-branch
git reset --hard HEAD^