Create GitLab Project Variable

Action ID: gitlab:projectVariable:create
NPM Package:

@backstage/plugin-scaffolder-backend-module-gitlab

Description

Creates a new project variable in GitLab with specified attributes and settings.

Input Schema

PropertyTypeDescriptionRequired
keystringThe key of a variable; must have no more than 255 characters; only A-Z, a-z, 0-9, and _ are allowed
rawbooleanWhether the variable is expandable
tokenstringThe token to use for authorization to GitLab
valuestringThe value of a variable
maskedbooleanWhether the variable is masked
repoUrlstring-
projectIdanyProject ID
variableTypestringVariable Type (env_var or file)
environmentScopestringThe environment_scope of the variable
variableProtectedbooleanWhether the variable is protected

Output Schema

No output schema defined for this action.

Usage Examples

Create a global environment variable for all environments

Use this to set a nonsecret variable that applies to all environments. After fetch:template, this step creates an env var available to GitLab CI jobs.

Copy
steps:
  - id: fetch-base
    action: fetch:template
    input:
      url: ./skeleton
      targetPath: ./workspace
      values:
        name: ${{ parameters.componentId }}

  - id: create-api-base-url
    action: gitlab:projectVariable:create
    input:
      repoUrl: gitlab.com?project=platform%2Fcatalog-service
      projectId: 53821
      key: API_BASE_URL
      value: https://api.internal.example.com
      variableType: env_var
      environmentScope: "*"

Create a protected and masked production token

Use this to store a sensitive token for production. The variable is protected and masked so it is only available on protected refs and its value is hidden.

Copy
steps:
  - id: fetch-base
    action: fetch:template
    input:
      url: ./skeleton
      targetPath: ./
      values:
        name: ${{ parameters.componentId }}

  - id: create-prod-token
    action: gitlab:projectVariable:create
    input:
      repoUrl: ${{ parameters.repoUrl }}
      token: ${{ secrets.gitlabToken }}
      projectId: ${{ parameters.gitlabProjectId }}
      key: PROD_API_TOKEN
      value: ${{ secrets.prodApiToken }}
      variableType: env_var
      variableProtected: true
      masked: true
      environmentScope: "production"

Create a file variable for service account credentials

Use this to store JSON credentials as a file variable for a specific environment. GitLab will mount the content into a file when used in CI jobs.

Copy
steps:
  - id: fetch-base
    action: fetch:template
    input:
      url: ./template
      targetPath: .
      values:
        name: ${{ parameters.name }}

  - id: create-gcp-creds-file
    action: gitlab:projectVariable:create
    input:
      repoUrl: gitlab.com?project=platform%2Fpayments-service
      token: ${{ secrets.gitlabToken }}
      projectId: 87122
      key: GCP_SA_JSON
      value: |-
        {
          "type": "service_account",
          "project_id": "payments-staging",
          "private_key_id": "abc1234567890abcdef",
          "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEv...\n-----END PRIVATE KEY-----\n",
          "client_email": "svc@payments-staging.iam.gserviceaccount.com",
          "client_id": "10987654321"
        }
      variableType: file
      variableProtected: true
      environmentScope: "staging"

Create a raw variable to prevent expansion in GitLab CI

Use this when you want to store a value that contains GitLab CI variables and keep it unexpanded. Set raw to true to keep the literal value.

Copy
steps:
  - id: fetch-base
    action: fetch:template
    input:
      url: ./skeleton
      targetPath: .
      values:
        name: ${{ parameters.serviceId }}

  - id: create-raw-image-var
    action: gitlab:projectVariable:create
    input:
      repoUrl: ${{ parameters.repoUrl }}
      projectId: ${{ parameters.gitlabProjectId }}
      key: DOCKER_IMAGE
      value: "${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_SLUG}"
      variableType: env_var
      raw: true
      environmentScope: "*"

Create a review app scoped variable using a wildcard environment scope

Use this to set a variable used only by review app environments. The wildcard scope applies to all review environments.

Copy
steps:
  - id: fetch-base
    action: fetch:template
    input:
      url: ./base
      targetPath: .
      values:
        name: ${{ parameters.componentId }}

  - id: create-review-host-var
    action: gitlab:projectVariable:create
    input:
      repoUrl: gitlab.com?project=web%2Fstorefront
      token: ${{ secrets.gitlabToken }}
      projectId: 64213
      key: REVIEW_APP_HOST
      value: "app-${CI_COMMIT_REF_SLUG}.example.com"
      variableType: env_var
      environmentScope: "review/*"