How to setup Wrangler in GitLab CI/CD for Gatsby project?

Hello :wave:t3:

I have Gatsby project, I published on Pages on Cloudflare using Wrangler Cli from local machine

Now I want to do it automatically from GitLab CI.

I added the code in .gitlab-ci.yml

frontendjob:
  image: node:latest
  stage: deploy
  script:
    - cd frontend/
    - echo "front end job works"
    - npm install
    - npm i @cloudflare/wrangler -g
    - wrangler publish

It doesn’t work.

Please help to fix this

You need v2 wrangler not v1, also the command is pages publish

frontendjob:
  image: node:latest
  stage: deploy
  script:
    - cd frontend/
    - echo "front end job works"
    - npm install
    - npm i wrangler -g
    - wrangler pages publish <output-dir>

I’d recommend installing wrangler as a local dev dep though rather than always installing globally. This way you’re fixed to a single version, it’s the same as you use locally and local deps are generally just preferred for this :slight_smile:

1 Like

Thank you @Walshy
I will try that
Can you please tell me how auth will work here?

You’d need to pass through the API token and account ID as env vars - CLOUDFLARE_API_TOKEN & CLOUDFLARE_ACCOUNT_ID

1 Like

(post deleted by author)

Since you’re uploading files (basically writing them) you’d need write access rather than read.

Make sure the “Cloudflare Pages” permission is set to “Edit”.

You can also use the “Cloudflare Workers” template which has all the needed permissions. You can select that in the API token page

1 Like

Done!

Thank You @Walshy !! :blush::pray:t2:

Now everything is working!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.