Skip to content

Commit

Permalink
Added support for PR branch, defaults to main branch (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
emZubair authored Sep 29, 2023
1 parent 52d7d57 commit f17100d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ A GitHub action that automates dependencies update. The action is designed to be
dependencies-autoupdate can:
1. Run a dependencies update command ie: npm update, cargo update.. etc
2. Run a validation step to ensure the update command was successful ie: make, cargo test.. etc
2. Checks out a branch and creates a pull requests with the updated dependencies on success.
3. Checks out a branch and creates a pull requests with the updated dependencies on success.

The action is language independant. Check the sample [go workflow](https://github.com/romoh/dependencies-autoupdate/blob/main/.github/workflows/autoupdate-dependencies-go.yml) & [rust workflow](https://github.com/romoh/dependencies-autoupdate/blob/main/.github/workflows/autoupdate-dependencies-rust.yml).
The action is language independent. Check the sample [go workflow](https://github.com/romoh/dependencies-autoupdate/blob/main/.github/workflows/autoupdate-dependencies-go.yml) & [rust workflow](https://github.com/romoh/dependencies-autoupdate/blob/main/.github/workflows/autoupdate-dependencies-rust.yml).

# Usage
### Example 1
```
jobs:
auto-update:
Expand All @@ -31,6 +32,37 @@ jobs:
update-path: "'./test/go'" #optional
```

### Example 2, A schedule job to upgrade requirements via Make command
```bash
name: Auto update Python dependencies
on:
schedule:
# runs monthly At 00:01 on day-of-month 1
- cron: '1 0 1 * *'
workflow_dispatch:

jobs:
auto-update:
runs-on: ubuntu-latest

steps:
- name: Checkout the head commit of the branch
uses: actions/checkout@v4

- uses: actions/setup-python@v4
with:
python-version: '3.8'

- name: Run auto dependency update
uses: emZubair/dependencies-autoupdate@v1.1
with:
token: ${{ secrets.GITHUB_TOKEN }}
pr-branch: "staging"
update-command: "'make update'"

```

Example 2 will create the PR against `staging` branch at the start of each month
It is recommended to use this action on a [schedule trigger](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#onschedule) at a fixed cadence, but it can be used on other triggers as well. Just note GitHub has limitations on default GITHUB_TOKEN access from forks.

# Action inputs
Expand All @@ -39,6 +71,7 @@ Name | Description | Required | Default
--| --| --| --|
token | GITHUB_TOKEN or a repo scoped Personal Access Token (PAT). | Yes | N/A
update-command | Command to update the dependencies and validate the changes. e.g. `cargo update && cargo test` or `go get -u && go mod tidy && go build`. | Yes | N/A
pr-branch | PR branch against which the PR should be created | No | `main`
update-path | Path to execute the update command if different from the main working directory. | No | defaults to working directory
on-changes-command | Command to execute after updates to dependencies are detected. This will be executed before the pull request is created. e.g. version increment. | No | N/A

Expand All @@ -55,7 +88,7 @@ If a pull request already exists and there are no further changes (i.e. no diff
Notes:
* The committer name is set to the GitHub Actions bot user. GitHub <noreply@github.com>
* The pull request branch name is fixed and defaults to "automated-dependencies-update".
* Pull request defaults to the branch checked out in the workflow.
* Pull request defaults to `main` branch when the value for `pr-branch` is not given

# License
This tool is distributed under the terms of the MIT license. See [LICENSE](https://github.com/romoh/dependencies-autoupdate/blob/main/LICENSE) for details.
5 changes: 3 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'Dependencies Autoupdate'
name: 'Dependencies Autoupdate with Custom branch'
description: 'Language agnostic action that automatically updates dependencies and creates PR with the changes. e.g.: go, js, rust, py, ts'
branding:
icon: 'arrow-up-circle'
Expand All @@ -19,5 +19,6 @@ inputs:
runs:
using: 'composite'
steps:
- run: ${{ github.action_path }}/autoupdate-dependencies.sh ${{ inputs.token }} ${{ inputs.update-command }} ${{ inputs.update-path }}? ${{ inputs.on-changes-command }}?
- run: ${{ github.action_path }}/autoupdate-dependencies.sh ${{ inputs.token }} ${{ inputs.update-command }} ${{ inputs.pr-branch }} ${{ inputs.update-path }}? ${{ inputs.on-changes-command }}?
shell: bash

16 changes: 11 additions & 5 deletions autoupdate-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ set -e

token=$1
update_command=$2
update_path=$3
on_changes_command=$4
pr_branch=$3
update_path=$4
on_changes_command=$5
repo=$GITHUB_REPOSITORY #owner and repository: ie: user/repo
username=$GITHUB_ACTOR

Expand All @@ -23,6 +24,11 @@ if [ -z "$update_command" ]; then
exit 1
fi

if [ -z "$pr_branch" ]; then
echo "pr-branch not set, falling back to `main`"
pr_branch="main"
fi

# remove optional params markers
update_path_value=${update_path%?}
if [ -n "$update_path_value" ]; then
Expand Down Expand Up @@ -50,9 +56,9 @@ else
git checkout $branch_name
git pull

# reset with latest from staging
# reset with latest from $pr_branch
# this avoids merge conflicts when existing changes are not merged
git reset --hard origin/staging
git reset --hard origin/$pr_branch
fi

echo "Running update command $update_command"
Expand Down Expand Up @@ -91,7 +97,7 @@ then
# create the PR
# if PR already exists, then update
response=$(curl --write-out "%{message}\n" -X POST -H "Content-Type: application/json" -H "Authorization: token $token" \
--data '{"title":"Autoupdate dependencies","head": "'"$branch_name"'","base":"staging", "body":"Auto-generated pull request. \nThis pull request is generated by GitHub action based on the provided make update"}' \
--data '{"title":"Autoupdate dependencies","head": "'"$branch_name"'","base":"'"$pr_branch"'", "body":"Auto-generated pull request. \nThis pull request is generated by GitHub action based on the provided update commands."}' \
"https://api.github.com/repos/$repo/pulls")

echo $response
Expand Down

0 comments on commit f17100d

Please sign in to comment.