Skip to content

Commit c4e401d

Browse files
committed
amend! deploy: allow deploying in forks
deploy: allow deploying in forks It would be nice if we could deploy the Azure Function contingent on the presence of the `AZURE_CLIENT_ID` secret. However, this is not possible in GitHub workflows: the job-level `if:` conditions lack access to the `secrets` context. Strangely enough, they _do_ have access to the `vars` context... To successfully deploy the Azure Function, it needs to know which `gitgitgadget-workflows` fork to target when triggering workflow runs, anyway, so let's _require_ a repository variable called `DEPLOY_WITH_WORKFLOWS` that specifies that fork in the form `<org>/gitgitgadget-workflows`. Note that such a fork _must_ have the `CONFIG` repository variable that contains the corresponding project configuration; The `deploy` workflow will retrieve this configuration and overwrite `gitgitgadget-config.json` with it, augmenting the `workflowsRepo` information on the fly. Also note: In order to read that `CONFIG` repository variable (which for some unfathomable reason cannot be read via the regular `GITHUB_TOKEN` available in GitHub workflows...), the GitHub App needs to be installed on that repository and configured in this here repository via the `GITGITGADGET_GITHUB_APP_ID` and `GITGITGADGET_GITHUB_APP_PRIVATE_KEY` secrets. This poses a bit of a Catch-22 because the Azure Function is expected to already be deployed when the GitHub App is registered, but the workflow should be used for the initial deployment, too. To accommodate for that, instead of erroring out, the workflow will merely warn when the repository variable cannot be read, and continue with the default configuration instead. Those secrets should be defined directly after registering the GitHub App. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 386e34d commit c4e401d

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

.github/workflows/deploy.yml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,38 @@ jobs:
2121
runs-on: ubuntu-latest
2222
steps:
2323
- uses: actions/checkout@v5
24+
- name: parse DEPLOY_WITH_WORKFLOWS
25+
if: vars.DEPLOY_WITH_WORKFLOWS != '' && contains(vars.DEPLOY_WITH_WORKFLOWS, '/')
26+
id: parsed
27+
run: |
28+
echo "owner=${WORKFLOWS_REPO%%/*}" >>$GITHUB_OUTPUT &&
29+
echo "name="${WORKFLOWS_REPO#*/}" >>$GITHUB_OUTPUT
30+
- uses: actions/create-github-app-token@v1
31+
if: steps.parsed.outputs.owner != '' && env.GITHUB_APP_ID != '' && env.GITHUB_APP_PRIVATE_KEY != ''
32+
id: workflows-repo-token
33+
with:
34+
app-id: ${{ secrets.GITGITGADGET_GITHUB_APP_ID }}
35+
private-key: ${{ secrets.GITGITGADGET_GITHUB_APP_PRIVATE_KEY }}
36+
owner: ${{ steps.parsed.outputs.owner }}
37+
repositories: ${{ steps.parsed.outputs.name }}
2438
- name: retrieve `vars.CONFIG` from workflows repo
2539
if: vars.DEPLOY_WITH_WORKFLOWS != ''
2640
env:
2741
WORKFLOWS_REPO: '${{ vars.DEPLOY_WITH_WORKFLOWS }}'
28-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42+
GH_TOKEN: ${{ steps.workflows-repo-token.outputs.token || secrets.GITHUB_TOKEN }}
2943
run: |
3044
set -x &&
31-
WORKFLOWS_OWNER="${WORKFLOWS_REPO%%/*}" &&
32-
WORKFLOWS_NAME="${WORKFLOWS_REPO#*/}" &&
33-
CONFIG="$(gh variable get CONFIG --repo "$WORKFLOWS_REPO")" &&
34-
jq '. + { "workflowsRepo": { "owner": "'"$WORKFLOWS_OWNER"'", "name": "'"$WORKFLOWS_NAME"'" } }' \
35-
<<<"$CONFIG" >GitGitGadget/gitgitgadget-config.json &&
45+
if ! CONFIG="$(gh variable get CONFIG --repo "$WORKFLOWS_REPO")"
46+
then
47+
echo "::warning::Could not retrieve variable CONFIG from $WORKFLOWS_REPO (please configure GITHUB_APP_ID and GITHUB_APP_PRIVATE_KEY)"
48+
CONFIG="$(cat GitGitGadget/gitgitgadget-config.json)"
49+
fi &&
50+
jq '. + {
51+
"workflowsRepo": {
52+
"owner": "${{ steps.parsed.outputs.owner }}",
53+
"name": "${{ steps.parsed.outputs.name }}"
54+
}
55+
}' <<<"$CONFIG" >GitGitGadget/gitgitgadget-config.json &&
3656
echo "Using the following configuration:" &&
3757
cat GitGitGadget/gitgitgadget-config.json
3858
- name: 'Login via Azure CLI'

0 commit comments

Comments
 (0)