Introduction
When deploying projects on GitLab CI or Lagoon environments and incorporating private Composer or NPM packages developed by Acro, it's essential to generate a GitLab token to gain access to these packages.
Generating the token
- Click here create a group access token in the Acro Commerce group settings page in GitLab.
- Click on the
Add new token
button and be sure that the token details correspond to the provided specifications for simplified token rotation and future expiration management.
- Token name:
PROJECT_NAME__ACRO_TOKEN
. - Role:
Reporter
. - Scopes:
read_registry
&read_api
.
- Make sure you keep a copy of the token in a safe place, as we'll need to incorporate it into the project's CI/CD configuration or Lagoon's environment variables.
Deploying on GitLab CI?
Including a token in the CI/CD process is crucial for the project's CI/CD
operations during tasks like composer install
or npm install
, which
involve linting, testing, and project deployment. In frontend projects, the
.gitlab-ci.yml
file dynamically generates an .npmrc
file, which relies on
the provided token.
Adding the token in the project's CI/CD settings
- Navigate to the Acro's GitLab and choose the desired project where you wish to add the variable.
- Click on the
Settings
tab on the sidebar. - Click on the
CI/CD
tab on the sidebar. - Under the
Variables
section, click on theExpand
button. - Click on the
Add variable
button. - Fill in the details as follows:
- Type:
Variable (default)
- Environments:
All (default)
- Flags:
Expand variable reference
andMask variable
- Key:
GITLAB_API_TOKEN
- Value:
The token value which you've previously generated.
Referencing the token in the GitLab CI file
Once the token is added as a secret variable, it can be referenced in the
.gitlab-ci.yml
file as shown below:
Frontend project example:
before_script:
- touch .npmrc
- echo "@acromedia:registry=https://git.acromedia.com/api/v4/packages/npm/" >> .npmrc
- echo "//git.acromedia.com/api/v4/packages/npm/:_authToken=${GITLAB_API_TOKEN}" >> .npmrc
- echo "//git.acromedia.com/api/v4/projects/:_authToken=${GITLAB_API_TOKEN}" >> .npmrc
Drupal project example:
before_script:
- composer config gitlab-token.git.acromedia.com ${GITLAB_API_TOKEN}1
- composer config gitlab-domains git.acromedia.com
- composer config data-dir ${CI_PROJECT_DIR}/.tmp/.composer
Deploying on Lagoon?
Add the token as an environment variable through the Lagoon dashboard by following the steps below:
Adding the token as a project variable
- Navigate to the Lagoon dashboard and choose the desired project where you wish to add the variable.
- Click on the
Variables
tab on the sidebar. - Click on the
Add
button. - Fill in the details as follows:
- Scope:
Build
- Name:
GITLAB_API_TOKEN
- Value:
The token value which you've previously generated.
- Click on the
Add project variable
button to add the variable. - Trigger a redeployment of your project environments to apply the changes and ensure the variable becomes available.
Referencing the token in the docker file
Once the token is added as a project variable, it can be referenced in your
docker file (eg. ./lagoon/cli.dockerfile
) as shown below:
Frontend project example:
ARG GITLAB_API_TOKEN
RUN echo "@acromedia:registry=https://git.acromedia.com/api/v4/packages/npm/" >> .npmrc
RUN echo "//git.acromedia.com/api/v4/packages/npm/:_authToken=${GITLAB_API_TOKEN}" >> .npmrc
RUN echo "//git.acromedia.com/api/v4/projects/:_authToken=${GITLAB_API_TOKEN}" >> .npmrc
Drupal project example:
ARG GITLAB_API_TOKEN
RUN if [[ -n "${GITLAB_API_TOKEN}" ]] ; then \
composer config gitlab-token.git.acromedia.com ${GITLAB_API_TOKEN}; \
fi