|
| 1 | +name: Deploy |
| 2 | +on: |
| 3 | + workflow_run: |
| 4 | + workflows: ['Build'] |
| 5 | + types: |
| 6 | + - completed |
| 7 | + |
| 8 | +jobs: |
| 9 | + checks: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + outputs: |
| 12 | + parameters: ${{ steps.parameters.outputs.result }} |
| 13 | + steps: |
| 14 | + - if: ${{ github.event.workflow_run.conclusion == 'failure' }} |
| 15 | + run: echo 'The triggering workflow failed' && exit 1 |
| 16 | + |
| 17 | + - name: Determine deploy parameters |
| 18 | + id: parameters |
| 19 | + uses: actions/github-script@v7 |
| 20 | + with: |
| 21 | + script: | |
| 22 | + const eventType = context.payload.workflow_run.event; |
| 23 | + const isFork = context.payload.workflow_run.repository.fork; |
| 24 | +
|
| 25 | + let parameters; |
| 26 | +
|
| 27 | + console.log({eventType, isFork}); |
| 28 | +
|
| 29 | + if (eventType == "push") { |
| 30 | + const branch = context.payload.workflow_run.head_branch; |
| 31 | + console.log({branch}); |
| 32 | + const shouldDeploy = !isFork && branch == "main"; |
| 33 | + parameters = { |
| 34 | + event: "branch", |
| 35 | + name: "main", |
| 36 | + shouldDeploy |
| 37 | + }; |
| 38 | + } else if (eventType == "pull_request") { |
| 39 | + let pull_number = context.payload.workflow_run.pull_requests[0]?.number; |
| 40 | + if(!pull_number) { |
| 41 | + const response = await github.rest.search.issuesAndPullRequests({q: 'repo:${{ github.repository }} is:pr sha:${{ github.event.workflow_run.head_sha }}',per_page: 1,}) |
| 42 | + const items = response.data.items |
| 43 | + if (items.length < 1) { |
| 44 | + throw new Error("No pull request found for the commit") |
| 45 | + } |
| 46 | + const pullRequestNumber = items[0].number |
| 47 | + console.info("Pull request number is", pullRequestNumber) |
| 48 | + pull_number = pullRequestNumber |
| 49 | + } |
| 50 | + const {data: pr} = await github.rest.pulls.get({ |
| 51 | + owner: context.repo.owner, |
| 52 | + repo: context.repo.repo, |
| 53 | + pull_number |
| 54 | + }); |
| 55 | +
|
| 56 | + console.log({pull_number}); |
| 57 | +
|
| 58 | + parameters = { |
| 59 | + event: "pr", |
| 60 | + name: `pr-${pull_number}`, |
| 61 | + pr_number: pull_number, |
| 62 | + shouldDeploy: true |
| 63 | + }; |
| 64 | + } else if (eventType == "release") { |
| 65 | + parameters = { |
| 66 | + event: "release", |
| 67 | + name: context.payload.workflow_run.head_branch, |
| 68 | + shouldDeploy: !isFork |
| 69 | + }; |
| 70 | + } |
| 71 | +
|
| 72 | + console.log(parameters); |
| 73 | + return parameters; |
| 74 | +
|
| 75 | + deploy: |
| 76 | + runs-on: ubuntu-latest |
| 77 | + needs: checks |
| 78 | + if: ${{ fromJson(needs.checks.outputs.parameters).shouldDeploy }} |
| 79 | + steps: |
| 80 | + - name: Checkout code |
| 81 | + uses: actions/checkout@v4 |
| 82 | + |
| 83 | + - name: Load parameters |
| 84 | + id: parameters |
| 85 | + uses: actions/github-script@v7 |
| 86 | + with: |
| 87 | + script: | |
| 88 | + const json = `${{ needs.checks.outputs.parameters }}`; |
| 89 | + const parameters = JSON.parse(json); |
| 90 | + core.setOutput("event", parameters.event); |
| 91 | + core.setOutput("name", parameters.name); |
| 92 | + core.setOutput("shouldDeploy", parameters.shouldDeploy); |
| 93 | +
|
| 94 | + - run: | |
| 95 | + echo "Starting docs deployment for ${{ steps.parameters.outputs.event }} ${{ steps.parameters.outputs.name }}" |
| 96 | +
|
| 97 | + - name: Download artifact |
| 98 | + uses: actions/github-script@v7 |
| 99 | + with: |
| 100 | + script: | |
| 101 | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 102 | + owner: context.repo.owner, |
| 103 | + repo: context.repo.repo, |
| 104 | + run_id: context.payload.workflow_run.id, |
| 105 | + }); |
| 106 | + let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { |
| 107 | + return artifact.name == "build-output" |
| 108 | + })[0]; |
| 109 | + let download = await github.rest.actions.downloadArtifact({ |
| 110 | + owner: context.repo.owner, |
| 111 | + repo: context.repo.repo, |
| 112 | + artifact_id: matchArtifact.id, |
| 113 | + archive_format: 'zip', |
| 114 | + }); |
| 115 | + let fs = require('fs'); |
| 116 | + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/build-output.zip`, Buffer.from(download.data)); |
| 117 | +
|
| 118 | + - name: Unzip artifact |
| 119 | + run: unzip "${{ github.workspace }}/build-output.zip" -d "${{ github.workspace }}/build" |
| 120 | + |
| 121 | + # - name: Deploy Subdomain |
| 122 | + # env: |
| 123 | + # TF_VAR_prefix_name: ${{ steps.parameters.outputs.name}} |
| 124 | + # TF_VAR_prefix_event_type: ${{ steps.parameters.outputs.event }} |
| 125 | + # CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 126 | + # CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 127 | + # TF_STATE_POSTGRES_CONN_STR: ${{ secrets.TF_STATE_POSTGRES_CONN_STR }} |
| 128 | + # uses: gruntwork-io/terragrunt-action@v2 |
| 129 | + # with: |
| 130 | + # tg_version: '0.58.12' |
| 131 | + # tofu_version: '1.7.1' |
| 132 | + # tg_dir: 'deployment/modules/cloudflare/docs' |
| 133 | + # tg_command: 'apply' |
| 134 | + |
| 135 | + # - name: Deploy Docs Subdomain Output |
| 136 | + # id: docs-output |
| 137 | + # env: |
| 138 | + # TF_VAR_prefix_name: ${{ steps.parameters.outputs.name}} |
| 139 | + # TF_VAR_prefix_event_type: ${{ steps.parameters.outputs.event }} |
| 140 | + # CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 141 | + # CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 142 | + # TF_STATE_POSTGRES_CONN_STR: ${{ secrets.TF_STATE_POSTGRES_CONN_STR }} |
| 143 | + # uses: gruntwork-io/terragrunt-action@v2 |
| 144 | + # with: |
| 145 | + # tg_version: '0.58.12' |
| 146 | + # tofu_version: '1.7.1' |
| 147 | + # tg_dir: 'deployment/modules/cloudflare/docs' |
| 148 | + # tg_command: 'output -json' |
| 149 | + |
| 150 | + # - name: Output Cleaning |
| 151 | + # id: clean |
| 152 | + # run: | |
| 153 | + # TG_OUT=$(echo '${{ steps.docs-output.outputs.tg_action_output }}' | sed 's|%0A|\n|g ; s|%3C|<|g' | jq -c .) |
| 154 | + # echo "output=$TG_OUT" >> $GITHUB_OUTPUT |
| 155 | + |
| 156 | + # - name: Publish to Cloudflare Pages |
| 157 | + # uses: cloudflare/pages-action@v1 |
| 158 | + # with: |
| 159 | + # apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN_PAGES_UPLOAD }} |
| 160 | + # accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 161 | + # projectName: ${{ fromJson(steps.clean.outputs.output).pages_project_name.value }} |
| 162 | + # workingDirectory: 'docs' |
| 163 | + # directory: 'build' |
| 164 | + # branch: ${{ steps.parameters.outputs.name }} |
| 165 | + # wranglerVersion: '3' |
| 166 | + |
| 167 | + # - name: Deploy Docs Release Domain |
| 168 | + # if: ${{ steps.parameters.outputs.event == 'release' }} |
| 169 | + # env: |
| 170 | + # TF_VAR_prefix_name: ${{ steps.parameters.outputs.name}} |
| 171 | + # CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 172 | + # CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 173 | + # TF_STATE_POSTGRES_CONN_STR: ${{ secrets.TF_STATE_POSTGRES_CONN_STR }} |
| 174 | + # uses: gruntwork-io/terragrunt-action@v2 |
| 175 | + # with: |
| 176 | + # tg_version: '0.58.12' |
| 177 | + # tofu_version: '1.7.1' |
| 178 | + # tg_dir: 'deployment/modules/cloudflare/docs-release' |
| 179 | + # tg_command: 'apply' |
| 180 | + |
| 181 | + # - name: Comment |
| 182 | + # uses: actions-cool/maintain-one-comment@v3 |
| 183 | + # if: ${{ steps.parameters.outputs.event == 'pr' }} |
| 184 | + # with: |
| 185 | + # number: ${{ fromJson(needs.checks.outputs.parameters).pr_number }} |
| 186 | + # body: | |
| 187 | + # 📖 Documentation deployed to [${{ fromJson(steps.clean.outputs.output).immich_app_branch_subdomain.value }}](https://${{ fromJson(steps.clean.outputs.output).immich_app_branch_subdomain.value }}) |
| 188 | + # emojis: 'rocket' |
| 189 | + # body-include: '<!-- Docs PR URL -->' |
0 commit comments