Hello,
just tried to write my own plugin. I want it to trigger a pipeline for a certain project each time a Tag is created on any project.
I followed the instructions here:
My questions:
- My plugin is triggered twice (2 pipelines are created) when i create a tag in my repository and 3 times when i delete a tag. Any idea/hint why this is the case?
- Is there a way to use gitlab internal functions instead of the api ?
- Is there a way to add configuration options to the GUI from within the plugin?
This is my plugin - as workaround its writing a temporary file and compares before and after hash to avoid multiple pipeline execution:
#!/usr/bin/env ruby
require 'json'
require 'tmpdir'
payload = JSON.parse(STDIN.read)
# Only run this code in case we are dealing with tag_push event
if payload['event_name'] == 'tag_push'
temp = Dir.tmpdir()
shaFile = "#{temp}/gitlab-trigger.sha"
# Get hash values to compare
payloadHash = "#{payload['before']} #{payload['after']}"
payloadHashBefore = File.read(shaFile) if File.exist?(shaFile)
# Save before and after hash to compare
File.delete(shaFile) if File.exist?(shaFile)
File.write(shaFile, "#{payload['before']} #{payload['after']}")
# Trigger Pipeline
if payloadHashBefore != payloadHash
exec('curl --request POST --form "token=TRIGGER_TOKEN" --form ref=master https://gitlab.example.org/api/v4/projects/36/trigger/pipeline')
end
end
Thanks for your help.
Cheers,
Ochorocho