How do I trigger a push to a remote server from a GitHub action? - github

I have this remote set up from my local
$ git remote -v
dev myuser#mydomain.remote:/home/myuser/maps.git (fetch)
dev myuser#mydomain.remote:/home/myuser/maps.git (push)
In my remote repo, I have a hook, ~/maps.git/hooks/post-receive, with
# The production directory
TARGET="/var/www/html"
# A temporary directory for deployment
TEMP="/home/myuser/deploy-folder"
# The Git repo
REPO="/home/myuser/maps.git"
# Deploy the content to the temporary directory
mkdir -p $TEMP
#git --work-tree=$TEMP --git-dir=$REPO checkout -f
cd $TEMP
git pull
...
What I would like to do is whenever a PR merges into a particular branch (authentication), I would like to trigger a "git push dev" code push to the remote server from a GitHub action. Not quite sure how to fill in my GitHub action. I have this
name: "Build Dev & Release"
on:
push:
paths:
- "**"
- ".github/workflows/my-github-action.yml"
branches:
- authentication
jobs:
pusht-to-dev-server:
???
but not quite sure how to implement the "push-to-dev-server" action.
Edit: In response to the answer given, I created this file
$ cat .github/workflows/directory-dev.yml
name: "Chicommons Maps Dev: Build & Release"
on:
push:
paths:
- "**"
- ".github/workflows/directory-dev.yml"
branches:
- authentication
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token.
fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository.
- name: Push changes
uses: ad-m/github-push-action#master
with:
github_url: dev.myremote.com:/home/myusername/maps.git
github_token: ${{ secrets.DEPLOY }}
branch: ${{ github.ref }}
and I created this environment encrypted secret
but when I push to my branch (autnentication), I get this error
Run ad-m/github-push-action#master
Push to branch refs/heads/authentication
Missing input "github_token: ${{ secrets.GITHUB_TOKEN }}".

Note that this would assume mydomain.remote is internet-facing (as opposed to on-premise only, behind a DMZ).
That means GitHub servers (where the GitHub action/workflow is executed) must be able to see and contact mydomain.remote.
You can the use the github-push-action, initially made to push to a GitHub repository, but that you can change to include your own domain server.
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token.
fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository.
- name: Push changes
uses: ad-m/github-push-action#master
with:
github_url: mydomain.remote
github_token: ${{ secrets.YOURDOMAIN_TOKEN }}
branch: ${{ github.ref }}

Related

Github repository is empty when rsync is used

I have added workflow to github actions where i want to rsync files from repository to remote server
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Deploy with rsync
run: rsync -avuz --delete . ${{ secrets.USER }}#${{ secrets.HOST }}:~/App/MainPage/
rsync sends nothing because my repository in source folder is empty, even though there are files in repo.
i get this message
I know that rsync works, because if i change dir (eg ../../) files are being transferred to remote server and i can see them there.
I forgot to add checkout action to workflow .yaml file
...
jobs:
build:
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v3
- name: Install SSH Key
uses: shimataro/ssh-key-action#v2
....

Unable to Manually Trigger GitHub Action

