Skip to content

Instantly share code, notes, and snippets.

@meleu
Created June 3, 2022 16:46
Show Gist options
  • Save meleu/2e5ce5f7f0c48589ff81ac871ebc5dbe to your computer and use it in GitHub Desktop.
Save meleu/2e5ce5f7f0c48589ff81ac871ebc5dbe to your computer and use it in GitHub Desktop.
[GitLab CI] Apenas Maintainers podem rodar o job
# ... muito conteúdo foi omitido aqui...
.template-maintainers-only:
before_script:
- source ./scripts/lib.sh
- maintainersOnly
# ...
critical_job:
extends: .template-maintainers-only
script:
# ... do some stuff...
#!/usr/bin/env bash
# ... muito conteúdo foi omitido aqui...
# functions to interact with gitlab's API
###############################################################################
# https://docs.gitlab.com/ee/api/access_requests.html#valid-access-levels
declare -Ar GITLAB_ROLES=(
[guest]=10
[reporter]=20
[developer]=30
[maintainer]=40
[owner]=50
)
gitlabApi() {
local path="$1"
curl \
--silent \
--header "PRIVATE-TOKEN: ${READ_API_TOKEN}" \
"${CI_API_V4_URL}/${path}"
}
getUserAccessLevel() {
gitlabApi \
"projects/${CI_PROJECT_ID}/members/all/${GITLAB_USER_ID}" \
| jq --exit-status '.access_level'
}
maintainersOnly() {
[[ "$(getUserAccessLevel)" -eq "${GITLAB_ROLES[maintainer]}" ]] \
|| msgBannerError "Only Maintainers of this repository can run this job"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment