Skip to content

Commit

Permalink
Add GitHub Enterprise support (ad-m#106)
Browse files Browse the repository at this point in the history
* Add GitHub url and GitHub Enterprise support

* Add review improvements

* Modify the default value to github.server_url
  • Loading branch information
Pascal Zimmermann committed Feb 18, 2022
1 parent 8407731 commit a7824cd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ With ease:
- update new code placed in the repository, e.g. by running a linter on it,
- track changes in script results using Git as archive,
- publish page using GitHub-Pages,
- push changes to a hosted GitHub Enterprise Server instance,
- mirror changes to a separate repository.

## Usage
Expand Down Expand Up @@ -44,6 +45,7 @@ jobs:
| name | value | default | description |
| ---- | ----- | ------- | ----------- |
| github_token | string | `${{ github.token }}` | [GITHUB_TOKEN](https://docs.github.com/en/free-pro-team@latest/actions/reference/authentication-in-a-workflow#using-the-github_token-in-a-workflow) <br /> or a repo scoped <br /> [Personal Access Token](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token). |
| github_url | string | `${{ github.server_url }}` | Specify the GitHub Enterprise or GitHub url|
| branch | string | (default) | Destination branch to push changes. <br /> Can be passed in using `${{ github.ref }}`. |
| force | boolean | false | Determines if force push is used. |
| tags | boolean | false | Determines if `--tags` is used. |
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ inputs:
description: 'GitHub token or PAT token'
required: true
default: ${{ github.token }}
github_url:
description: 'GitHub url or GitHub Enterprise url'
required: true
default: ${{ github.server_url }}
repository:
description: 'Repository name to push. Default or empty value represents current github repository (${GITHUB_REPOSITORY})'
default: ''
Expand Down
5 changes: 4 additions & 1 deletion start.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@ const trim = (value, charlist) => trimLeft(trimRight(value, charlist));
const main = async () => {
let branch = process.env.INPUT_BRANCH;
const repository = trim(process.env.INPUT_REPOSITORY || process.env.GITHUB_REPOSITORY);
const github_url = trim(process.env.INPUT_GITHUB_URL)
if (!branch) {
const headers = {
'User-Agent': 'github.com/ad-m/github-push-action'
};
if (process.env.INPUT_GITHUB_TOKEN) headers.Authorization = `token ${process.env.INPUT_GITHUB_TOKEN}`;
const body = JSON.parse(await get(`https://api.github.com/repos/${repository}`, { headers }))
const api_url = github_url === 'github.com' ? 'api.github.com' : github_url + '/api/v3';
const body = JSON.parse(await get(`https://${api_url}/repos/${repository}`, { headers }))
branch = body.default_branch;
}
await exec('bash', [path.join(__dirname, './start.sh')], {
env: {
...process.env,
INPUT_BRANCH: branch,
INPUT_REPOSITORY: repository,
INPUT_GITHUB_URL: github_url,
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ fi

cd ${INPUT_DIRECTORY}

remote_repo="https://${GITHUB_ACTOR}:${INPUT_GITHUB_TOKEN}@github.com/${REPOSITORY}.git"
remote_repo="https://${GITHUB_ACTOR}:${INPUT_GITHUB_TOKEN}@${GITHUB_URL}/${REPOSITORY}.git"

git push "${remote_repo}" HEAD:${INPUT_BRANCH} --follow-tags $_FORCE_OPTION $_TAGS;

0 comments on commit a7824cd

Please sign in to comment.