Skip to content

Commit 219f96d

Browse files
feat!: add token to remote URL so remote operations have auth (grafana#111)
ls-remote commands were failing if persist-credentials: false was set on actions/checkout. This change is flagged as breaking because it will use the token passed in for any remote git commands.
1 parent ebbefa8 commit 219f96d

2 files changed

Lines changed: 116 additions & 7 deletions

File tree

.github/workflows/test.yml

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,3 +766,112 @@ jobs:
766766
git push --force --delete origin "${TO_DELETE}"
767767
env:
768768
TO_DELETE: ${{ steps.setup-test-branch.outputs.branch-name }}
769+
test-persist-credentials-false-branch-on-remote:
770+
runs-on: ubuntu-latest
771+
needs: [check-not-fork]
772+
permissions:
773+
contents: write
774+
steps:
775+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
776+
with:
777+
persist-credentials: false
778+
- name: Setup test branch
779+
id: setup-test-branch
780+
run: |
781+
BRANCH_NAME="test-creds-false-branch-on-remote-$(date +%s)"
782+
783+
git config --global user.name 'github-actions[bot]'
784+
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
785+
786+
git checkout -b $BRANCH_NAME
787+
git push --set-upstream https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.git $BRANCH_NAME
788+
789+
# output status here to manually verify file changes
790+
git status --porcelain=v2 --branch --untracked-files=no
791+
792+
echo $BRANCH_NAME > "test-file.txt"
793+
echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT
794+
- uses: ./
795+
id: test-action
796+
continue-on-error: true
797+
with:
798+
token: ${{ github.token }}
799+
stage-all-files: true
800+
commit-message: ${{ steps.setup-test-branch.outputs.branch-name }}
801+
- name: Delete test branch
802+
if: ${{ always() }}
803+
run: |
804+
git push --force --delete https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.git "${TO_DELETE}"
805+
env:
806+
TO_DELETE: ${{ steps.setup-test-branch.outputs.branch-name }}
807+
- name: Check output
808+
run: |
809+
changedFilesIfAvailable=$(echo ${RESPONSE} | jq -r '.data.createCommitOnBranch.commit.changedFilesIfAvailable')
810+
811+
if [[ -z "$changedFilesIfAvailable" || "$changedFilesIfAvailable" == "null" ]]; then
812+
echo "Error: changedFilesIfAvailable is empty or null. Verify the output from test-action."
813+
exit 1
814+
fi
815+
816+
if [[ "$changedFilesIfAvailable" -ne 1 ]]; then
817+
echo "Error: changedFilesIfAvailable is expected to be 1 but got $changedFilesIfAvailable."
818+
exit 1
819+
fi
820+
821+
echo "Validation passed: changedFilesIfAvailable is $changedFilesIfAvailable."
822+
env:
823+
RESPONSE: ${{ steps.test-action.outputs.commit-response }}
824+
825+
test-persist-credentials-false-branch-not-on-remote: # make sure the action works on a clean machine without building
826+
runs-on: ubuntu-latest
827+
needs: [check-not-fork]
828+
permissions:
829+
contents: write
830+
steps:
831+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
832+
with:
833+
persist-credentials: false
834+
- name: Setup test branch
835+
id: setup-test-branch
836+
run: |
837+
BRANCH_NAME="test-creds-false-branch-not-on-remote-$(date +%s)"
838+
839+
git config --global user.name 'github-actions[bot]'
840+
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
841+
842+
git checkout -b $BRANCH_NAME
843+
844+
git status --porcelain=v2 --branch --untracked-files=no
845+
846+
echo $BRANCH_NAME > "test-file.txt"
847+
echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT
848+
- uses: ./
849+
id: test-action
850+
with:
851+
token: ${{ github.token }}
852+
stage-all-files: true
853+
create-branch-on-remote: true
854+
commit-message: ${{ steps.setup-test-branch.outputs.branch-name }}
855+
- name: Delete test branch
856+
if: ${{ always() }}
857+
run: |
858+
git push --force --delete https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.git "${TO_DELETE}"
859+
env:
860+
TO_DELETE: ${{ steps.setup-test-branch.outputs.branch-name }}
861+
- name: Check output
862+
run: |
863+
changedFilesIfAvailable=$(echo ${RESPONSE} | jq -r '.data.createCommitOnBranch.commit.changedFilesIfAvailable')
864+
865+
if [[ -z "$changedFilesIfAvailable" || "$changedFilesIfAvailable" == "null" ]]; then
866+
echo "Error: changedFilesIfAvailable is empty or null. Verify the output from test-action."
867+
exit 1
868+
fi
869+
870+
if [[ "$changedFilesIfAvailable" -ne 1 ]]; then
871+
echo "Error: changedFilesIfAvailable is expected to be 1 but got $changedFilesIfAvailable."
872+
exit 1
873+
fi
874+
875+
echo "Validation passed: changedFilesIfAvailable is $changedFilesIfAvailable."
876+
env:
877+
RESPONSE: ${{ steps.test-action.outputs.commit-response }}

action.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ runs:
4747
echo "ERROR: Invalid GitHub remote URL"
4848
exit 1
4949
fi
50-
env:
51-
GH_TOKEN: ${{ inputs.token }}
5250
5351
- name: Stage all files
5452
if: inputs.stage-all-files == 'true'
@@ -59,12 +57,14 @@ runs:
5957
if: inputs.create-branch-on-remote == 'true'
6058
shell: bash
6159
run: |
62-
onRemote=$(git ls-remote --heads origin ${BRANCH})
60+
onRemote=$(git ls-remote --heads https://x-access-token:${GH_TOKEN}@github.com/${REPO}.git ${BRANCH})
6361
if [[ -z "$onRemote" ]]; then
64-
git push --set-upstream origin ${BRANCH}
62+
git push --set-upstream https://x-access-token:${GH_TOKEN}@github.com/${REPO}.git ${BRANCH}
6563
fi
6664
env:
6765
BRANCH: ${{ steps.commit-context.outputs.branch }}
66+
REPO: ${{ steps.commit-context.outputs.repo }}
67+
GH_TOKEN: ${{ inputs.token }}
6868

6969
- name: Get file additions and deletions
7070
id: additions-and-deletions
@@ -159,12 +159,12 @@ runs:
159159
shell: bash
160160
id: commit-changes
161161
run: |
162-
branch_oid=$(git ls-remote origin refs/heads/${BRANCH} | awk '{ print $1 }')
162+
branch_oid=$(git ls-remote https://x-access-token:${GH_TOKEN}@github.com/${REPO}.git refs/heads/${BRANCH} | awk '{ print $1 }')
163163
164164
if [ -n "$branch_oid" ]; then
165165
head_oid=$branch_oid # Use remote OID of the current branch
166166
else
167-
head_oid=$(git ls-remote origin HEAD | awk '{ print $1 }') # Fallback to HEAD
167+
head_oid=$(git ls-remote https://x-access-token:${GH_TOKEN}@github.com/${REPO}.git HEAD | awk '{ print $1 }') # Fallback to HEAD
168168
fi
169169
170170
graphql_query='mutation(
@@ -205,4 +205,4 @@ runs:
205205
BRANCH: ${{ steps.commit-context.outputs.branch }}
206206
REPO: ${{ steps.commit-context.outputs.repo }}
207207
COMMIT_MESSAGE: ${{ inputs.commit-message }}
208-
TEMP_CONTENTS_DIR: ${{ steps.additions-and-deletions.outputs.contents_dir }}
208+
TEMP_CONTENTS_DIR: ${{ steps.additions-and-deletions.outputs.contents_dir }}

0 commit comments

Comments
 (0)