2020-05-10 21:57:11 +02:00
|
|
|
# GitLab CI
|
|
|
|
|
2021-12-23 02:52:01 +02:00
|
|
|
Below are some example GitLab CI jobs that use GoReleaser to release a project.
|
2021-03-19 13:54:34 +02:00
|
|
|
|
2024-07-09 04:30:10 +02:00
|
|
|
> If you are using private hosted or Enterprise version of GitLab, please
|
|
|
|
> follow this [guide](../scm/gitlab.md) before diving into the details.
|
2021-09-01 14:12:28 +02:00
|
|
|
|
2021-03-19 13:54:34 +02:00
|
|
|
## Basic Releasing
|
|
|
|
|
2021-12-23 02:52:01 +02:00
|
|
|
You can easily run GoReleaser in GitLab CI using its Docker container.
|
2021-03-19 13:54:34 +02:00
|
|
|
|
|
|
|
In the repository's GitLab CI settings, add a `GITLAB_TOKEN` variable. The value should
|
|
|
|
be an API token with `api` scope for a user that has access to the project. This
|
|
|
|
variable should be masked and optionally protected if the job will only run on
|
|
|
|
protected branches and tags.
|
2022-03-19 22:49:55 +02:00
|
|
|
|
|
|
|
!!! warning
|
2024-02-02 03:53:16 +02:00
|
|
|
|
|
|
|
If you use a project access token, make sure to set `use_package_registry`
|
|
|
|
to `true` as well, otherwise it might not work.
|
2022-03-19 22:49:55 +02:00
|
|
|
|
2022-11-04 21:04:19 +02:00
|
|
|
!!! warning
|
2024-02-02 03:53:16 +02:00
|
|
|
|
|
|
|
If you are using a [protected variable](https://docs.gitlab.com/ee/ci/variables/#protected-cicd-variables)
|
|
|
|
to store any of the values needed by goreleaser, ensure that you are protecting the tags as CI jobs in
|
|
|
|
Gitlab only may access protected variables if the job is run for protected refs
|
|
|
|
([branches](https://docs.gitlab.com/ee/user/project/protected_branches.html),
|
|
|
|
[tags](https://docs.gitlab.com/ee/user/project/protected_tags.html)).
|
2022-11-04 21:04:19 +02:00
|
|
|
|
2021-12-23 02:52:01 +02:00
|
|
|
See [Quick Start](https://goreleaser.com/quick-start/) for more information on
|
2021-03-19 13:54:34 +02:00
|
|
|
GoReleaser's environment variables.
|
|
|
|
|
|
|
|
Add a `.gitlab-ci.yml` file to the root of the project:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
stages:
|
|
|
|
- release
|
|
|
|
|
|
|
|
release:
|
|
|
|
stage: release
|
|
|
|
image:
|
|
|
|
name: goreleaser/goreleaser
|
2024-02-02 03:22:41 +02:00
|
|
|
entrypoint: [""]
|
2024-06-29 20:53:32 +02:00
|
|
|
rules:
|
|
|
|
- if: $CI_COMMIT_TAG
|
2021-03-19 13:54:34 +02:00
|
|
|
variables:
|
|
|
|
# Disable shallow cloning so that goreleaser can diff between tags to
|
|
|
|
# generate a changelog.
|
|
|
|
GIT_DEPTH: 0
|
|
|
|
script:
|
2023-01-31 04:06:46 +02:00
|
|
|
- goreleaser release --clean
|
2021-03-19 13:54:34 +02:00
|
|
|
```
|
|
|
|
|
2021-12-23 02:52:01 +02:00
|
|
|
Notice that `entrypoint` is intentionally blank. See the
|
|
|
|
[GitLab documentation on entrypoints](https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#overriding-the-entrypoint-of-an-image)
|
2021-03-19 13:54:34 +02:00
|
|
|
for more information.
|
|
|
|
|
2021-12-23 02:52:01 +02:00
|
|
|
When tags are pushed to the repository,
|
|
|
|
an available GitLab Runner with the Docker executor will pick up the release job.
|
2022-08-28 21:15:24 +02:00
|
|
|
`goreleaser/goreleaser` will start in a container, and the repository will be mounted inside.
|
2021-12-23 02:52:01 +02:00
|
|
|
Finally, the `script` section will run within the container starting in your project's directory.
|
2021-03-19 13:54:34 +02:00
|
|
|
|
|
|
|
## Releasing Archives and Pushing Images
|
|
|
|
|
2021-12-23 02:52:01 +02:00
|
|
|
Pushing images to a registry requires using Docker-in-Docker. To create GitLab releases and push
|
2021-03-19 13:54:34 +02:00
|
|
|
images to a Docker registry, add a file `.gitlab-ci.yml` to the root of the project:
|
2020-05-10 21:57:11 +02:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
stages:
|
|
|
|
- release
|
|
|
|
|
|
|
|
release:
|
|
|
|
stage: release
|
|
|
|
image: docker:stable
|
|
|
|
services:
|
|
|
|
- docker:dind
|
|
|
|
|
|
|
|
variables:
|
|
|
|
# Optionally use GitLab's built-in image registry.
|
|
|
|
# DOCKER_REGISTRY: $CI_REGISTRY
|
|
|
|
# DOCKER_USERNAME: $CI_REGISTRY_USER
|
|
|
|
# DOCKER_PASSWORD: $CI_REGISTRY_PASSWORD
|
|
|
|
|
|
|
|
# Or, use any registry, including the official one.
|
|
|
|
DOCKER_REGISTRY: https://index.docker.io/v1/
|
|
|
|
|
|
|
|
# Disable shallow cloning so that goreleaser can diff between tags to
|
|
|
|
# generate a changelog.
|
|
|
|
GIT_DEPTH: 0
|
|
|
|
|
|
|
|
# Only run this release job for tags, not every commit (for example).
|
2024-06-29 20:53:32 +02:00
|
|
|
rules:
|
|
|
|
- if: $CI_COMMIT_TAG
|
2020-05-10 21:57:11 +02:00
|
|
|
|
|
|
|
script: |
|
|
|
|
# GITLAB_TOKEN is needed to create GitLab releases.
|
2024-02-02 03:22:41 +02:00
|
|
|
# CI_JOB_TOKEN is needed if use_job_token is set.
|
2020-05-10 21:57:11 +02:00
|
|
|
# DOCKER_* are needed to push Docker images.
|
2020-07-27 16:10:15 +02:00
|
|
|
docker run --rm --privileged \
|
2020-05-10 21:57:11 +02:00
|
|
|
-v $PWD:/go/src/gitlab.com/YourGitLabUser/YourGitLabRepo \
|
|
|
|
-w /go/src/gitlab.com/YourGitLabUser/YourGitLabRepo \
|
|
|
|
-v /var/run/docker.sock:/var/run/docker.sock \
|
2024-02-02 03:22:41 +02:00
|
|
|
-e DOCKER_USERNAME \
|
|
|
|
-e DOCKER_PASSWORD \
|
|
|
|
-e DOCKER_REGISTRY \
|
2020-05-10 21:57:11 +02:00
|
|
|
-e GITLAB_TOKEN \
|
2024-02-02 03:22:41 +02:00
|
|
|
-e CI_JOB_TOKEN \
|
2023-01-31 04:06:46 +02:00
|
|
|
goreleaser/goreleaser release --clean
|
2020-05-10 21:57:11 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
In GitLab CI settings, add variables for `DOCKER_REGISTRY`, `DOCKER_USERNAME`,
|
|
|
|
and `DOCKER_PASSWORD` if you aren't using the GitLab image registry. If you are
|
|
|
|
using the GitLab image registry, you don't need to set these.
|
|
|
|
|
|
|
|
Add a variable `GITLAB_TOKEN` if you are using [GitLab
|
|
|
|
releases](https://docs.gitlab.com/ce/user/project/releases/). The value should
|
|
|
|
be an API token with `api` scope for a user that has access to the project.
|
|
|
|
|
2022-12-28 15:19:29 +02:00
|
|
|
Alternatively, you can provide the gitlab token in a file. GoReleaser will check
|
|
|
|
`~/.config/goreleaser/gitlab_token` by default, but you can change that in the
|
|
|
|
`.goreleaser.yaml` file:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
# .goreleaser.yaml
|
|
|
|
env_files:
|
|
|
|
gitlab_token: ~/.path/to/my/gitlab_token
|
|
|
|
```
|
|
|
|
|
|
|
|
Note that the environment variable will be used if available, regardless of the
|
|
|
|
`gitlab_token` file.
|
|
|
|
|
2020-05-10 21:57:11 +02:00
|
|
|
The secret variables, `DOCKER_PASSWORD` and `GITLAB_TOKEN`, should be masked.
|
|
|
|
Optionally, you might want to protect them if the job that uses them will only
|
|
|
|
be run on protected branches or tags.
|
|
|
|
|
2021-12-23 02:52:01 +02:00
|
|
|
Make sure the `image_templates` in the file `.goreleaser.yaml` reflect that
|
2020-05-10 21:57:11 +02:00
|
|
|
custom registry!
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
dockers:
|
2024-02-02 03:22:41 +02:00
|
|
|
- goos: linux
|
|
|
|
goarch: amd64
|
|
|
|
image_templates:
|
|
|
|
- "registry.gitlab.com/Group/Project:{{ .Tag }}"
|
|
|
|
- "registry.gitlab.com/Group/Project:latest"
|
2020-05-10 21:57:11 +02:00
|
|
|
```
|
2021-01-21 05:54:12 +02:00
|
|
|
|
2021-03-19 13:54:34 +02:00
|
|
|
## Example Repository
|
2021-01-21 05:54:12 +02:00
|
|
|
|
|
|
|
You can check [this example repository](https://gitlab.com/goreleaser/example) for a real world example.
|
|
|
|
|
|
|
|
<a href="https://gitlab.com/goreleaser/example/-/releases">
|
|
|
|
<figure>
|
|
|
|
<img src="https://img.carlosbecker.dev/goreleaser-gitlab.png"/>
|
|
|
|
<figcaption>Example release on GitLab.</figcaption>
|
|
|
|
</figure>
|
|
|
|
</a>
|