DEV Community

Cover image for Config Gitlab Runner To Push A Tag
🚀 Vu Dao 🚀
🚀 Vu Dao 🚀

Posted on

Config Gitlab Runner To Push A Tag

Any commits should be tagged align with build version especially master branch. How to configure Gitlab runner to do this?

What’s In This Document

🚀 What is the usecase

  • Developer tells gitlab runner to tag the commit and publish the tag .gitlab-ci.yml ```

build:
stage: build
script:
- echo "Build and tag the commit"
- tag=1.0-${CI_COMMIT_SHORT_SHA}
- git tag $tag
- git push origin $tag
tags:
- gitlab-runner


- But got the error `remote: You are not allowed to upload code.`
Enter fullscreen mode Exit fullscreen mode

Build and tag the commit
$ tag=1.0-${CI_COMMIT_SHORT_SHA}
$ git tag $tag
$ git push origin $tag
remote: You are not allowed to upload code.
fatal: unable to access 'https://gitlab-ci-token:[MASKED]@gitlab.com/hello-gitlab.git/': The requested URL returned error: 403
ERROR: Job failed: exit status 1


### 🚀 **[Solve problem by using OAUTH2.0 and tokens](#-Solve-problem-by-using-OAUTH2.0-and-tokens)**
#### **1. Create access token**
- Go to `User Setttings` -> `Access Tokens`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4mve0fn40k5wnkp2e6cp.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oul9w4o5ebpobo948aw2.png)

#### **2. Update [.gitlab-ci.yaml](https://github.com/vumdao/gitlab-tag/.gitlab-ci.yaml)**
Enter fullscreen mode Exit fullscreen mode

build:
stage: build
before_script:
- project_url=$(echo $CI_PROJECT_URL | sed 's/https:\/\///')
- git remote set-url origin https://oauth2:$GITLAB_TOKEN@$project_url
script:
- echo "Build and tag the commit"
- tag=1.0-${CI_COMMIT_SHORT_SHA}
- git tag $tag
- git push origin $tag
only:
refs:
- tagme
tags:
- gitlab-runner


#### **3. Check result**
Enter fullscreen mode Exit fullscreen mode

Build and tag the commit
$ tag=1.0-${CI_COMMIT_SHORT_SHA}
$ git tag $tag
$ git push origin $tag
warning: redirecting to https://gitlab.com/hello-gitlab.git/
To https://gitlab.com/hello-gitlab

  • [new tag] 1.0-0714997f -> 1.0-0714997f Job succeeded

#### **Ref: [Make pipeline permissions more controllable and flexible](https://gitlab.com/groups/gitlab-org/-/epics/3559)**

### **Visit wwww.cloudopz.co to get more**


<h3 align="center">
  <a href="https://dev.to/vumdao">:stars: Blog</a>
  <span> · </span>
  <a href="https://vumdao.hashnode.dev/">Web</a>
  <span> · </span>
  <a href="https://www.linkedin.com/in/vu-dao-9280ab43/">Linkedin</a>
  <span> · </span>
  <a href="https://www.linkedin.com/groups/12488649/">Group</a>
  <span> · </span>
  <a href="https://www.facebook.com/CloudOpz-104917804863956">Page</a>
  <span> · </span>
  <a href="https://twitter.com/VuDao81124667">Twitter :stars:</a>
</h3>
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
oferei profile image
Ofer Reichman

Hey, thanks! This proved useful for me.
Just a small note: the lines in the before_script section (git remote set-url ...) should be called after the build. Otherwise the build may fail with this error:
[UnityConnectServicesConfig] config is NOT valid, switching to default
Cancelling DisplayDialog: Failed to activate/update license Missing or bad username or password. Please try again using valid credentials or contact support@unity3d.com

I avoided the error by putting all the code above at the end of my script section.

Collapse
 
vumdao profile image
🚀 Vu Dao 🚀

@oferei I now don't need to use oauth2 just use the url eg. GIT_URL="https://gitlab-ci-token:${TOKEN}@gitlab.com/devops/k8s.git"