Skip to content

worktree-io/gitlab-comment-action

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

gitlab-comment-action

A GitLab CI/CD job template that posts an Open workspace badge comment on new GitLab issues, powered by Worktree.

When a new issue is created, a comment like this is automatically posted:

A workspace is ready for this issue.

Open workspace →

Powered by Worktree · Install

Install

1. Add a CI/CD variable

In your GitLab project go to Settings → CI/CD → Variables and add:

Key Value Flags
WORKTREE_GITLAB_TOKEN A personal access token with api scope Masked

2. Create a pipeline trigger token

Go to Settings → CI/CD → Pipeline triggers and create a new trigger token. Copy the token — you'll need it for the webhook bridge.

3. Deploy the webhook bridge

GitLab webhooks send JSON payloads, but the pipeline trigger API accepts form parameters. Deploy this Cloudflare Worker to bridge them:

export default {
  async fetch(request, env) {
    if (request.method !== "POST") return new Response("Method Not Allowed", { status: 405 });

    const payload = await request.json();
    const event = payload.object_kind;
    if (event !== "issue" || payload.object_attributes?.action !== "open") {
      return new Response("ignored", { status: 200 });
    }

    const iid = payload.object_attributes.iid;
    const projectId = payload.project.id;

    const body = new URLSearchParams({
      token: env.TRIGGER_TOKEN,
      ref: env.REF || "main",
      "variables[ISSUE_IID]": String(iid),
    });

    const resp = await fetch(
      `https://gitlab.com/api/v4/projects/${projectId}/trigger/pipeline`,
      { method: "POST", body }
    );

    const text = await resp.text();
    return new Response(text, { status: resp.status });
  },
};

Set the following Worker secrets / environment variables:

  • TRIGGER_TOKEN — the pipeline trigger token from step 2
  • REF — the branch to run the pipeline on (default: main)

4. Add the GitLab webhook

In Settings → Webhooks, add a webhook pointing to your Worker URL and enable Issues events.

5. Include the template

Add this to your .gitlab-ci.yml:

include:
  - remote: 'https://raw.githubusercontent.com/worktree-io/gitlab-comment-action/v1/templates/comment.yml'

That's it. When a new issue is opened, the webhook fires the Worker, which triggers a pipeline with ISSUE_IID set — the worktree-comment job picks it up and posts the comment.

How it works

  • The job runs in the .pre stage and only when CI_PIPELINE_SOURCE == "trigger" and ISSUE_IID is set, so it never interferes with normal pipelines.
  • GIT_STRATEGY: none means no repository checkout is needed — the job finishes in seconds.
  • The comment body is built with printf and safely JSON-encoded with jq before being sent to the GitLab Notes API.

License

MIT

About

GitLab CI/CD job template that posts an Open workspace link on new issues

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors