-
-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feat]: Configure hooks directly in workflow step (i.e. without templatesync.yml
)
#467
Comments
Hi @kdeldycke and thanks for the feature request. Just some first fast thoughts: If I remember right the reason for having the file approach was the missing possibility to use objects within GitHub action input parameters. https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs If there haven't been changes lately the parameters within the Maybe 🤔 it is possible to split the current action so that you are able to execute the hooks even in another action. Not super sure if this is working (need to check how the filesystem is acting within the dockerized action) I definitely need to do some research first. |
Yeah, that might be a workaround I'm ready to explore. Like having a second step right after Here is how it would look like: steps:
- uses: actions/checkout@v4.1.1
- name: Sync from template repo
id: template_sync
uses: AndreasAugustin/actions-template-sync@v1.6.2
with:
# We need custom PAT with workflows permissions so we can update all the boilerplate .github files from
# awesome-template.
github_token: ${{ secrets.WORKFLOW_UPDATE_GITHUB_PAT || secrets.GITHUB_TOKEN }}
source_repo_path: kdeldycke/awesome-template
pr_title: "[sync] Update from `awesome-template` repository"
pr_commit_msg: "[sync] Update from `awesome-template` repository"
pr_labels: "📚 documentation"
- name: Checkout template sync PR
uses: actions/checkout@v4.1.1
with:
ref: ${{ steps.template_sync.outputs.pr_branch }}
- name: Update repo URLs
# Replace "/kdeldycke/awesome-template/" in URLs by "/kdeldycke/awesome-<repo_id>/".
run: >
find ./.github/ -iname "*.md" -or -iname "*.yaml" -exec
sed -i "s/\/kdeldycke\/awesome-template\//\/kdeldycke\/${{ github.event.repository.name }}\//g" "{}" \;
- name: Push to PR
uses: peter-evans/create-pull-request@v6.0.0
with:
assignees: ${{ github.actor }}
commit-message: "[sync] Update template URLs"
branch: ${{ steps.template_sync.outputs.pr_branch }}
So as you can see, I would need a |
Hi @kdeldycke . # using GITHUB_TOKEN here, please adjust to your needs
- name: Sync from template repo
id: template_sync
uses: AndreasAugustin/actions-template-sync@v1.7.0
env:
MY_ENV_VARIABLE_NAME: awesome-iam # is possible to be used within the templatesync.yml
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
source_repo_path: kdeldycke/awesome-template
pr_title: "[sync] Update from `awesome-template` repository"
pr_commit_msg: "[sync] Update from `awesome-template` repository"
pr_labels: "📚 documentation"
is_allow_hooks: true (still need the file) hooks:
precommit:
commands:
# Replace "/kdeldycke/awesome-template/" in URLs by "/kdeldycke/awesome-iam/"
- sed -i "s/\/kdeldycke\/awesome-template\//\/kdeldycke\/${MY_ENV_VARIABLE_NAME}\//g" ./*.md This should solve your current use case. Nevertheless I will think about the other options. Remark |
@all-contributors please add @kdeldycke for idea |
I've put up a pull request to add @kdeldycke! 🎉 |
I experimented yesterday with the new |
yes, sorry. Did not consider those 'edge-cases'. #479 ( There is likely to be a further feature (soon) #478 which will likely give some value to your workaround solution (actually that was the feature I meant with the Just out of curiosity. |
Thanks for identifying and listing the edge-case regarding And good ideas about #478. That's a powerful feature! 💪 As for the This, and I don't want to copy-n-past, keep in sync and maintain another Nth CI/CD file at the root of my repositories. My policy is: move every repository-specific config in a [^1] BTW, speaking of edge-cases, do you recognize the [^2] Another alternative would be to support remote - name: actions-template-sync
uses: AndreasAugustin/actions-template-sync@v1
with:
(...)
templatesync: https://raw.githubusercontent.com/kdeldycke/workflows/v3.0.0/templatesync.yml |
It took me a week and 45 commits, but I finally found a workaround to do what I was looking for. Tl;Dr: awesome-template-sync:
name: Sync awesome template
if: >
startsWith(github.event.repository.name, 'awesome-')
&& github.event.repository.name != 'awesome-template'
runs-on: ubuntu-22.04
# We need custom PAT through the whole job so we get workflow permissions to update all the boilerplate .github
# files from awesome-template.
steps:
- name: Initial checkout
uses: actions/checkout@v4.1.1
with:
token: ${{ secrets.WORKFLOW_UPDATE_GITHUB_PAT || secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Sync from template repo
id: template_sync
uses: AndreasAugustin/actions-template-sync@v1.8.1
with:
github_token: ${{ secrets.WORKFLOW_UPDATE_GITHUB_PAT || secrets.GITHUB_TOKEN }}
source_repo_path: kdeldycke/awesome-template
pr_title: "[sync] Updates from `awesome-template`"
pr_commit_msg: "[sync] Updates from awesome-template"
pr_branch_name_prefix: "sync-awesome-template"
pr_labels: "📚 documentation"
- name: Checkout new template sync branch
uses: actions/checkout@v4.1.1
with:
token: ${{ secrets.WORKFLOW_UPDATE_GITHUB_PAT || secrets.GITHUB_TOKEN }}
ref: ${{ steps.template_sync.outputs.pr_branch }}
fetch-depth: 0
# XXX We cannot rely on peter-evans/create-pull-request to manage the push of the new commit to the PR, as it
# ends up with a: "Unexpected error: fatal: could not open '.git/COMMIT_EDITMSG': Permission denied".
# See: https://github.com/AndreasAugustin/actions-template-sync/issues/484
# So we manage the next steps with bare git calls.
- name: Update repo URLs
# Replace "/kdeldycke/awesome-template/" in URLs by "/kdeldycke/awesome-<repo_id>/".
run: >
find ./.github/ -type f -iregex ".*\.\(md\|yaml\)$" -print -exec sed -i
"s/\/kdeldycke\/awesome-template\//\/kdeldycke\/${{ github.event.repository.name }}\//g" "{}" \;
- name: Deactivate LFS
# XXX Was introduced in https://github.com/AndreasAugustin/actions-template-sync/commit/5335a55
run: |
git config lfs.https://github.com/kdeldycke/${{ github.event.repository.name }}.git/info/lfs.locksverify false
- name: Setup Git user
run: |
git config --global user.name "${{ github.actor }}"
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
- name: Merge change to previous commit
run: |
git commit --all --amend --no-edit
- name: Push new commit to PR
run: |
git push --force This job is available at: https://github.com/kdeldycke/workflows/blob/ac65c9b5a5878d9b7c301cb5166302c147be55e3/.github/workflows/docs.yaml#L367-L419 |
Note that my workaround above is only working after a second pass. The fist pass always ends up on this error: git commit --all --amend --no-edit
shell: /usr/bin/bash -e {0}
error: insufficient permission for adding an object to repository database .git/objects
error: .github/ISSUE_TEMPLATE/new-link.yaml: failed to insert into database
error: unable to index file '.github/ISSUE_TEMPLATE/new-link.yaml'
fatal: updating files failed
Error: Process completed with exit code 128. This might be related to: #484 The full log of that workflow run is available at: https://github.com/kdeldycke/awesome-billing/actions/runs/8139058758/job/22241336465 |
hooks can be configured directly within the workflow efinition
@kdeldycke thanks for the input and super sorry for the hassle you are having getting this to work. - name: Test action step
uses: AndreasAugustin/actions-template-sync@v1
with:
source_repo_path: AndreasAugustin/template.git
upstream_branch: main
is_dry_run: true
is_allow_hooks: true
hooks: >
prepull:
commands:
- echo 'hi, we are within the prepull phase'
- echo 'maybe you want to do adjustments on the local code' Hope this is also ok for your use case. (unfortunately I did not think about this solution at first thoughts... ) |
Yes, kinda agree 👍
Thanks 😊 will try to implement this soon. Thanks btw for your found bugs. Think I need to do some investigations before fully implement the feature.
understood, but as a workaround would have possibly solve the issue faster 🥷 (but am super happy about the work and the mentioned bugs) . Agree on the bash interpolation part (YAML has a small difference within variable interpolation syntax)
you got me 🥷
Yes, nice idea. Maybe also (or in addition) git submodules is possible.
|
While the hooks are not proper YAML objects, having a way to add them directly in the action is a great progress! Thanks to this new parameter, I simplified a lot my workflow and everything is now self-contained within the Thanks a lot @AndreasAugustin for listening and considering my use-cases. |
Describe the feature
actions-template-sync
supports hooks and that's great! I'm planning to use them to run a couple ofsed
commands in theprecommit
phase, so I can adapt to the target repository some hard-coded strings from the source template repository.For this I need a
templatesync.yml
. What I'd love to have is a way to encode these rules directly on the workflow step.I.e. I'd like to write this:
And bypass entirely the creation of a
templatesync.yml
file.Use Case
I have a collection of
kdeldycke/awesome-*
repositories, in which I push content from mykdeldycke/awesome-template
repository.To do that I rely on a reused workflow, that is called in the target
awesome-*
repositories.With the current version of
actions-template-sync
, I have to add an maintain a dedicatedtemplatesync.yml
file in each target repository.But with the addition of hook rules directly in the workflow step, I can create generic
sed
commands in my reuseable workflows. That's extremely powerful to automate tedious work.Proposed Solution
Add support for the
hooks
>precommit
>commands
keywords under thewith
parameter.Notes
This is a follow up on:
Acknowledgements
The text was updated successfully, but these errors were encountered: