Skip to content
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

Feature request: Support GitHub deployments #274

Open
sebdanielsson opened this issue Jul 4, 2024 · 11 comments
Open

Feature request: Support GitHub deployments #274

sebdanielsson opened this issue Jul 4, 2024 · 11 comments

Comments

@sebdanielsson
Copy link

I'm coming from the deprecated pages-action and one feature I really miss is being able to update the GitHub deployments environment.

@Hugo-C
Copy link

Hugo-C commented Jul 6, 2024

adding:

    environment: <environment_name>

works for me here, is it what you are looking for ?

@sebdanielsson
Copy link
Author

sebdanielsson commented Jul 6, 2024

adding:

    environment: <environment_name>

works for me here, is it what you are looking for ?

My understanding is that this is for choosing which Cloudflare Pages environment to deploy to, not the GitHub environment for deployments.

https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment

@TheAifam5
Copy link

TheAifam5 commented Jul 7, 2024

I have the same problem, the pages-action did set the proper url for preview/release deployments. With wrangler-actions I could for release deployment hardcode it but how do I set the url when deploying preview?

@hellt
Copy link

hellt commented Jul 25, 2024

I would love to have this as well.

And also the preview aliases are not set to the git branch for preview branches. It would be great to have them back =)

@nprogers
Copy link

What output exactly is needed to support GitHub Deployments? Does the deployment-url output work for previews?

@hellt
Copy link

hellt commented Jul 25, 2024

@nprogers ideally I'd love to have the same functionality as the CF-Pages+GitHub interfation provides via a bot leaving a comment with all the required information

image

if this is not possible, then at least the gh api to be triggered to show the deployment environment with the preview URL as the current pages-action does:

image

@aaronadamsCA
Copy link

When you do implement this, please don't make users pass in the GitHub token manually (cloudflare/pages-action#69). Actions can always access github.token, so this should really be a boolean flag for clarity.

Also please keep bot comments separate from deployments. I do not want a bot leaving a comment on our PRs! I only want the deployments.

@dijitali
Copy link

dijitali commented Sep 10, 2024

I'm using the below as a workaround (basically grab the deployment-url output from Wrangler and write it as a comment with the GitHub Script Action):

on: [push]

...

      - name: Publish to Cloudflare Pages
        id: deploy
        uses: cloudflare/[email protected]
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
          command: pages deploy . --project-name=${{ vars.CLOUDFLARE_PROJECT_NAME }} --commit-dirty=true
      - uses: actions/github-script@v7
        with:
          script: |
            const issue_number = (await github.rest.repos.listPullRequestsAssociatedWithCommit({
                  commit_sha: context.sha,
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                })
              ).data[0].number;
            github.rest.issues.createComment({
              issue_number: issue_number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: '🚢 deployed to <${{ steps.deploy.outputs.deployment-url }}>'
            })

EDIT: Probably pretty easy to use the createDeployment API also

@aaronadamsCA
Copy link

aaronadamsCA commented Sep 10, 2024

To build on what @dijitali shared, here's how we're creating deployments now while using this action. I posted this in cloudflare/pages-action#117 (comment) but it's a lot more relevant here!

The job steps below assume you already have a wrangler.toml file, otherwise you'll need to pass more arguments to the pages deploy command. You'll need to replace matrix.name and matrix.directory with your own values.

    steps:

      # Do stuff

      - name: Create GitHub deployment
        id: github_deployment
        env:
          GH_TOKEN: "${{ github.token }}"
        run: |
          id=$(gh api "repos/{owner}/{repo}/deployments" \
            -f "ref=${{ github.ref_name }}" \
            -F "auto_merge=false" \
            -F "required_contexts[]" \
            -f "environment=${{ matrix.name }} (${{ github.ref_name == 'main' && 'production' || 'preview' }})" \
            -F "production_environment=${{ github.ref_name == 'main' && 'true' || 'false' }}" \
            -q ".id"
          )
          echo "id=${id?}" >> "$GITHUB_OUTPUT"

      # Do stuff

      - name: Deploy to Cloudflare Pages
        id: deploy
        uses: cloudflare/wrangler-action@9681c2997648301493e78cacbfb790a9f19c833f # v3.9.0
        with:
          accountId: "${{ vars.CLOUDFLARE_ACCOUNT_ID }}"
          apiToken: "${{ secrets.CLOUDFLARE_API_TOKEN }}"
          command: pages deploy
          workingDirectory: "${{ matrix.directory }}"

      # Do stuff

      - name: Create GitHub deployment status
        if: always() && steps.github_deployment.outcome == 'success'
        env:
          GH_TOKEN: "${{ github.token }}"
        run: |
          gh api "repos/{owner}/{repo}/deployments/${{ steps.github_deployment.outputs.id }}/statuses" \
            -f "state=${{ steps.deploy.outcome == 'success' && 'success' || 'failure' }}" \
            -f "environment_url=${{ steps.deploy.outputs.deployment-url }}" \
            -F "auto_inactive=false"

@tunnckoCore
Copy link

tunnckoCore commented Sep 30, 2024

My problem is figuring out a way to use the commit sha (eg. github.sha) in the workers code as sort of verification/provenance which exact commit is deployed.

Ideas? Will just setting through env going to work? (going to try now) like

env:
  GH_COMMIT_SHA: ${{ github.sha }}

and then just use GH_COMMIT_SHA in the worker?

@H3nkl3r
Copy link

H3nkl3r commented Oct 13, 2024

I solved it like this:

 - uses: actions/github-script@v7
    id: createDeployment
    with:
      script: |
        const ref = `${{ github.head_ref }}` || `${{ github.ref_name }}` || context.ref;
        const result = await github.rest.repos.createDeployment({
          owner: context.repo.owner,
          repo: context.repo.repo,
          ref: ref,
          auto_merge: false,
          required_contexts: [],
          environment: context.ref === 'refs/heads/main' ? 'hansdiether (Production)' : 'hansdiether (Preview)',
          production_environment: context.ref === 'refs/heads/main'
        })
        return result.data.id

  - uses: actions/github-script@v7
    with:
      script: |
        github.rest.repos.createDeploymentStatus({
          owner: context.repo.owner,
          repo: context.repo.repo,
          deployment_id: `${{ steps.createDeployment.outputs.result }}`,
          environment: context.ref === 'refs/heads/main' ? 'hansdiether (Production)' : 'hansdiether (Preview)',
          environment_url: `${{ steps.deploy.outputs.deployment-url }}`,
          state: 'success',
          auto_inactive: false
        })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants