GitHub Actions: Move branch to another branch - github

So I'm working on a project where I have a number of Sass stylesheets, and I want to host it on GitHub Pages. I've pointed Pages to the gh-pages branch, and when I make a change I am trying to set up Actions to 'teleport' that branch to my freshly-pushed main source code branch, make a commit containing the generation of new CSS files from the Sass source code, and push that to the gh-pages branch for Pages to host. This way I know it has started from the latest version of the source, with no leftover generated files, and run a single deterministic compilation to create the CSS files I'm after.
I've been able to set this up, but the only way I've been able to get it working is by deleting the gh-pages branch, checking out the main branch, recreating gh-pages and committing from there. The problem with this is that, when the gh-pages branch gets deleted, GitHub Pages stops tracking it and moves back to the main branch. Here's the workflow anyway, just for reference:
name: Compile Sass
on:
push:
branches:
- main
jobs:
build_css:
runs-on: ubuntu-latest
steps:
- name: Checkout source Git branch
uses: actions/checkout#v2
with:
ref: main
fetch-depth: 10
submodules: true
- name: Delete gh-pages branch
uses: dawidd6/action-delete-branch#v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branches: gh-pages
- name: Create fresh gh-pages branch
uses: peterjgrainger/action-create-branch#v2.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
branch: 'gh-pages'
- name: Checkout new gh-pages branch
uses: actions/checkout#v2
with:
ref: gh-pages
fetch-depth: 1
- name: Compile CSS from SCSS files
uses: gha-utilities/sass-build#v0.4.5
with:
source: styles/index.scss
destination: styles/index.css
- name: Add and commit changes to gh-pages branch
run: |
git config --local user.email 'action#github.com'
git config --local user.name 'GitHub Action'
git add styles/*
git commit -m 'Compile website'
- name: Push changes
uses: ad-m/github-push-action#master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: gh-pages
Solutions I've tried using git rebase and git reset haven't worked for a range of reasons, and I've found that the former (using git rebase --onto main gh-pages gh-pages) works on my local machine but not in CI.
I feel like this must be a reasonably normal thing to want to do, so are there any more widely-accepted methods that I've missed?

Related

Create a PR from feature to master automatically without creating a temp branch [duplicate]

I have GitHub repository with 2 branches: "master" & "develop".
The workflow for us is that any code should be committed to the "develop" branch then pushed to GitHub, then a Pull Request should be created to merge the commits into the "master" branch.
I am trying to write an Action that will create a Pull Request once a developer pushes commits to the branch "develop" and had the following script:
name: Create pull request
on:
push:
branches:
- develop
jobs:
prForMasterBranch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Create Pull Request
uses: peter-evans/create-pull-request#v2
with:
commit-message: update master branch
title: Update master branch
branch: develop
I can see that this action has executed successfully on "Push" event of the "develop" branch, but I can't see any new Pull Requests!
I checked the logs for the action and found these lines at the end of pull request creation:
Pushing pull request branch to 'origin/develop'
Branch 'develop' no longer differs from base branch 'master'
Closing pull request and deleting branch 'develop'
It seems I am missing something, but couldn't figure it out.
Any help is appreciated.
If you look at the documentation of the create-pull-request action, it mentions that
Create Pull Request action will:
Check for repository changes in the Actions workspace. This includes:
untracked (new) files
- tracked (modified) files
- commits made during the workflow that have not been pushed
Commit all changes to a new branch,
or update an existing pull request branch.
Create a pull request to
merge the new branch into the base—the branch checked out in the
workflow.
It would always need an intermediary branch where it can commit the changes.
So if you modify your workflow config as below, adding the Reset master branch step to get the latest changes from the remote develop branch and reset the master branch, and specify branch: temp for the action, the workflow would create a temp branch with the same commits that you have pushed to develop branch and open a PR from temp to master branch. In subsequent commits to develop, it would keep on making the same changes to temp branch and open a PR similarly or update the existing PR.
name: Create pull request
on:
push:
branches:
- develop
jobs:
prForMasterBranch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Reset master branch
run: |
git fetch origin develop:develop
git reset --hard develop
- name: Create Pull Request
uses: peter-evans/create-pull-request#v4
with:
commit-message: update master branch
title: Update master branch
branch: temp
delete-branch: true
assignees: user-you-want-to
reviewers: user-you-want-to
Note that the temp branch will have the exact commits that are pushed to the develop branch.
PR without intermediate branch
Replace
team-you-want-to
or
user-you-want-to
with team or user you want assign if needed, if not comment them.
name: Create pull request
on:
push:
branches:
- develop
jobs:
reatePullRequest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Get latest changes
run: |
git fetch origin develop:develop
git reset --hard develop
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request#v4
with:
commit-message: Update master
committer: GitHub <noreply#github.com>
author: ${{ github.actor }} <${{ github.actor }}#users.noreply.github.com>
signoff: false
branch: develop
title: 'Updating master'
labels: |
update
reviewers: user-you-want-to
team-reviewers: |
team-you-want-to
draft: false

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 trigger a github action when a github action push code

I have build a github action, which config as
name: clock-in
on:
workflow_dispatch:
push:
paths:
- 'src/github/log/*'
jobs:
clock-in:
runs-on: ubuntu-latest
steps:
- ...
- run: ./src/github/push.sh
and the push.sh
#! /bin/bash
remote_repo="https://${GITHUB_ACTOR}:${GH_TOKEN}#github.com/${GITHUB_REPOSITORY}.git" # remote repo address
git config user.email "41898282+github-actions[bot]#users.noreply.github.com"
git config user.name "GitHub Actions"
git add .
git commit -m "update clockin log 😎"
git push
I wish that the github action can be triggered when 'src/github/log/*' file changed and pushed. however, when I make a push, it work normally, but when github action push the change, the new acion is not triggered. How I can make the action triggered when the push comes from github action

Create Pull Requests Automatically [duplicate]

I have GitHub repository with 2 branches: "master" & "develop".
The workflow for us is that any code should be committed to the "develop" branch then pushed to GitHub, then a Pull Request should be created to merge the commits into the "master" branch.
I am trying to write an Action that will create a Pull Request once a developer pushes commits to the branch "develop" and had the following script:
name: Create pull request
on:
push:
branches:
- develop
jobs:
prForMasterBranch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Create Pull Request
uses: peter-evans/create-pull-request#v2
with:
commit-message: update master branch
title: Update master branch
branch: develop
I can see that this action has executed successfully on "Push" event of the "develop" branch, but I can't see any new Pull Requests!
I checked the logs for the action and found these lines at the end of pull request creation:
Pushing pull request branch to 'origin/develop'
Branch 'develop' no longer differs from base branch 'master'
Closing pull request and deleting branch 'develop'
It seems I am missing something, but couldn't figure it out.
Any help is appreciated.
If you look at the documentation of the create-pull-request action, it mentions that
Create Pull Request action will:
Check for repository changes in the Actions workspace. This includes:
untracked (new) files
- tracked (modified) files
- commits made during the workflow that have not been pushed
Commit all changes to a new branch,
or update an existing pull request branch.
Create a pull request to
merge the new branch into the base—the branch checked out in the
workflow.
It would always need an intermediary branch where it can commit the changes.
So if you modify your workflow config as below, adding the Reset master branch step to get the latest changes from the remote develop branch and reset the master branch, and specify branch: temp for the action, the workflow would create a temp branch with the same commits that you have pushed to develop branch and open a PR from temp to master branch. In subsequent commits to develop, it would keep on making the same changes to temp branch and open a PR similarly or update the existing PR.
name: Create pull request
on:
push:
branches:
- develop
jobs:
prForMasterBranch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Reset master branch
run: |
git fetch origin develop:develop
git reset --hard develop
- name: Create Pull Request
uses: peter-evans/create-pull-request#v4
with:
commit-message: update master branch
title: Update master branch
branch: temp
delete-branch: true
assignees: user-you-want-to
reviewers: user-you-want-to
Note that the temp branch will have the exact commits that are pushed to the develop branch.
PR without intermediate branch
Replace
team-you-want-to
or
user-you-want-to
with team or user you want assign if needed, if not comment them.
name: Create pull request
on:
push:
branches:
- develop
jobs:
reatePullRequest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Get latest changes
run: |
git fetch origin develop:develop
git reset --hard develop
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request#v4
with:
commit-message: Update master
committer: GitHub <noreply#github.com>
author: ${{ github.actor }} <${{ github.actor }}#users.noreply.github.com>
signoff: false
branch: develop
title: 'Updating master'
labels: |
update
reviewers: user-you-want-to
team-reviewers: |
team-you-want-to
draft: false

Create Pull Request on Git Push

I have GitHub repository with 2 branches: "master" & "develop".
The workflow for us is that any code should be committed to the "develop" branch then pushed to GitHub, then a Pull Request should be created to merge the commits into the "master" branch.
I am trying to write an Action that will create a Pull Request once a developer pushes commits to the branch "develop" and had the following script:
name: Create pull request
on:
push:
branches:
- develop
jobs:
prForMasterBranch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Create Pull Request
uses: peter-evans/create-pull-request#v2
with:
commit-message: update master branch
title: Update master branch
branch: develop
I can see that this action has executed successfully on "Push" event of the "develop" branch, but I can't see any new Pull Requests!
I checked the logs for the action and found these lines at the end of pull request creation:
Pushing pull request branch to 'origin/develop'
Branch 'develop' no longer differs from base branch 'master'
Closing pull request and deleting branch 'develop'
It seems I am missing something, but couldn't figure it out.
Any help is appreciated.
If you look at the documentation of the create-pull-request action, it mentions that
Create Pull Request action will:
Check for repository changes in the Actions workspace. This includes:
untracked (new) files
- tracked (modified) files
- commits made during the workflow that have not been pushed
Commit all changes to a new branch,
or update an existing pull request branch.
Create a pull request to
merge the new branch into the base—the branch checked out in the
workflow.
It would always need an intermediary branch where it can commit the changes.
So if you modify your workflow config as below, adding the Reset master branch step to get the latest changes from the remote develop branch and reset the master branch, and specify branch: temp for the action, the workflow would create a temp branch with the same commits that you have pushed to develop branch and open a PR from temp to master branch. In subsequent commits to develop, it would keep on making the same changes to temp branch and open a PR similarly or update the existing PR.
name: Create pull request
on:
push:
branches:
- develop
jobs:
prForMasterBranch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Reset master branch
run: |
git fetch origin develop:develop
git reset --hard develop
- name: Create Pull Request
uses: peter-evans/create-pull-request#v4
with:
commit-message: update master branch
title: Update master branch
branch: temp
delete-branch: true
assignees: user-you-want-to
reviewers: user-you-want-to
Note that the temp branch will have the exact commits that are pushed to the develop branch.
PR without intermediate branch
Replace
team-you-want-to
or
user-you-want-to
with team or user you want assign if needed, if not comment them.
name: Create pull request
on:
push:
branches:
- develop
jobs:
reatePullRequest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Get latest changes
run: |
git fetch origin develop:develop
git reset --hard develop
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request#v4
with:
commit-message: Update master
committer: GitHub <noreply#github.com>
author: ${{ github.actor }} <${{ github.actor }}#users.noreply.github.com>
signoff: false
branch: develop
title: 'Updating master'
labels: |
update
reviewers: user-you-want-to
team-reviewers: |
team-you-want-to
draft: false