mabounassif
mabounassif

Reputation: 2341

Project templates in Gitlab upon creation

What is the cleanest way to create a project in a self hosted gitlab with predefined (dynamically generated) files?

Concretely, I want to create a git repo on gitlab that upon creation contains all the files of a basic iOS project already. I just have to clone it and just start developing, instead of creating the project locally and then push it up to Gitlab.

Anyone?

I was looking into the gitlab-shell and the creation hook in gitlab-project: https://github.com/gitlabhq/gitlab-shell/blob/master/lib/gitlab_projects.rb

Am I about to embark on a world of pain following this method? Is there something already out there that makes it easy to hook up at the application level?

Upvotes: 14

Views: 4824

Answers (3)

VonC
VonC

Reputation: 1323115

See GitLab 13.7 (December 2020), available in GitLab CE (free)

Improved UI for project creation

The improved user interface for adding a project makes it easier to select between creating a blank project, starting a project from a template, importing an existing project, and creating a CI/CD-only project for external repositories.

See Documentation and Issue.

This allows to create a project from a template easily:

https://gitlab.com/gitlab-org/gitlab/uploads/42faf9f5f0c8fb5157a89595ab9748f9/Import_1.0.png


You have also, with GitLab 14.8 (February 2022):

Add default issue and merge request templates in a project’s repository

In addition to defining default issue and merge request description templates in project settings, you can now set default templates in the .gitlab directory of a project’s repository. Do it by creating a Default.md file in the issue or merge request templates folders. If default templates exist in both the project’s settings and in the repository, the template in the settings takes precedence.

Thanks for the contribution @davebarr!

See Documentation and Issue.

Upvotes: 0

For GitLab Premium (self-hosted) (or GitLab Silver plan for gitlab.com) please see Custom instance-level project templates, available since Gitlab 11.2.

Upvotes: 1

Greg Dubicki
Greg Dubicki

Reputation: 6930

For GitLab CE (or EE too, if you need more features than the standard templates provide) I propose using this set of tools:

  • configure system hooks in GitLab to make it fire a POST request on project_create events,

  • set up adnanh/webhook as a server that will receive these POST requests and then run egnyte/gitlabform app to configure the new project,

  • create configuration for gitlabform with any files you need. Example:

gitlab:
  url: https://gitlab.yourcompany.com
  # You can also set in your environment GITLAB_TOKEN
  # this can be both private token OR OAuth2 access token
  token: "<private token of an admin user>"
  api_version: 4

group_settings:
  # this assumes that you create your new iOS projects in a "ios" group in GitLab
  ios:
    files:
      "README.md":
        # this will prevent resetting the file to below contents if it has already been customized
        overwrite: false
        # this will prevent the commit that applies this file change triggering CI build
        skip_ci: true
        branches:
          - develop
        content: |
          This is a default project README. Please replace it with a proper one!
      "other-file":
        branches:
          - develop
        # You can provide file contents with external file too. Both absolute and relative paths are supported.
        # Relative paths are interpreted as relative to `config.yml` file location.
        file: some-file.txt

See https://github.com/egnyte/gitlabform for more info about the features, more examples and a full config syntax description.

Disclosure: I am the original author of egnyte/gitlabform app and I have contributed to adnanh/webhook and therefore I like them both. :)

Upvotes: 1

Related Questions