Skip to content

Commit 9916a27

Browse files
committed
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 7fc8079 commit 9916a27

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

.github/workflows/deploy.yml

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,50 @@ permissions:
1515

1616
jobs:
1717
deploy:
18-
if: github.event.repository.fork == false
18+
if: github.event.repository.fork == false || vars.DEPLOY_WITH_WORKFLOWS != ''
1919
environment: deploy-to-azure
2020
runs-on: ubuntu-latest
2121
steps:
2222
- uses: actions/checkout@v5
23+
- name: parse DEPLOY_WITH_WORKFLOWS
24+
if: vars.DEPLOY_WITH_WORKFLOWS != '' && contains(vars.DEPLOY_WITH_WORKFLOWS, '/')
25+
id: parsed
26+
env:
27+
WORKFLOWS_REPO: '${{ vars.DEPLOY_WITH_WORKFLOWS }}'
28+
run: |
29+
echo "owner=${WORKFLOWS_REPO%%/*}" >>$GITHUB_OUTPUT &&
30+
echo "name=${WORKFLOWS_REPO#*/}" >>$GITHUB_OUTPUT
31+
- uses: actions/create-github-app-token@v1
32+
if: steps.parsed.outputs.owner != '' && env.GITHUB_APP_ID != '' && env.GITHUB_APP_PRIVATE_KEY != ''
33+
id: workflows-repo-token
34+
env:
35+
GITHUB_APP_ID: ${{ secrets.GITGITGADGET_GITHUB_APP_ID }}
36+
GITHUB_APP_PRIVATE_KEY: ${{ secrets.GITGITGADGET_GITHUB_APP_PRIVATE_KEY }}
37+
with:
38+
app-id: ${{ secrets.GITGITGADGET_GITHUB_APP_ID }}
39+
private-key: ${{ secrets.GITGITGADGET_GITHUB_APP_PRIVATE_KEY }}
40+
owner: ${{ steps.parsed.outputs.owner }}
41+
repositories: ${{ steps.parsed.outputs.name }}
42+
- name: retrieve `vars.CONFIG` from workflows repo
43+
if: vars.DEPLOY_WITH_WORKFLOWS != ''
44+
env:
45+
WORKFLOWS_REPO: '${{ vars.DEPLOY_WITH_WORKFLOWS }}'
46+
GH_TOKEN: ${{ steps.workflows-repo-token.outputs.token || secrets.GITHUB_TOKEN }}
47+
run: |
48+
set -x &&
49+
if ! CONFIG="$(gh variable get CONFIG --repo "$WORKFLOWS_REPO")"
50+
then
51+
echo "::warning::Could not retrieve variable CONFIG from $WORKFLOWS_REPO (please configure GITHUB_APP_ID and GITHUB_APP_PRIVATE_KEY)"
52+
CONFIG="$(cat GitGitGadget/gitgitgadget-config.json)"
53+
fi &&
54+
jq '. + {
55+
"workflowsRepo": {
56+
"owner": "${{ steps.parsed.outputs.owner }}",
57+
"name": "${{ steps.parsed.outputs.name }}"
58+
}
59+
}' <<<"$CONFIG" >GitGitGadget/gitgitgadget-config.json &&
60+
echo "Using the following configuration:" &&
61+
cat GitGitGadget/gitgitgadget-config.json
2362
- name: 'Login via Azure CLI'
2463
uses: azure/login@v2
2564
with:

0 commit comments

Comments
 (0)