github action push not adding new commit to the branch - github

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 }}

Related

Github Actions: Cache Error and end action without fail message

I have a github action that transforms my Readme from one format to the other and which will then push the new Readme to the repository. For the pushing I have defined this job:
push_readme:
name: Push new Readme
needs: generate_readme
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Download readme result from job 1 generate_readme
uses: actions/download-artifact#v3
with:
name: readme
- name: Commit files
run: |
git config --local user.email "action#github.com"
git config --local user.name "GitHub Action"
git status
git add READMEmd.md
git commit -m "Actions Generated Readme"
- name: Push changes
uses: ad-m/github-push-action#master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
The commit returns an error when there is nothing to commit - which happens whenever the readme was not updated in the recent push. This is expected and fine. However, I would like to handle this error properly s.t. the action simply ends when it occurs WITHOUT telling me it failed. Instead I'd like something in the sense of "There is no new README to commit. Ending the action".
Could anyone point me to how to do that? I failed to find the solution yet.
You can utilize Bash and check git diff for the README file and set an output parameter with GITHUB_OUTPUT for the next step to check if there indeed is a commit.
Here is an example:
- name: Commit files
id: commit
run: |
git config --local user.email "action#github.com"
git config --local user.name "GitHub Action"
git status
if [[ -n $(git diff README.md) ]]; then
git add README.md
git commit -m "Actions Generated Readme"
echo "DONE=true" >> $GITHUB_OUTPUT
else
echo "README is the same. Nothing to commit."
fi
- name: Push changes
if: ${{ steps.commit.DONE }}
uses: ad-m/github-push-action#master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

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.

Automatically create merge-back pull request

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.

Create a 'release' entry in Github

I'd like to create a release entry on my Github project.
I tried this from the cli
git commit -a -m 123
git tag -a "123" -m "msg"
git push
This commits the files but I don't see a release tab entry - nor do I see the tag.
Thanks.
By default, the git push command doesn’t transfer tags to remote servers. You will have to explicitly push tags to a shared server after you have created them.
From Git Basics - Tagging
No need for the double quotes on git tag - git tag -a 123 -m "msg"
Use the --tags flag on push - git push --tags

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