Skip to content

Commit a51640c

Browse files
committed
feat: support git push arguments
ref issue #100
1 parent d1d225c commit a51640c

File tree

4 files changed

+45
-26
lines changed

4 files changed

+45
-26
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Add a step like this to your workflow:
4343
# Default: '--no-rebase'
4444
pull_strategy: '--no-rebase or --no-ff or --rebase'
4545

46-
# Whether to push the commit and, if any, its tags to the repo (only `true` and `false` are accepted)
46+
# Whether to push the commit and, if any, its tags to the repo. It can also be used to set the git push arguments (see the paragraph below for more info)
4747
# Default: true
4848
push: false
4949

@@ -76,6 +76,17 @@ You can delete files with the `remove` option: that runs a `git rm` command that
7676
The script will not stop if one of the git commands fails. E.g.: if your command shows a "fatal: pathspec 'yourFile' did not match any files" error the action will go on.
7777
You can also use JSON or YAML arrays (e.g. `'["first", "second"]'`, `"['first', 'second']"`) to make the action run multiple `git rm` commands: the action will log how your input has been parsed. Please mind that your input still needs to be a string because of how GitHub Actions works with inputs: just write your array inside the string, the action will parse it later.
7878

79+
### Pushing:
80+
81+
By default the action runs the following command: `git push origin ${branch input} --set-upstream`. You can use the `push` input to modify this behavior, here's what you can set it to:
82+
83+
- `true`: this is the default value, it will behave as usual.
84+
- `false`: this prevents the action from pushing at all, no `git push` command is run.
85+
- any other string:
86+
The action will use your string as the arguments for the `git push` command. Please note that nothing is used other than your arguments, and the command will result in `git push ${push input}` (no remote, no branch, no `--set-upstream`, you have to include them yourself).
87+
88+
One way to use this is if you want to force push to a branch of your repo: you'll need to set the `push` input to, for example, `origin yourBranch --force`.
89+
7990
### Tagging:
8091

8192
You can use the `tag` option to enter the arguments for a `git add` command. In order for the action to isolate the tag name from the rest of the arguments, it should be the first word not preceded by an hyphen (e.g. `-a tag-name -m "some other stuff"` is ok).
@@ -192,6 +203,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
192203

193204
<!-- markdownlint-enable -->
194205
<!-- prettier-ignore-end -->
206+
195207
<!-- ALL-CONTRIBUTORS-LIST:END -->
196208

197209
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ inputs:
2727
required: false
2828
default: '--no-rebase'
2929
push:
30-
description: Whether to push the commit and, if any, its tags to the repo
30+
description: Whether to push the commit and, if any, its tags to the repo. It can also be used to set the git push arguments (more info in the README)
3131
required: false
32+
default: 'true'
3233
remove:
3334
description: Arguments for the git rm command
3435
required: false

lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,34 @@ console.log(`Running in ${baseDir}`)
9999
.catch((err) => setFailed(err))
100100
} else info('> No tag info provided.')
101101

102-
if (getInput('push')) {
102+
const pushOption = parseBool(getInput('push')) ?? getInput('push')
103+
if (pushOption) {
104+
// If the options is `true | string`...
103105
info('> Pushing commit to repo...')
104-
await git.push(
105-
'origin',
106-
getInput('branch'),
107-
{ '--set-upstream': null },
108-
(err, data?) => {
109-
if (data) setOutput('pushed', 'true')
110-
return log(err, data)
111-
}
112-
)
106+
107+
if (pushOption === true) {
108+
debug(`Running: git push origin ${getInput('branch')} --set-upstream`)
109+
await git.push(
110+
'origin',
111+
getInput('branch'),
112+
{ '--set-upstream': null },
113+
(err, data?) => {
114+
if (data) setOutput('pushed', 'true')
115+
return log(err, data)
116+
}
117+
)
118+
} else {
119+
debug(`Running: git push ${pushOption}`)
120+
await git.push(
121+
undefined,
122+
undefined,
123+
pushOption.split(' '),
124+
(err, data?) => {
125+
if (data) setOutput('pushed', 'true')
126+
return log(err, data)
127+
}
128+
)
129+
}
113130

114131
if (getInput('tag')) {
115132
info('> Pushing tags to repo...')
@@ -294,26 +311,15 @@ async function checkInputs() {
294311
)})`
295312
)
296313
}
297-
298314
// #endregion
299315

300316
// #region push
301-
setDefault('push', 'true')
302317
if (getInput('push')) {
303-
// It's just to scope the parsed constant
318+
// It has to be either 'true', 'false', or any other string (use as arguments)
304319
const parsed = parseBool(getInput('push'))
305320

306-
if (parsed === undefined)
307-
throw new Error(
308-
`"${getInput(
309-
'push'
310-
)}" is not a valid value for the 'push' input: only "true" and "false" are allowed.`
311-
)
312-
313-
if (!parsed) setInput('push', undefined)
314-
315321
debug(
316-
`Current push option: ${getInput('push')} (${typeof getInput('push')})`
322+
`Current push option: '${getInput('push')}' (parsed as ${typeof parsed})`
317323
)
318324
}
319325
// #endregion

0 commit comments

Comments
 (0)