admin管理员组文章数量:1245899
AIM: To modify one GitHub Actions .yaml file by running another GitHub Actions file. Specifically, I want to randomize the cron schedule of the other file.
PROBLEM: It gives error
! [remote rejected] main -> main (refusing to allow a GitHub App to create or update workflow `.github/workflows/main-runner.yaml` without `workflows` permission)
What I tried:
Using a classic PAT with Workflows Permission (which is GH_CLASSIC_TOKEN in repo secrets):
Allowing Workflow Permissions in repo settings:
Deleting Repo and re-creating it.
Links to my repo:
- Seeker220/workflow-test-repo
- Failed GitHub Actions Logs
Relevant Code:
- trigger-creator.yaml
name: Trigger Creator
on:
workflow_dispatch: # Allows manual triggering of the workflow
jobs:
update-cron:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Enable Main Workflow
run: gh workflow enable main-runner.yaml
env:
GH_TOKEN: ${{ secrets.GH_CLASSIC_TOKEN }}
- name: Generate Randomized Cron Time
id: random-cron
run: |
# Generate a random hour between 0 and 23
NEW_HOUR=$((RANDOM % 24))
# Generate a random minute between 0 and 59
NEW_MINUTE=$((RANDOM % 60))
# Create new cron expression
NEW_CRON="$NEW_MINUTE $NEW_HOUR * * *"
echo "NEW_CRON=$NEW_CRON" >> $GITHUB_ENV
- name: Update `main-runner.yaml` with New Cron
run: |
# Replace the old cron with the new one in main-runner.yaml
sed -i "s/^ - cron: \".*\"/ - cron: \"${NEW_CRON}\"/" .github/workflows/main-runner.yaml
- name: Commit and Push Changes
run: |
git config --global user.name "github-actions"
git config --global user.email "[email protected]"
git remote set-url origin https://x-access-token:${{ secrets.GH_CLASSIC_TOKEN }}@github/${{ github.repository }}.git
git add .github/workflows/main-runner.yaml
git commit -m "Update main-runner cron to: ${NEW_CRON}"
git push origin main
env:
GH_CLASSIC_TOKEN: ${{ secrets.GH_CLASSIC_TOKEN }}
- main-runner.yaml
name: Main Runner
on:
schedule:
- cron: "0 15 * * *" # Placeholder cron, gets updated dynamically
jobs:
run-task:
runs-on: ubuntu-latest
steps:
- name: Run Sample Command
run: echo "Hello from Main Runner"
- name: Disable Itself After Running
run: gh workflow disable main-runner.yaml
env:
GH_TOKEN: ${{ secrets.GH_CLASSIC_TOKEN }}
So far, these didn't solve the issue. I have found similar questions on SE, in most of them, OP was using a GITHUB_TOKEN instead of PAT.
Thanks :)
AIM: To modify one GitHub Actions .yaml file by running another GitHub Actions file. Specifically, I want to randomize the cron schedule of the other file.
PROBLEM: It gives error
! [remote rejected] main -> main (refusing to allow a GitHub App to create or update workflow `.github/workflows/main-runner.yaml` without `workflows` permission)
What I tried:
Using a classic PAT with Workflows Permission (which is GH_CLASSIC_TOKEN in repo secrets):
Allowing Workflow Permissions in repo settings:
Deleting Repo and re-creating it.
Links to my repo:
- Seeker220/workflow-test-repo
- Failed GitHub Actions Logs
Relevant Code:
- trigger-creator.yaml
name: Trigger Creator
on:
workflow_dispatch: # Allows manual triggering of the workflow
jobs:
update-cron:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Enable Main Workflow
run: gh workflow enable main-runner.yaml
env:
GH_TOKEN: ${{ secrets.GH_CLASSIC_TOKEN }}
- name: Generate Randomized Cron Time
id: random-cron
run: |
# Generate a random hour between 0 and 23
NEW_HOUR=$((RANDOM % 24))
# Generate a random minute between 0 and 59
NEW_MINUTE=$((RANDOM % 60))
# Create new cron expression
NEW_CRON="$NEW_MINUTE $NEW_HOUR * * *"
echo "NEW_CRON=$NEW_CRON" >> $GITHUB_ENV
- name: Update `main-runner.yaml` with New Cron
run: |
# Replace the old cron with the new one in main-runner.yaml
sed -i "s/^ - cron: \".*\"/ - cron: \"${NEW_CRON}\"/" .github/workflows/main-runner.yaml
- name: Commit and Push Changes
run: |
git config --global user.name "github-actions"
git config --global user.email "[email protected]"
git remote set-url origin https://x-access-token:${{ secrets.GH_CLASSIC_TOKEN }}@github/${{ github.repository }}.git
git add .github/workflows/main-runner.yaml
git commit -m "Update main-runner cron to: ${NEW_CRON}"
git push origin main
env:
GH_CLASSIC_TOKEN: ${{ secrets.GH_CLASSIC_TOKEN }}
- main-runner.yaml
name: Main Runner
on:
schedule:
- cron: "0 15 * * *" # Placeholder cron, gets updated dynamically
jobs:
run-task:
runs-on: ubuntu-latest
steps:
- name: Run Sample Command
run: echo "Hello from Main Runner"
- name: Disable Itself After Running
run: gh workflow disable main-runner.yaml
env:
GH_TOKEN: ${{ secrets.GH_CLASSIC_TOKEN }}
So far, these didn't solve the issue. I have found similar questions on SE, in most of them, OP was using a GITHUB_TOKEN instead of PAT.
Thanks :)
Share Improve this question asked Feb 16 at 8:53 Asmit KarmakarAsmit Karmakar 13910 bronze badges1 Answer
Reset to default 0You have to tell git
commands to use your generated token. You can do so via the checkout
actions at the top of you workflow and provide the token
there (see here for docs example, omitted rest of your file for brevity):
...
jobs:
update-cron:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GH_CLASSIC_TOKEN }}
...
And afterward, you should be able to push
with this token if you only define user.name
and user.email
via git config
(once again irrelevant parts omitted):
...
jobs:
update-cron:
runs-on: ubuntu-latest
steps:
...
- name: Commit and Push Changes
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "[email protected]"
git add .github/workflows/main-runner.yaml
git commit -m "Update main-runner cron to: ${NEW_CRON}"
git push origin main
Also, please note:
- Changed the
email
fromgithub-actions
togithub-actions[bot]
as that will register as GitHub Actions bot in the GitHub GUI (which I assume you want?) - You should use fine-grained tokens with minimal scope (
contents: write
andworkflows: write
should do I think), read more about them here (you could do this after the initial testing that this works with personal token) as this approach is more secure
本文标签: GitHub PAT cannot modify workflow filesStack Overflow
版权声明:本文标题:GitHub PAT cannot modify workflow files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740212477a2242259.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论