GitLab generic packages repository
- Tier: Free, Premium, Ultimate
- Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
Use the generic packages repository to publish and manage generic files, such as release binaries, in your project's package registry. This feature is particularly useful for storing and distributing artifacts that don't fit into specific package formats like npm or Maven.
The generic packages repository provides:
- A place to store any file type as a package.
- Version control for your packages.
- Integration with GitLab CI/CD.
- API access for automation.
Authenticate to the package registry
To interact with the package registry, you must authenticate with one of the following methods:
- A personal access token with the scope set to
api
. - A project access token with the scope set to
api
and at least the Developer role. - A CI/CD job token.
- A deploy token with the scope set to
read_package_registry
,write_package_registry
, or both.
Do not use authentication methods other than the methods documented here. Undocumented authentication methods might be removed in the future.
When you authenticate with the package registry, you should follow these best practices:
- To access permissions associated with the Developer role, use a personal access token.
- Use CI/CD job tokens for automated pipelines.
- Use deploy tokens for external system integration.
- Always send authentication information over HTTPS.
HTTP Basic authentication
If you use a tool that doesn't support the standard authentication methods, you can use HTTP Basic authentication:
curl --user "<username>:<token>" <other options> <GitLab API endpoint>
Although it is ignored, you must provide a username. The token is your personal access token, CI/CD job token, or deploy token.
Publish a package
You can publish packages with the API.
Publish a single file
To publish a single file, use the following API endpoint:
PUT /projects/:id/packages/generic/:package_name/:package_version/:file_name
Replace the placeholders in the URL with your specific values:
-
:id
: Your project ID or URL-encoded path -
:package_name
: Name of your package -
:package_version
: Version of your package -
:file_name
: Name of the file you're uploading
For example:
Personal access token
With HTTP headers:
curl --location --header "PRIVATE-TOKEN: <personal_access_token>" \
--upload-file path/to/file.txt \
"https://gitlab.example.com/api/v4/projects/24/packages/generic/my_package/1.0.0/file.txt"
With HTTP Basic authentication:
curl --location --user "<username>:<personal_access_token>" \
--upload-file path/to/file.txt \
"https://gitlab.example.com/api/v4/projects/24/packages/generic/my_package/1.0.0/file.txt"
Project access token
With HTTP headers:
curl --location --header "PRIVATE-TOKEN: <project_access_token>" \
--upload-file path/to/file.txt \
"https://gitlab.example.com/api/v4/projects/24/packages/generic/my_package/1.0.0/file.txt"
With HTTP Basic authentication:
curl --location --user "<project_access_token_username>:project_access_token" \
--upload-file path/to/file.txt \
"https://gitlab.example.com/api/v4/projects/24/packages/generic/my_package/1.0.0/file.txt"
Deploy token
With HTTP headers:
curl --location --header "DEPLOY-TOKEN: <deploy_token>" \
--upload-file path/to/file.txt \
"https://gitlab.example.com/api/v4/projects/24/packages/generic/my_package/1.0.0/file.txt"
With HTTP Basic authentication:
curl --location --user "<deploy_token_username>:<deploy_token>" \
--upload-file path/to/file.txt \
"https://gitlab.example.com/api/v4/projects/24/packages/generic/my_package/1.0.0/file.txt"
Replace <deploy_token_username>
with the username of your deploy token and <deploy_token>
with your actual deploy token.
CI/CD job token
These examples are for a .gitlab-ci.yml
file. GitLab CI/CD automatically provides the CI_JOB_TOKEN
.
With HTTP headers:
publish:
stage: deploy
script:
- |
curl --location --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
--upload-file path/to/file.txt \
"${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/my_package/${CI_COMMIT_TAG}/file.txt"
With HTTP Basic authentication:
publish:
stage: deploy
script:
- |
curl --location --user "gitlab-ci-token:${CI_JOB_TOKEN}" \
--upload-file path/to/file.txt \
"${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/my_package/${CI_COMMIT_TAG}/file.txt"
Each request returns a response indicating success or failure. If your upload is successful, the response status is 201 Created
.
Publish multiple files
To publish multiple files or an entire directory, you must make one API call for each file.
You should follow these best practices when you publish multiple files to the repository:
- Versioning: Use a consistent versioning scheme for your package. This could be based on your project's version, build number, or date.
- File organization: Consider how you want to structure your files within the package. You might want to include a manifest file that lists all the included files and their purposes.
- Automation: Whenever possible, automate the publishing process through CI/CD pipelines. This ensures consistency and reduces manual errors.
- Error handling: Implement error checking in your scripts. For example, check the HTTP response code from cURL to ensure each file was uploaded successfully.
- Logging: Maintain logs of what files were uploaded, when, and by whom. This can be crucial for troubleshooting and auditing.
- Compression: For large directories, consider compressing the contents into a single file before uploading. This can simplify the upload process and reduce the number of API calls.
- Checksums: Generate and store checksums (MD5, SHA256) for your files. This allows users to verify the integrity of downloaded files.
For example:
With a Bash script
Create a Bash script to iterate through files and upload them:
PUT /projects/:id/packages/generic/:package_name/:package_version/:file_name
```0
#### With GitLab CI/CD
For automated uploads in your CI/CD pipeline, you can iterate through your files and upload them:
```shell
PUT /projects/:id/packages/generic/:package_name/:package_version/:file_name
```1
### Maintain directory structure
To preserve the structure of a published directory, include the relative path in the file name:
```shell
PUT /projects/:id/packages/generic/:package_name/:package_version/:file_name
```2
## Download a package
You can download packages with the API.
### Download a single file
To download a single package file, use the following API endpoint:
```shell
PUT /projects/:id/packages/generic/:package_name/:package_version/:file_name
```3
Replace the placeholders in the URL with your specific values: