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鈥檚 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
Enter fullscreen mode Exit fullscreen mode
  • But got the error remote: You are not allowed to upload code.
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
Enter fullscreen mode Exit fullscreen mode

馃殌 Solve problem by using OAUTH2.0 and tokens

1. Create access token

  • Go to User Setttings -> Access Tokens

Alt Text

Alt Text

2. Update .gitlab-ci.yaml

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
Enter fullscreen mode Exit fullscreen mode

3. Check result

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
Enter fullscreen mode Exit fullscreen mode

Ref: Make pipeline permissions more controllable and flexible

Visit wwww.cloudopz.co to get more

馃尃 Blog Web Linkedin Group Page Twitter 馃尃

Discussion (0)