I recently started working on using some GitHub actions on my projects. I am able to setup them up to run automatically but am struggling with having them run manually. I know that you need the have the workflow_dispatch in the on section. I'm not sure if it's not working because I have it automatically run too. Is someone able to tell me what I am doing wrong?
Here is one of my workflow YAML files
name: Create-Doc-Nightly
on:
push:
branches: [ "nightly" ]
paths:
- 'src/**'
- 'pom.xml'
workflow_dispatch:
jobs:
doc:
name: Create Doc
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
name: Step 1 - Checkout Nightly Branch
with:
persist-credentials: false
fetch-depth: 0
- name: Step 2 - Setup JDK 17
uses: actions/setup-java#v3.4.1
with:
java-version: 17
distribution: 'temurin'
- name: Step 3 - Remove Doc
run: |
git remote set-url origin https://jnstockley:${{ secrets.TOKEN }}#github.com/jnstockley/BTTN.git
git config user.email "jack#jstockley.com"
git config --local user.name "Jack Stockley"
git rm -r docs
git commit -m "Removed Docs"
git push origin nightly
- name: Step 4 - Create Doc
run: mvn dokka:dokka -f pom.xml
- name: Step 5 - Move Docs
run: |
rm -rf docs
mkdir -p docs
mv target/dokka/* docs
- name: Step 6 - Publish docs
run: |
git remote set-url origin https://jnstockley:${{ secrets.TOKEN }}#github.com/jnstockley/BTTN.git
git config user.email "jack#jstockley.com"
git config --local user.name "Jack Stockley"
git add -f docs
git commit -m "Updated Docs"
git push origin nightly
Link to GitHub repo, nightly branch: https://github.com/jnstockley/BTTN/tree/nightly
The workflow must be on your default branch in order to use workflow_dispatch.
I believe in your case it's only on the branch nightly while it should also be on main.
To manually trigger a workflow, use the workflow_dispatch event. You can manually trigger a workflow run using the GitHub API, GitHub CLI, or GitHub browser interface. For more information, see Manually running a workflow
on: workflow_dispatch
Providing inputs
You can configure custom-defined input properties, default input values, and required inputs for the event directly in your workflow. When you trigger the event, you can provide the ref and any inputs. When the workflow runs, you can access the input values in the inputs context. For more information, see Contexts
This example defines inputs called logLevel, tags, and environment. You pass values for these inputs to the workflow when you run it. This workflow then prints the values to the log, using the inputs.logLevel, inputs.tags, and inputs.environment context properties.
yaml
on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
type: choice
options:
- info
- warning
- debug
tags:
description: 'Test scenario tags'
required: false
type: boolean
environment:
description: 'Environment to run tests against'
type: environment
required: true
jobs:
log-the-inputs:
runs-on: ubuntu-latest
steps:
- run: |
echo "Log level: $LEVEL"
echo "Tags: $TAGS"
echo "Environment: $ENVIRONMENT"
env:
LEVEL: ${{ inputs.logLevel }}
TAGS: ${{ inputs.tags }}
ENVIRONMENT: ${{ inputs.environment }}
If you run this workflow from a browser you must enter values for the required inputs manually before the workflow will run.
You might like the following documentation links
workflow_dispatch
github docs - events-that-trigger-workflows

GitHub actions push to remote repo

How can I push some files that were generated by the runner (user1/repo1) to the main branch from another remote repo (user2/repo1) via GitHub actions?
Please note that:
I set-up a secret key (named ACCESS_TOKEN) in user1/repo1, such that it corresponds to the Personal Access Token from the destination repo (user2/repo1)
the GitHub actions needs to be repeated every ~30 minutes
there already exists a file.rds in the destination repo. The push thus needs to override that file every time
the runner needs to be macOS-latest
This is what I have tried so far:
name: gitaction
on:
schedule:
- cron: "*/30 * * * *"
workflow_dispatch:
jobs:
genFileAndPush:
runs-on: macOS-latest
steps:
- uses: actions/checkout#master
- uses: r-lib/actions/setup-r#master
with:
r-version: '4.1.2'
- name: Run R scripts and generate file
run: |
saveRDS(1:3, file = "file.rds")
shell: Rscript {0}
- name: Push to remote repository
run: |
git config --local user.name actions-user
git config --local user.email "actions#github.com"
git add file.rds
git commit -m "commit"
git remote set-url origin https://env.REPO_KEY#github.com/user2/repo1.git
git push -u origin main
env:
REPO_KEY: ${{secrets.ACCESS_TOKEN}}
username: github-actions
It returns the following error:
remote: Permission to user2/repo1.git denied to github-actions[bot].
fatal: unable to access 'https://github.com/user2/repo1.git/': The requested URL returned error: 403
Error: Process completed with exit code 128.
What am I missing?
Edit
As suggested, I tried using GuillaumeFalourd/git-commit-push#v1.1:
name: gitaction
on:
workflow_dispatch:
jobs:
genFileAndPush:
runs-on: macOS-latest
steps:
- uses: actions/checkout#master
- uses: r-lib/actions/setup-r#master
with:
r-version: '4.1.2'
- name: Run R scripts and generate file
run: |
saveRDS(1:3, file = "file.rds")
shell: Rscript {0}
- uses: actions/checkout#v2.3.4
- uses: GuillaumeFalourd/git-commit-push#v1.1
with:
target_branch: main
files: file.rds
remote_repository: https://github.com/user2/repo1
access_token: ${{secrets.ACCESS_TOKEN}}
force: true
Although there were no error, the file was not pushed (because it was not detected?):
Run GuillaumeFalourd/git-commit-push#v1.1
Run CURRENT_BRANCH=${GITHUB_REF}
WARNING: No changes were detected. git commit push action aborted.
There are some actions on the Github Marketplace that can help you with pushing files to other repositories.
Here is an example of one supported on all OS runners.
The workflow would look like this:
name: gitaction
on:
workflow_dispatch:
jobs:
genFileAndPush:
runs-on: macOS-latest
steps:
- uses: actions/checkout#master
- uses: r-lib/actions/setup-r#master
with:
r-version: '4.1.2'
- name: Run R scripts and generate file
run: |
saveRDS(1:3, file = "file.rds")
shell: Rscript {0}
- uses: GuillaumeFalourd/git-commit-push#v1.3
with:
target_branch: main
files: file.rds
remote_repository: https://github.com/user2/repo1
access_token: ${{secrets.ACCESS_TOKEN}}
force: true
You can find more actions like this one on the marketplace.
Otherwise, you can also perform the whole operation manually using command lines to clone the remote repository, copy the files from the local repo wherever you want on the remote repo, then push the new files to the remote repository.

