Netlify-cms: Gitlab exposes target_url only for external builds

Created on 1 Jun 2020  路  5Comments  路  Source: netlify/netlify-cms

Describe the bug
I'm unable to use the deploy preview feature with Gitlab as a backend. My setup makes use of environments to tell to Gitlab the preview URL:
image

environment:
    name: review/$CI_COMMIT_REF_NAME
    url: https://$CI_ENVIRONMENT_SLUG.preview.mywebsite.tld
    on_stop: stop_review_app

The Netlify CMS Gitlab backend fetches the target_url information from the Gitlab Commit status API endpoint but the returned value for the target_url property stays null in my setup, and therefore Netlify CMS only provides a dead link.

If I understand the Gitlab source correctly, this gets set only through 3rd-party integration.

It gets really hard to get environment information for a specific MR through the API. Gitlab internally uses /<user>/<repo>/-/merge_requests/<iid>/ci_environments_status, but there doesn't seem to be a public equivalent.

Applicable Versions:

  • Netlify CMS version: 2.10.49
  • Git provider: Gitlab
  • OS: Ubuntu
  • Browser version: Firefox 76
  • Node.JS version: 14
  • Gitlab version: 13.0.3

CMS configuration

backend:
  name: gitlab
  repo: <me/my_repo>
  auth_type: implicit
  app_id: <redacted>
  api_root: https://<gitlab.host>/api/v4
  base_url: https://<gitlab.host>
  auth_endpoint: oauth/authorize
  branch: master # Branch to update (optional; defaults to master)

local_backend: true
publish_mode: editorial_workflow

media_folder: "/static/uploads"
public_folder: ""

collections:
  - name: "posts"
    label: "Posts"
    folder: "posts"
    path: "{{year}}/{{month}}/{{slug}}"
    media_folder: "{{media_folder}}"
    create: true
    preview_path: "{{year}}/{{month}}/{{slug}}"
    fields:
      - { label: "Title", name: "title", widget: "string" }
      - {
          label: "Publish Date",
          name: "date",
          widget: "date",
          dateFormat: "DD/MM/YYYY",
        }
      - label: "Tags"
        name: "tags"
        widget: "list"
      - label: "Featured Image"
        name: "thumbnail"
        widget: "image"
        required: false
        media_library:
          config:
            multiple: false
      - { label: "Body", name: "body", widget: "markdown" }
extensionbackends unconfirmed bug

Most helpful comment

Hello @tcitworld @erezrokah
I have encountered the same issue with gitlab and target_url and this configuration works for me (just add 2 stages pre and after my pages deploy and set appropriate statuses in them):

pre-deploy:
  stage: pre-deploy
  environment:
    name: demo
  script:
    - 'curl --verbose --request POST --header "PRIVATE-TOKEN: ${API_PRIVATE_TOKEN}" "${CI_API_V4_URL}/projects/${CI_PROJECT_NAMESPACE}%2F${CI_PROJECT_NAME}/statuses/$CI_COMMIT_SHA?state=pending&context=pages:deploy&target_url=${CI_PAGES_URL}"'
  only:
    - /^cms\/.*$/

after-deploy:
  stage: after-deploy
  environment:
    name: demo
  script:
    - 'curl --verbose --request POST --header "PRIVATE-TOKEN: ${API_PRIVATE_TOKEN}" "${CI_API_V4_URL}/projects/${CI_PROJECT_NAMESPACE}%2F${CI_PROJECT_NAME}/statuses/$CI_COMMIT_SHA?state=success&context=pages:deploy&target_url=${CI_PAGES_URL}"'
  only:
    - /^cms\/.*$/

All 5 comments

Hi @tcitworld, I'm not sure I understand the bug.
It's up to the CI service/build process to set the commit status with the preview URL (target_url).

As long as you pass a token with relevant permissions you should be able to add a commit status via the API https://docs.gitlab.com/ee/api/commits.html#post-the-build-status-to-a-commit

Gitlab CI already sets a status on the commit (with "status": "success" among other things), but doesn't include the target_url information. I may be able to POST another commit status containing the information through API from inside Gitlab CI, but it definitely feels like it's not the correct way to to this.

I found this issue that says that setting the Commit Status "was intended to only be used with external CI services".

Do you want me to open an issue on Gitlab's tracker to ask them what they think about it?

Hi @tcitworld, you could contact GitLab about setting target_url in their CI, but I think it might be better to have two different statues - the default one set by GitLab CI and another for the deploy preview.

You can see an example in a CircleCI job here (though with GitHub):
https://github.com/erezrokah/gatsby-netlify-cms-aws/blob/5e7c9197d40797a73cf1393fbf95c4112b68de88/.circleci/config.yml#L142
https://github.com/erezrokah/gatsby-netlify-cms-aws/blob/5e7c9197d40797a73cf1393fbf95c4112b68de88/.circleci/config.yml#L164

Then it would make sense to do it yourself via the commit status API (I would consider that as external since it is not built in into the GitLab CI platform).

Am I making sense here?

Closing as this is not a CMS issue

Hello @tcitworld @erezrokah
I have encountered the same issue with gitlab and target_url and this configuration works for me (just add 2 stages pre and after my pages deploy and set appropriate statuses in them):

pre-deploy:
  stage: pre-deploy
  environment:
    name: demo
  script:
    - 'curl --verbose --request POST --header "PRIVATE-TOKEN: ${API_PRIVATE_TOKEN}" "${CI_API_V4_URL}/projects/${CI_PROJECT_NAMESPACE}%2F${CI_PROJECT_NAME}/statuses/$CI_COMMIT_SHA?state=pending&context=pages:deploy&target_url=${CI_PAGES_URL}"'
  only:
    - /^cms\/.*$/

after-deploy:
  stage: after-deploy
  environment:
    name: demo
  script:
    - 'curl --verbose --request POST --header "PRIVATE-TOKEN: ${API_PRIVATE_TOKEN}" "${CI_API_V4_URL}/projects/${CI_PROJECT_NAMESPACE}%2F${CI_PROJECT_NAME}/statuses/$CI_COMMIT_SHA?state=success&context=pages:deploy&target_url=${CI_PAGES_URL}"'
  only:
    - /^cms\/.*$/
Was this page helpful?
0 / 5 - 0 ratings

Related issues

chriskirknielsen picture chriskirknielsen  路  3Comments

marcojakob picture marcojakob  路  3Comments

jake-101 picture jake-101  路  3Comments

ghost picture ghost  路  3Comments

calavera picture calavera  路  3Comments