Skip to content

Commit d649ada

Browse files
fix: support GH_TOKEN to override GITHUB_TOKEN (#307)
Co-authored-by: Duncan MacQuarrie <5956299+3dbrows@users.noreply.github.com>
1 parent 00a065d commit d649ada

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

docs/EXAMPLES.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ jobs:
131131
132132
The Action/CLI will automatically detect the [environment variables](https://crowdin.github.io/crowdin-cli/configuration#environment-variables) and use them for the configuration.
133133
134-
> **Note**
134+
> [!NOTE]
135135
> To avoid any conflicts, do not use the `crowdin.yml` file in the repository when using the above configuration approach.
136136

137137
### Upload sources only
@@ -195,7 +195,7 @@ Note that the value of the `crowdin_branch_name` is `env.BRANCH_NAME` - this is
195195

196196
The Crowdin CLI supports a `--cache` parameter for the `upload sources` command that stores source file checksums locally. This allows the CLI to only upload files that have changed, significantly reducing upload time for large projects.
197197

198-
> **Note**
198+
> [!NOTE]
199199
> The cache feature is experimental. For any feedback, visit [Crowdin CLI Discussions](https://github.com/crowdin/crowdin-cli/discussions).
200200

201201
To persist the cache between workflow runs, use the `actions/cache` action to save and restore the cache file located at `~/.crowdin/cache.json`:
@@ -302,7 +302,7 @@ jobs:
302302
CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}
303303
```
304304

305-
> **Note**
305+
> [!NOTE]
306306
> If you are using a **String-based** project, you need to use this option to download translations. The default `download_translations` option does not work for this type of projects.
307307

308308
The `download_bundle` option accepts the bundle numeric ID.
@@ -569,6 +569,8 @@ jobs:
569569
steps:
570570
- name: Checkout
571571
uses: actions/checkout@v4
572+
with:
573+
persist-credentials: false
572574
573575
- name: Generate GitHub App Token
574576
id: generate-token
@@ -599,6 +601,9 @@ To set this up:
599601

600602
For more details, see [GitHub issue #270](https://github.com/crowdin/github-action/issues/270).
601603

604+
> [!NOTE]
605+
> When using a GitHub App token via `GH_TOKEN`, the Checkout step must use `persist-credentials: false`. Otherwise, the default `GITHUB_TOKEN` cached in `.git/config` by `actions/checkout` takes precedence over `GH_TOKEN`, and the action will not use your App token for creating pull requests.
606+
602607
### Checking the translation progress
603608

604609
```yaml

entrypoint.sh

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,17 @@ download_translations() {
7979
}
8080

8181
create_pull_request() {
82-
BRANCH="${1}"
82+
[ -z "${GITHUB_TOKEN}" ] && [ -z "${GH_TOKEN}" ] && {
83+
echo "ERROR: Either 'GITHUB_TOKEN' or 'GH_TOKEN' must be set in the environment variables!"
84+
exit 1
85+
}
8386

84-
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
87+
AUTH_TOKEN="${GITHUB_TOKEN}"
88+
if [ -n "${GH_TOKEN}" ]; then
89+
AUTH_TOKEN="${GH_TOKEN}"
90+
fi
91+
92+
AUTH_HEADER="Authorization: token ${AUTH_TOKEN}"
8593
HEADER="Accept: application/vnd.github.v3+json; application/vnd.github.antiope-preview+json; application/vnd.github.shadow-cat-preview+json"
8694

8795
if [ -n "$INPUT_GITHUB_API_BASE_URL" ]; then
@@ -95,14 +103,16 @@ create_pull_request() {
95103

96104
auth_status=$(curl -sL --write-out '%{http_code}' --output /dev/null -H "${AUTH_HEADER}" -H "${HEADER}" "${PULLS_URL}")
97105
if [[ $auth_status -eq 403 || "$auth_status" -eq 401 ]] ; then
98-
echo "FAILED TO AUTHENTICATE USING 'GITHUB_TOKEN' CHECK TOKEN IS VALID"
106+
echo "FAILED TO AUTHENTICATE WITH GITHUB. PLEASE CHECK YOUR GITHUB TOKEN"
99107
echo "pull_request_url=" >> $GITHUB_OUTPUT
100108
echo "pull_request_number=" >> $GITHUB_OUTPUT
101109
exit 1
102110
fi
103111

104112
echo "CHECK IF PULL REQUEST ALREADY EXIST"
105113

114+
BRANCH="${1}"
115+
106116
if [ -n "$INPUT_PULL_REQUEST_BASE_BRANCH_NAME" ]; then
107117
BASE_BRANCH="$INPUT_PULL_REQUEST_BASE_BRANCH_NAME"
108118
else
@@ -233,9 +243,18 @@ create_pull_request() {
233243
}
234244

235245
push_to_branch() {
236-
BRANCH=${INPUT_LOCALIZATION_BRANCH_NAME}
246+
[ -z "${GITHUB_TOKEN}" ] && [ -z "${GH_TOKEN}" ] && {
247+
echo "ERROR: Either 'GITHUB_TOKEN' or 'GH_TOKEN' must be set in the environment variables!"
248+
exit 1
249+
}
237250

238-
REPO_URL="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${INPUT_GITHUB_BASE_URL}/${GITHUB_REPOSITORY}.git"
251+
AUTH_TOKEN="${GITHUB_TOKEN}"
252+
if [ -n "${GH_TOKEN}" ]; then
253+
AUTH_TOKEN="${GH_TOKEN}"
254+
fi
255+
256+
BRANCH=${INPUT_LOCALIZATION_BRANCH_NAME}
257+
REPO_URL="https://${GITHUB_ACTOR}:${AUTH_TOKEN}@${INPUT_GITHUB_BASE_URL}/${GITHUB_REPOSITORY}.git"
239258

240259
echo "CONFIGURING GIT USER"
241260
git config --global user.email "${INPUT_GITHUB_USER_EMAIL}"
@@ -389,11 +408,6 @@ if [ "$INPUT_DOWNLOAD_SOURCES" = true ]; then
389408
download_sources "$@"
390409

391410
if [ "$INPUT_PUSH_SOURCES" = true ]; then
392-
[ -z "${GITHUB_TOKEN}" ] && {
393-
echo "CAN NOT FIND 'GITHUB_TOKEN' IN ENVIRONMENT VARIABLES"
394-
exit 1
395-
}
396-
397411
[ -n "${INPUT_GPG_PRIVATE_KEY}" ] && {
398412
setup_commit_signing
399413
}
@@ -406,11 +420,6 @@ if [ "$INPUT_DOWNLOAD_TRANSLATIONS" = true ]; then
406420
download_translations "$@"
407421

408422
if [ "$INPUT_PUSH_TRANSLATIONS" = true ]; then
409-
[ -z "${GITHUB_TOKEN}" ] && {
410-
echo "CAN NOT FIND 'GITHUB_TOKEN' IN ENVIRONMENT VARIABLES"
411-
exit 1
412-
}
413-
414423
[ -n "${INPUT_GPG_PRIVATE_KEY}" ] && {
415424
setup_commit_signing
416425
}
@@ -425,11 +434,6 @@ if [ "$INPUT_DOWNLOAD_BUNDLE" ]; then
425434
crowdin bundle download $INPUT_DOWNLOAD_BUNDLE $DOWNLOAD_BUNDLE_ARGS
426435

427436
if [ "$INPUT_PUSH_TRANSLATIONS" = true ]; then
428-
[ -z "${GITHUB_TOKEN}" ] && {
429-
echo "CAN NOT FIND 'GITHUB_TOKEN' IN ENVIRONMENT VARIABLES"
430-
exit 1
431-
}
432-
433437
[ -n "${INPUT_GPG_PRIVATE_KEY}" ] && {
434438
setup_commit_signing
435439
}

0 commit comments

Comments
 (0)