github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/www/docs/ci/gitlab.md (about) 1 # GitLab CI 2 3 Below are some example GitLab CI jobs that use GoReleaser to release a project. 4 5 > If you are using private hosted or Enterprise version of Gitlab, please follow this [guide](/scm/gitlab/) before diving into the details. 6 7 ## Basic Releasing 8 9 You can easily run GoReleaser in GitLab CI using its Docker container. 10 11 In the repository's GitLab CI settings, add a `GITLAB_TOKEN` variable. The value should 12 be an API token with `api` scope for a user that has access to the project. This 13 variable should be masked and optionally protected if the job will only run on 14 protected branches and tags. 15 See [Quick Start](https://goreleaser.com/quick-start/) for more information on 16 GoReleaser's environment variables. 17 18 Add a `.gitlab-ci.yml` file to the root of the project: 19 20 ```yaml 21 stages: 22 - release 23 24 release: 25 stage: release 26 image: 27 name: goreleaser/goreleaser 28 entrypoint: [''] 29 only: 30 - tags 31 variables: 32 # Disable shallow cloning so that goreleaser can diff between tags to 33 # generate a changelog. 34 GIT_DEPTH: 0 35 script: 36 - goreleaser release --rm-dist 37 ``` 38 39 Notice that `entrypoint` is intentionally blank. See the 40 [GitLab documentation on entrypoints](https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#overriding-the-entrypoint-of-an-image) 41 for more information. 42 43 When tags are pushed to the repository, 44 an available GitLab Runner with the Docker executor will pick up the release job. 45 `goreleaser/goreleaser` will start in a container and the repository will be mounted inside. 46 Finally, the `script` section will run within the container starting in your project's directory. 47 48 ## Releasing Archives and Pushing Images 49 50 Pushing images to a registry requires using Docker-in-Docker. To create GitLab releases and push 51 images to a Docker registry, add a file `.gitlab-ci.yml` to the root of the project: 52 53 ```yaml 54 stages: 55 - release 56 57 release: 58 stage: release 59 image: docker:stable 60 services: 61 - docker:dind 62 63 variables: 64 # Optionally use GitLab's built-in image registry. 65 # DOCKER_REGISTRY: $CI_REGISTRY 66 # DOCKER_USERNAME: $CI_REGISTRY_USER 67 # DOCKER_PASSWORD: $CI_REGISTRY_PASSWORD 68 69 # Or, use any registry, including the official one. 70 DOCKER_REGISTRY: https://index.docker.io/v1/ 71 72 # Disable shallow cloning so that goreleaser can diff between tags to 73 # generate a changelog. 74 GIT_DEPTH: 0 75 76 # Only run this release job for tags, not every commit (for example). 77 only: 78 refs: 79 - tags 80 81 script: | 82 # GITLAB_TOKEN is needed to create GitLab releases. 83 # DOCKER_* are needed to push Docker images. 84 docker run --rm --privileged \ 85 -v $PWD:/go/src/gitlab.com/YourGitLabUser/YourGitLabRepo \ 86 -w /go/src/gitlab.com/YourGitLabUser/YourGitLabRepo \ 87 -v /var/run/docker.sock:/var/run/docker.sock \ 88 -e DOCKER_USERNAME -e DOCKER_PASSWORD -e DOCKER_REGISTRY \ 89 -e GITLAB_TOKEN \ 90 goreleaser/goreleaser release --rm-dist 91 ``` 92 93 In GitLab CI settings, add variables for `DOCKER_REGISTRY`, `DOCKER_USERNAME`, 94 and `DOCKER_PASSWORD` if you aren't using the GitLab image registry. If you are 95 using the GitLab image registry, you don't need to set these. 96 97 Add a variable `GITLAB_TOKEN` if you are using [GitLab 98 releases](https://docs.gitlab.com/ce/user/project/releases/). The value should 99 be an API token with `api` scope for a user that has access to the project. 100 101 The secret variables, `DOCKER_PASSWORD` and `GITLAB_TOKEN`, should be masked. 102 Optionally, you might want to protect them if the job that uses them will only 103 be run on protected branches or tags. 104 105 Make sure the `image_templates` in the file `.goreleaser.yml` reflect that 106 custom registry! 107 108 Example: 109 110 ```yaml 111 dockers: 112 - 113 goos: linux 114 goarch: amd64 115 image_templates: 116 - 'registry.gitlab.com/Group/Project:{{ .Tag }}' 117 - 'registry.gitlab.com/Group/Project:latest' 118 ``` 119 120 ## Example Repository 121 122 You can check [this example repository](https://gitlab.com/goreleaser/example) for a real world example. 123 124 <a href="https://gitlab.com/goreleaser/example/-/releases"> 125 <figure> 126 <img src="https://img.carlosbecker.dev/goreleaser-gitlab.png"/> 127 <figcaption>Example release on GitLab.</figcaption> 128 </figure> 129 </a>