Skip to content

Commit

Permalink
Merge pull request #10 from hubverse-org/znk/no-token
Browse files Browse the repository at this point in the history
Update workflows to be fully re-useable
  • Loading branch information
zkamvar authored Dec 23, 2024
2 parents 60fc066 + 6950655 commit 9d364f6
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 21 deletions.
49 changes: 42 additions & 7 deletions .github/workflows/generate-data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ jobs:
name: "Generate Data"
runs-on: ubuntu-latest
continue-on-error: true
strategy:
matrix:
strategy:
matrix:
site: ${{ fromJSON(inputs.repos) }}
steps:
- id: is-bot
name: "Check if we are running a bot"
run: |
if [[ "${{ secrets.id }}" = "none" ]]; then
bot=false
else
bot=true
fi
echo "bot=$bot" >> "$GITHUB_OUTPUT"
shell: bash
- id: id
name: "setup run variables"
run: |
Expand All @@ -52,6 +62,7 @@ jobs:
pip install --upgrade pip
pip install git+https://github.com/hubverse-org/hub-dashboard-predtimechart
- uses: actions/create-github-app-token@v1
if: ${{ fromJSON(steps.is-bot.outputs.bot) }}
id: token
with:
app-id: ${{ secrets.id }}
Expand All @@ -63,7 +74,7 @@ jobs:
uses: actions/checkout@v4
with:
persist-credentials: false
token: ${{ steps.token.outputs.token }}
token: ${{ steps.token.outputs.token || secrets.key }}
repository: ${{ steps.id.outputs.repo }}
sparse-checkout-cone-mode: false
sparse-checkout: |
Expand All @@ -77,11 +88,24 @@ jobs:
echo "::error title=Missing config (${{ steps.id.outputs.repo }})::No predtimechart-config.yml file found. Exiting."
exit 1
fi
- id: check-branch
name: "check for ptc/data branch"
env:
GH_TOKEN: ${{ steps.token.outputs.token || secrets.key }}
run: |
exists=$(gh api -X GET "repos/${{ steps.id.outputs.repo }}/branches" --jq '.[].name | select(. == "ptc/data")')
if [[ "$exists" = "ptc/data" ]]; then
fetch=true
else
fetch=false
fi
echo "fetch=$fetch" >> "$GITHUB_OUTPUT"
- id: checkout-data
if: ${{ fromJSON(steps.check-branch.outputs.fetch) }}
name: "Checkout ptc/data branch"
uses: actions/checkout@v4
with:
token: ${{ steps.token.outputs.token }}
token: ${{ steps.token.outputs.token || secrets.key }}
persist-credentials: false
repository: ${{ steps.id.outputs.repo }}
ref: 'ptc/data'
Expand Down Expand Up @@ -142,6 +166,16 @@ jobs:
matrix:
site: ${{ fromJSON(inputs.repos) }}
steps:
- id: is-bot
name: "Check if we are running a bot"
run: |
if [[ "${{ secrets.id }}" = "none" ]]; then
bot=false
else
bot=true
fi
echo "bot=$bot" >> "$GITHUB_OUTPUT"
shell: bash
- id: checkout-this-here-repo-scripts
uses: actions/checkout@v4
with:
Expand All @@ -157,6 +191,7 @@ jobs:
echo "repo=$owner/$name" >> $GITHUB_OUTPUT
echo "file=$owner-$name-data" >> $GITHUB_OUTPUT
- uses: actions/create-github-app-token@v1
if: ${{ fromJSON(steps.is-bot.outputs.bot) }}
id: token
with:
app-id: ${{ secrets.id }}
Expand All @@ -173,15 +208,15 @@ jobs:
"${{ steps.id.outputs.repo }}" \
"${{ inputs.slug }}" \
"${{ inputs.email }}" \
"${{ steps.token.outputs.token }}"
"${{ steps.token.outputs.token || secrets.key }}"
- id: checkout-pages
uses: actions/checkout@v4
with:
persist-credentials: false
repository: ${{ steps.id.outputs.repo }}
ref: ptc/data
path: 'data'
token: ${{ steps.token.outputs.token }}
token: ${{ steps.token.outputs.token || secrets.key }}
- id: fetch-artifact
uses: actions/download-artifact@v4
with:
Expand All @@ -195,4 +230,4 @@ jobs:
"${{ steps.id.outputs.repo }}" \
"${{ inputs.slug }}" \
"${{ inputs.email }}" \
"${{ steps.token.outputs.token }}"
"${{ steps.token.outputs.token || secrets.key }}"
37 changes: 30 additions & 7 deletions .github/workflows/generate-site.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ jobs:
volumes:
- ${{ github.workspace }}:/site
steps:
- id: is-bot
name: "Check if we are running a bot"
run: |
if [[ "${{ secrets.id }}" = "none" ]]; then
bot=false
else
bot=true
fi
echo "bot is $bot"
echo "bot=$bot" >> "$GITHUB_OUTPUT"
shell: bash
- uses: actions/create-github-app-token@v1
if: ${{ fromJSON(steps.is-bot.outputs.bot) }}
name: "Generate App Token (if no token present)"
id: token
with:
Expand All @@ -53,7 +65,7 @@ jobs:
name: "Fetch ${{ steps.id.outputs.repo }}"
uses: actions/checkout@v4
with:
token: ${{ steps.token.outputs.token }}
token: ${{ steps.token.outputs.token || secrets.key }}
persist-credentials: false
repository: ${{ steps.id.outputs.repo }}
- id: check-configs
Expand Down Expand Up @@ -81,15 +93,15 @@ jobs:
&& echo "success=true" >> $GITHUB_OUTPUT \
|| echo "success=false" >> $GITHUB_OUTPUT
- name: Upload artifact
# if: ${{ steps.build-site.outputs.success == true }}
# if: ${{ fromJSON(steps.build-site.outputs.success) }}
id: upload
uses: actions/upload-artifact@v4
with:
name: ${{ steps.id.outputs.file }}
path: '/site/pages/'
retention-days: 1
# - name: signal success
# # if: ${{ steps.build-site.outputs.success == true }}
# # if: ${{ fromJSON(steps.build-site.outputs.success) }}
# run: |
# touch "${{runner.temp}}/${{steps.id.outputs.file}}.success"
push-site:
Expand Down Expand Up @@ -122,7 +134,18 @@ jobs:
persist-credentials: false
sparse-checkout: |
scripts
- id: is-bot
name: "Check if we are running a bot"
run: |
if [[ "${{ secrets.id }}" = "none" ]]; then
bot=false
else
bot=true
fi
echo "bot=$bot" >> "$GITHUB_OUTPUT"
shell: bash
- uses: actions/create-github-app-token@v1
if: ${{ fromJSON(steps.is-bot.outputs.bot) }}
id: token
with:
app-id: ${{ secrets.id }}
Expand All @@ -132,14 +155,14 @@ jobs:
- id: check-branch
name: "check for gh-pages branch and create if needed"
env:
GH_TOKEN: ${{ github.token }}
GH_TOKEN: ${{ steps.token.outputs.token || secrets.key }}
run: |
bash -x "${{ github.workspace }}/scripts/check-branch.sh" \
"gh-pages" \
"${{ steps.id.outputs.repo }}" \
"${{ inputs.slug }}" \
"${{ inputs.email }}" \
"${{ steps.token.outputs.token }}"
"${{ steps.token.outputs.token || secrets.key }}"
- id: checkout-pages
name: "Fetch ${{ steps.id.outputs.repo }}"
uses: actions/checkout@v4
Expand All @@ -148,7 +171,7 @@ jobs:
repository: ${{ steps.id.outputs.repo }}
ref: gh-pages
path: 'pages'
token: ${{ steps.token.outputs.token }}
token: ${{ steps.token.outputs.token || secrets.key }}
- id: fetch-artifact
name: "Load changes"
uses: actions/download-artifact@v4
Expand All @@ -162,4 +185,4 @@ jobs:
"${{ steps.id.outputs.repo }}" \
"${{ inputs.slug }}" \
"${{ inputs.email }}" \
"${{ steps.token.outputs.token }}"
"${{ steps.token.outputs.token || secrets.key }}"
2 changes: 1 addition & 1 deletion .github/workflows/rebuild-data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
repos: '${{ needs.repos.outputs.repos }}'
slug: '${{ needs.repos.outputs.slug }}'
email: '${{ needs.repos.outputs.email }}'
regenerate: ${{ github.event.client_payload.regenerate }}
regenerate: ${{ github.event.client_payload.regenerate || false }}
secrets:
id: ${{ vars.APP_ID }}
key: ${{ secrets.PRIVATE_KEY }}
2 changes: 1 addition & 1 deletion appHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_slug_id():
app = ghapp["app"]
app_usr = gh.get_user(app.slug+"[bot]")
email = f'{app_usr.id}+{app_usr.login}@users.noreply.github.com'
write_string("slug", app.slug)
write_string("slug", app.slug + "[bot]")
write_string("email", email)
write_string("id", app_usr.id)

