cloudflare / wrangler-action

🧙‍♀️ easily deploy cloudflare workers applications using wrangler and github actions
Apache License 2.0
1.26k stars 159 forks source link

Feature request: Support GitHub deployments #274

Closed sebdanielsson closed 1 week ago

sebdanielsson commented 4 months ago

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 commented 4 months ago

adding:

    environment: <environment_name>

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

sebdanielsson commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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

hellt commented 4 months ago

@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 commented 3 months ago

When you do implement this, please don't make users pass in the GitHub token manually (https://github.com/cloudflare/pages-action/issues/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 commented 2 months ago

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/wrangler-action@v3.7.0
        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 commented 2 months ago

To build on what @dijitali shared, here's how we're creating deployments now while using this action. I posted this in https://github.com/cloudflare/pages-action/issues/117#issuecomment-2325330025 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 commented 1 month ago

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?

tnkuehne commented 1 month ago

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
        })
shahsimpson commented 1 week ago

Tracking this issue here https://github.com/cloudflare/wrangler-action/issues/301