GitHub action push to pantheon

I'm trying to create an action that pushes changes to pantheon on every push so we can have more advanced workflows with GitHub but keep the convenience of dev-sites created on pantheon. I have set up 3 secrets
SSH_KEY (private key)
HOST_IP
PANTHEON_REPO (url)
And came up with this action after some Google-ing:
name: Push to pantheon
on: [push,pull_request]
jobs:
pusher:
name: Push to pantheon
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Install SSH key
uses: shimataro/ssh-key-action#v2
with:
key: ${{ secrets.SSH_KEY }}
known_hosts: 'just-a-placeholder-so-we-dont-get-errors'
- name: Adding Known Hosts
run: ssh-keyscan -H ${{ secrets.HOST_IP }} >> ~/.ssh/known_hosts
- name: Push changes
run: |
git remote add pantheon ${{ secrets.PANTHEON_REPO }}
git config --local user.email "..."
git config --local user.name "Github Action"
git push pantheon ${GITHUB_REF##*/}
However the action fails at git remote add with the error:
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I created the ssh key specifically for this and added the public key to pantheon.

GitHub action runs twice on merge

I have a build and deploy GitHub action that runs when I update my GitHub pages repository. In addition I have one that updates the recipes using I store.
Most of the time it runs fine but occasionally I update from my phone (with Working Copy) and do a merge, then each action runs twice, all of them triggered by the same push. The recipe update action succeeds both times.
Yet when that happens one of the build and deploy actions fails with something like “! [remote rejected] master -> gh-pages (cannot lock ref 'refs/heads/gh-pages': is at 37c581108d857f9d9c8fe584103d78e4473d280b but expected ceaf2249cc2f7864f0269e64d372fc40ce0b06e0)”
It doesn’t break anything but I’m not sure why it happens and I’d like to fix it.
Build and deploy
on:
push:
branches:
- main
schedule:
- cron: '0 */2 * * *'
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout#v2
with:
persist-credentials: false
- name: Setup Python Environment
uses: actions/setup-python#v2
with:
python-version: 3.8
- name: Install Requirements
run: pip install -r requirements.txt
- name: Execute Python script
run: |
python3 -m papexp
env:
EMAIL: ${{ secrets.EMAIL }}
PASSWORD: ${{ secrets.PASSWORD }}
- name: setup git config
run: |
git config --local user.name ${{ secrets.USERNAME_GITHUB }}
git config --local user.email ${{ secrets.EMAIL }}
git pull --ff-only origin main
git add images/recipes/*
git add .
git commit -am "Update recipes" || echo "Nothing to update"
- name: Push changes
uses: ad-m/github-push-action#master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}
I don't think the action is running twice because of a single push, and I don't think it's related to whether you update from your mobile or not.
Your action runs when you push to main, but it also runs every 2 hours. So sometimes you're going to get conflicts, when the action triggered by a push runs at the same time as a scheduled action.
If you need the action to run in both situations (triggered and scheduled), and if the occasional collisions aren't causing you problems, I'd just put up with it TBH. Trying to implement some kind of locking mechanism to avoid collisions is probably more effort than it's worth.