GitLab
Provides helpers for resolving merge request context, posting MR comments, fetching avatars, and creating GitLab releases.
Usage
include:
- project: hosst/gitlab-pipelines
file: helpers/gitlab.yamlVariables
| Variable | Default | Description |
|---|---|---|
GITLAB_AUTH_TOKEN | $CI_JOB_TOKEN | Token used for API requests — the default job token works for all jobs; set a Personal Access Token here only if you need broader API access |
GITLAB_PROJECT_NAME | $CI_PROJECT_TITLE | Project display name |
GITLAB_PROJECT_SLUG | $CI_PROJECT_NAME | Project slug |
GITLAB_PROJECT_URL | https://gitlab.com/$CI_PROJECT_PATH | Project URL |
GITLAB_MESSAGE_URL | https://gitlab.com/api/v4/projects/.../notes | MR notes API endpoint |
GITLAB_RELEASE_URL | https://gitlab.com/api/v4/projects/.../releases | Releases API endpoint |
GITLAB_MERGE_REQUESTS_URL | $CI_PROJECT_URL/-/merge_requests | MR list URL |
GITLAB_MERGE_REQUEST_CREATE_URL | $CI_PROJECT_URL/-/merge_requests/new?... | New MR URL for current branch |
GITLAB_CODE_CHANGES_URL | $CI_PROJECT_URL/-/compare/... | Diff URL between before and after SHA |
GITLAB_PIPELINE_URL | $CI_PROJECT_URL/-/pipelines/$CI_PIPELINE_ID | Current pipeline URL |
GITLAB_PIPELINE_JOB_URL | $CI_PROJECT_URL/-/jobs/$CI_JOB_ID | Current job URL |
GITLAB_PIPELINES_URL | $CI_PROJECT_URL/-/pipelines?... | Pipeline list URL for current branch |
GITLAB_ROLLBACK_URL | $GITLAB_PIPELINES_URL | Rollback URL (same as pipelines list) |
Merge request (.gitlab_merge_request)
Resolves GITLAB_MERGE_REQUEST_IID and GITLAB_MERGE_REQUEST_URL from CI_OPEN_MERGE_REQUESTS in before_script. Falls back to the new MR URL when no open MR is found.
Variables
Examples
Typically called via !reference in after_script so the MR URL is available for notifications.
deploy:
after_script:
- !reference [.gitlab_merge_request, before_script]
- echo "MR: $GITLAB_MERGE_REQUEST_URL"MR comment (.gitlab_message)
Posts a comment on the current merge request as an after_script step.
Variables
| Variable | Default | Description |
|---|---|---|
GITLAB_MESSAGE_BODY | Hello world! | Comment body |
GITLAB_MESSAGE_DATA | body=$GITLAB_MESSAGE_BODY | POST data sent to the API |
Examples
deploy:
extends: .gitlab_message
variables:
GITLAB_MESSAGE_BODY: "Deployed $CI_COMMIT_REF_NAME to production"
script:
- helm upgrade ...Project avatar (.gitlab_project)
Fetches the project avatar URL from the GitLab API and exports it as GITLAB_PROJECT_AVATAR. Falls back to a placeholder image if no avatar is set.
Variables
Examples
Typically called via !reference in after_script to include the project avatar in a Slack notification.
deploy:
after_script:
- !reference [.gitlab_project, before_script]
- echo "$GITLAB_PROJECT_AVATAR"User avatar (.gitlab_user_avatar)
Resolves the current user's avatar URL and exports it as GITLAB_USER_AVATAR. Tries the GitLab account avatar, then the commit author's Gravatar, falling back to an identicon.
Variables
Examples
Typically called via !reference in after_script to include the triggering user's avatar in a Slack notification.
deploy:
after_script:
- !reference [.gitlab_user_avatar, before_script]
- echo "$GITLAB_USER_AVATAR"Release (.gitlab_release)
Creates a GitLab release when a tag is pushed. Skips gracefully if the release already exists. Only runs on tag pipelines.
Variables
| Variable | Default | Description |
|---|---|---|
GITLAB_RELEASE_NAME | $CI_COMMIT_TAG | Release title |
GITLAB_RELEASE_MESSAGE | $CI_COMMIT_TAG_MESSAGE | Release description |
GITLAB_RELEASE_TAG | $CI_COMMIT_TAG | Tag to create the release for |
Examples
release:
extends: .gitlab_release
stage: publish