I have a static blog setup and every time I want a new post I have to commit and push a .md file. This is part of a group, so I was wondering if there is a way to automate the commit and push part every time a new .md file is saved to a google drive folder.
The first part is covered by IFTTT, where every time a new file is uploaded, a new issue is created on github containing the link to the file in the body.
However, I don't know how to create the action that will now download the file, create a new branch, commit and push the file to that branch and finally set up a pull request for me to approve.
If you know of any other way of automating this process I am open to suggestions!
Thanks!
Edit1:
I am not really sure how to complete this (including generating a random number where I wrote <random-number>. Here's what I have so far:
name: Pull request on issue
on:
issues:
jobs:
create:
runs-on: ubuntu-latest
steps:
- name: create branch
uses: peterjgrainger/[email protected]
with:
# The branch to create
branch: post-<random-number>
- name: download file
run: wget ${{ github.event.issue.body }} -O source/_posts/
- name: commit and push new file
run: |
git config user.name "GitHub Actions Bot"
git config user.email "<>"
git add .
git commit -m "New post"
git push -u origin post-<random-number>
- name: create pull request towards the main branch
uses: peter-evans/[email protected]
with:
token: ${{ secrets.GH_TOKEN }}
commit-message: Auto Pull Request
title: New post pr
body: Auto-created Pull Request
branch: post-<random-number> # The branch where you commit
base: master # Don't forget to specify the right base branch here
Edit2:
name: Pull request on issue
on:
issues:
inputs:
secret:
required: true
description: "Github PAT"
jobs:
create:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Generate random number
id: random
run: echo "::set-output name=value::$(echo $RANDOM)"
- name: create branch
uses: peterjgrainger/[email protected]
with:
# The branch to create
branch: post-${{ steps.random.outputs.value }}
- name: download file
run: wget ${{ github.event.issue.body }} -O source/_posts/
- name: commit and push new file
run: |
git config user.name "GitHub Actions Bot"
git config user.email "<>"
git add .
git commit -m "New post"
git push -u origin post-${{ steps.random.outputs.value }}
- name: create pull request towards the main branch
uses: peter-evans/[email protected]
with:
token: ${{ secrets.GH_TOKEN }}
commit-message: Auto Pull Request
title: New post pr
body: Auto-created Pull Request
branch: post-${{ steps.random.outputs.value }} # The branch where you commit
base: master # Don't forget to specify the right base branch here
This is what I have at the moment, thanks to @GuiFalourd.
Unfortunately, when I run this, I get the following error:
Run peterjgrainger/[email protected]
Error: No token defined in the environment variables
Which token is it referring to? Is this referring to the private action you mentioned? The repository is public, though.
Thanks again for your help!
CodePudding user response:
Updating your workflow to add the random number could be achieved using an output variable. I also think you need to add the actions/checkout
action to your repo to access the downloaded file.
Updated workflow file would look like this
name: Pull request on issue
on:
issues:
jobs:
create:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Generate random number
id: random
run: echo "::set-output name=value::$(echo $RANDOM)"
- name: Example how to use the output
run: echo "${{ steps.random.outputs.value }}"
- name: create branch
uses: peterjgrainger/[email protected]
with:
# The branch to create
branch: post-${{ steps.random.outputs.value }}
- name: download file
run: wget ${{ github.event.issue.body }} -O source/_posts/
- name: commit and push new file
run: |
git config user.name "GitHub Actions Bot"
git config user.email "<>"
git add .
git commit -m "New post"
git push -u origin post-${{ steps.random.outputs.value }}
- name: create pull request towards the main branch
uses: peter-evans/[email protected]
with:
token: ${{ secrets.GH_TOKEN }}
commit-message: Auto Pull Request
title: New post pr
body: Auto-created Pull Request
branch: post-${{ steps.random.outputs.value }} # The branch where you commit
base: master # Don't forget to specify the right base branch here
You could also create a composite
action that could reproduce all the steps you are using for example using something like this:
The action action.yml would look like this.
name: 'Action Name'
description: 'Runs a composite step action'
inputs:
secret:
required: true
description: "Github PAT"
runs:
using: "composite"
steps:
- name: Checkout
uses: actions/[email protected]
- name: Generate random number
id: random
run: echo "::set-output name=value::$(echo $RANDOM)"
shell: bash
- name: Example how to use the output
run: echo "${{ steps.random.outputs.value }}"
shell: bash
- name: create branch
uses: peterjgrainger/[email protected]
with:
# The branch to create
branch: post-${{ steps.random.outputs.value }}
- name: download file
run: wget ${{ github.event.issue.body }} -O source/_posts/
- name: commit and push new file
run: |
git config user.name "GitHub Actions Bot"
git config user.email "<>"
git add .
git commit -m "New post"
git push -u origin post-${{ steps.random.outputs.value }}
shell: bash
- name: create pull request towards the main branch
uses: peter-evans/[email protected]
with:
token: ${{ inputs.secret }}
commit-message: Auto Pull Request
title: New post pr
body: Auto-created Pull Request
branch: post-${{ steps.random.outputs.value }}
base: master
Adding some other inputs
to the action if you want to turn some values dynamic (you can't use the secrets directly inside an action for example).
EDIT
To use this action file locally in the same repository (if you didn't want it to be public), you will need to create a .github/actions/<action-name>
folder, and add this action.yml
file there.
Then, to use it in your workflow job, you will need to update your workflow.yml
to this implementation:
name: Pull request on issue
on:
issues:
jobs:
create:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected] # Necessary to access local action
- name: Local Action Call
uses: ./.github/actions/<action-name>
with:
secret: ${{ secrets.GH_TOKEN }}
Updating the <action-name>
for a folder name of your choice.
I've created a workflow example with something similar (calling a local action) here with this action,yml file
Note that there are also many actions on the Github Marketplace to perform a git commit push
operation.