Expand Down
6 changes: 3 additions & 3 deletions scripts/check-branch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ exists=$(gh api -X GET "repos/${repo}/branches" --jq '.[].name | select(. == "${
if [[ "$exists" != "${branch}" ]]; then
tmp=$(mktemp --directory)
cd "$tmp"
git config --global user.name "${slug}[bot]"
git config --global user.name "${slug}"
git config --global user.email "${email}"
git init
git switch -c "${branch}"
git remote add origin https://${slug}[bot]:${token}@github.com/${repo}.git
git commit --allow-empty -m 'initial ${branch} commit'
git remote add origin https://${slug}:${token}@github.com/${repo}.git
git commit --allow-empty -m "initial ${branch} commit"
git push --set-upstream origin "${branch}"
cd
fi
4 changes: 2 additions & 2 deletions scripts/pushit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ else
fi

cd "$dir" || (echo "Directory '$dir' not found" && exit 1)
git config --global user.name "${slug}[bot]"
git config --global user.name "${slug}"
git config --global user.email "${email}"
git remote set-url origin "https://${slug}[bot]:${token}@github.com/${repo}.git"
git remote set-url origin "https://${slug}:${token}@github.com/${repo}.git"
ls
git status
if [[ "${amend}" == "true" ]]; then
Expand Down

0 comments on commit 9d364f6

Please sign in to comment.