Closed sebdanielsson closed 1 week 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.
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?
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 =)
What output exactly is needed to support GitHub Deployments? Does the deployment-url output work for previews?
@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
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:
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.
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
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"
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?
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
})
Tracking this issue here https://github.com/cloudflare/wrangler-action/issues/301
I'm coming from the deprecated pages-action and one feature I really miss is being able to update the GitHub deployments environment.