Skip to content

Internal packages

Using GitLab as a Composer package repository

GitLab itself offers mechanics to act as a Composer package repository.

To utilise GitLab's package repository locally, you will first need to add a personal access token for composer to use. Create one with API scope at https://gitlab.morphsites.net/-/user_settings/personal_access_tokens.

Then run the following replacing <access_token> with your access token:

shell
composer global config gitlab-token.gitlab.morphsites.net <access_token>

For backwards compatibility, you should also need to add the following:

shell
composer config gitlab-domains gitlab.morphsites.net

Then to utilise GitLab as a source for composer packages, run the following:

shell
composer config repositories.gitlab composer https://gitlab.morphsites.net/api/v4/group/64/-/packages/composer/

Using GitLab package repository on a server

To utilise GitLab's package repository on a server, you will first need to create a deploy token for Composer to use on the morphsites/lib group.

Create a deploy token at https://gitlab.morphsites.net/groups/morphsites/lib/-/settings/repository: read_package_registry access.

  • Name the token based on the username and server. For example, if the site is running under the example user on prod1.uk.morphdigital.net, the token should be named [email protected].
  • Select the read_package_registry scope.

If you are unable to create a deploy token, ask a senior developer to create one for you.

Then run the following replacing <username> and <password> with your deploy token username and password:

shell
composer global config gitlab-token.gitlab.morphsites.net <username> <password>

Adding a new Composer package to GitLab

Add the following as a .gitlab-ci.yml. When tagging, the package will then automatically be released and available.

yaml
stages:
    - deploy

deploy:
    image: alpine:latest
    stage: deploy
    only:
        - tags
    script:
        - apk add curl
        - 'curl --header "Job-Token: $CI_JOB_TOKEN" --data tag=$CI_COMMIT_TAG "${CI_API_V4_URL}/projects/$CI_PROJECT_ID/packages/composer"'
    environment: production

Creating a new Laravel Package

As standard, all new packages should use the spatie laravel package template. To proceed with using this, you will need to have a GitHub account with your SSH key configured to allow you to clone the repository.

Ensure you have set up your new repo on GitLab in the morphsites/lib namespace, unless you are unable to, in which case you should use your own namespace and ask a senior developer to move it to the correct namespace.

Clone the package template into the folder your new package will occupy and cleanup some files, where <package-slug> is the slug for the package:

shell
git clone --depth=1 [email protected]:spatie/package-skeleton-laravel.git <package-slug>
rm -rf !$/.git

Once done, connect to GitLab:

shell
cd <package-slug>
git init --initial-branch=main
git remote add origin [email protected]:morphsites/lib/<package-slug>.git
git add .
git commit -m "Initial commit"
git push -u origin main

Configure the package:

shell
php configure.php

Use the following settings:

  • Author name: Your name
  • Author email: Your morphsites email
  • Author Username: morphsites
  • Vendor name: morphsites
  • Vendor namespace: Morphsites
  • Package/Class name: keep it simple, remove any Laravel prefixes
  • Phpstan: Y
  • Pint: Y
  • Dependabot: N
  • Ray: Y
  • Changelog: N
  • Execute composer + run tests: Y
  • Trust plugins
  • Self delete script: Y

Then, lastly, you'll need to ensure that the package is tagged to GitLab's package repository correctly, by adding the following as a .gitlab-ci.yml file at the root of the repo:

yaml
stages:
    - deploy

deploy:
    stage: deploy
    only:
        - tags
    script:
        - apk add curl
        - 'curl --header "Job-Token: $CI_JOB_TOKEN" --data tag=$CI_COMMIT_TAG "${CI_API_V4_URL}/projects/$CI_PROJECT_ID/packages/composer"'
    environment: production

With all that done, commit your changes and you're now ready to start coding away at your brand new package.

Package changelogs

With the adoption of the Conventional Commits standard, package changelogs can be automatically generated utilising the marcocesarato/php-conventional-changelog package. This wil allow for clean and precise changelogs for all our packages. It will also allow for the automatic generation tags based on the release types.