<p>So here's how I managed to do it:</p>

<p>Assuming that I need to build an AMI with Packer, only if a new release is detected in the github repository of interest, my gitlab ci yaml looks like:</p>

<pre><code>image: ubuntu

variables:
  NEW_ECS_AGENT: "false"

before_script:
  - apt update
  - apt -y install wget unzip jq curl httpie
  - current_version=$(curl -s https://checkpoint-api.hashicorp.com/v1/check/packer | jq -r .current_version | sed 's/v//g')
  - wget https://releases.hashicorp.com/packer/${current_version}/packer_${current_version}_linux_amd64.zip -O temp.zip; unzip temp.zip; rm temp.zip
  - mkdir -p /opt/hashicorp
  - mv packer /opt/hashicorp/
  - export PATH="$PATH:/opt/hashicorp"
  - LAST_PUBLISH_TIMESTAMP=$(http -b GET https://api.github.com/repos/aws/amazon-ecs-agent/releases/latest | jq -r '.published_at')
  - LAST_PUBLISH_DATE=$(echo ${LAST_PUBLISH_TIMESTAMP:0:10} | sed 's/-//g')
  - TODAY=$(date +"%Y%m%d")
  - TODAYS_DAY=$(echo ${TODAY:6:2})
  - if [[ (( 'TODAY - LAST_PUBLISH_DATE' -gt 0 ) &amp;&amp; ( 'TODAY - LAST_PUBLISH_DATE' -lt 2 )) || (( 'TODAY - LAST_PUBLISH_DATE' -ge 70 ) &amp;&amp; ( 'TODAY - LAST_PUBLISH_DATE' -le 72 ) &amp;&amp; ( "$TODAYS_DAY" == "01" )) || ( 'TODAY - LAST_PUBLISH_DATE' -eq 8870 ) ]] ; then eval export NEW_ECS_AGENT="true" ; fi
  - echo $NEW_ECS_AGENT
  - echo $CI_PIPELINE_SOURCE

build-zabbix-agent-scheduled:
  stage: build
  script:
    - echo $NEW_ECS_AGENT
    - if [[ "$NEW_ECS_AGENT" == "false" ]] &amp;&amp; [[ "$CI_PIPELINE_SOURCE" == "schedule" ]] ; then echo "No new ecs-agent version detected. Skipping AMI building..." &amp;&amp; exit ; fi
    - cd ecs-zabbix
    - packer build template.json
  only:
    refs:
      - schedules

</code></pre>

<p>First part of the <code>before_script</code> pulls the latest packer version and then it checks the aws/amazon-ecs-agent github repo for the latest release publish date, and detects if there is a new release, based on the diff with the current date.</p>

<p>I know the multiple if clauses look kinda stupid (they basically take into account all possibilities of dates diff (same month, different month, different year, goal is to check if a release was made the day before the pipeline runs, and if so, it builds it)) but at least it works.</p>

<p>Lastly, there is a conditional exit (inside the <code>script</code> key) if the above conditions are not met. It works, but in the interface it shows the pipeline as passed. I am not sure if there is a better way of doing this.</p>

CC BY-SA 4.0
2019-07-18T15:23:39.757Z
2019-07-18T15:23:39.757Z
dimisjim
<p>One could set up a webhook between github and gitlab, see settings>integrations>github in gitlab or trigger a pipeline in gitlab by configuring github.</p>

<p><a href="https://docs.gitlab.com/ee/ci/ci_cd_for_external_repos/github_integration.html" rel="nofollow noreferrer">https://docs.gitlab.com/ee/ci/ci_cd_for_external_repos/github_integration.html</a></p>

<p>If one does not have a github account and one cannot fork the github project, one could create a script that runs on daily basis and triggers the gitlab pipeline by calling the pipeline API if "new release in github" is applicable.</p>

CC BY-SA 4.0
2019-07-17T19:25:24.237Z
First one is not an option (I am not the owner of the github repo), but second one could work. Thanks!
dimisjim
CC BY-SA 4.0
2019-07-17T19:27:03.193Z
@DimitrisMoraitidis You could use the [github API V3](https://developer.github.com/v3/) and find the latest release in a repository.
030
CC BY-SA 4.0
2019-07-18T09:05:53.463Z
Indeed. Most particularly, this would work: [Github Releases API](https://developer.github.com/v3/repos/releases/). For example, this returns the latest version of hashicorp's terraform: `http -b GET https://api.github.com/repos/hashicorp/terraform/releases/latest | jq -r '.tag_name'`
dimisjim
CC BY-SA 4.0
2019-07-17T19:12:35.993Z
2019-07-17T19:12:35.993Z
030
<p>I would like to trigger a GitlabCI pipeline that builds a Packer image, whenever there is a new version of a binary released in Github.</p>

<p>How could I do that via the ci yaml configuration?
Should I somehow use the API for <a href="https://docs.gitlab.com/ee/ci/triggers/README.html" rel="nofollow noreferrer">pipeline triggering</a>?</p>

<gitlab><cicd>
Trigger a GitlabCI pipeline after it detects a change via a curl callback
dimisjim
# Trigger a GitlabCI pipeline after it detects a change via a curl callback

(Asked by: username_0 on 2019-07-16)

I would like to trigger a GitlabCI pipeline that builds a Packer image, whenever there is a new version of a binary released in Github.

How could I do that via the ci yaml configuration?
Should I somehow use the API for [pipeline triggering](https://docs.gitlab.com/ee/ci/triggers/README.html)?

**Tags:** gitlab, cicd


---

## 2 answers

### Answer by username_0 on 2019-07-18. Score: 0 | Accepted answer

So here's how I managed to do it:
Assuming that I need to build an AMI with Packer, only if a new release is detected in the github repository of interest, my gitlab ci yaml looks like:
image: ubuntu

variables:
  NEW_ECS_AGENT: "false"

before_script:
  - apt update
  - apt -y install wget unzip jq curl httpie
  - current_version=$(curl -s https://checkpoint-api.hashicorp.com/v1/check/packer | jq -r .current_version | sed 's/v//g')
  - wget https://releases.hashicorp.com/packer/${current_version}/packer_${current_version}_linux_amd64.zip -O temp.zip; unzip temp.zip; rm temp.zip
  - mkdir -p /opt/hashicorp
  - mv packer /opt/hashicorp/
  - export PATH="$PATH:/opt/hashicorp"
  - LAST_PUBLISH_TIMESTAMP=$(http -b GET https://api.github.com/repos/aws/amazon-ecs-agent/releases/latest | jq -r '.published_at')
  - LAST_PUBLISH_DATE=$(echo ${LAST_PUBLISH_TIMESTAMP:0:10} | sed 's/-//g')
  - TODAY=$(date +"%Y%m%d")
  - TODAYS_DAY=$(echo ${TODAY:6:2})
  - if [[ (( 'TODAY - LAST_PUBLISH_DATE' -gt 0 ) && ( 'TODAY - LAST_PUBLISH_DATE' -lt 2 )) || (( 'TODAY - LAST_PUBLISH_DATE' -ge 70 ) && ( 'TODAY - LAST_PUBLISH_DATE' -le 72 ) && ( "$TODAYS_DAY" == "01" )) || ( 'TODAY - LAST_PUBLISH_DATE' -eq 8870 ) ]] ; then eval export NEW_ECS_AGENT="true" ; fi
  - echo $NEW_ECS_AGENT
  - echo $CI_PIPELINE_SOURCE

build-zabbix-agent-scheduled:
  stage: build
  script:
    - echo $NEW_ECS_AGENT
    - if [[ "$NEW_ECS_AGENT" == "false" ]] && [[ "$CI_PIPELINE_SOURCE" == "schedule" ]] ; then echo "No new ecs-agent version detected. Skipping AMI building..." && exit ; fi
    - cd ecs-zabbix
    - packer build template.json
  only:
    refs:
      - schedules


First part of the before_script pulls the latest packer version and then it checks the aws/amazon-ecs-agent github repo for the latest release publish date, and detects if there is a new release, based on the diff with the current date.
I know the multiple if clauses look kinda stupid (they basically take into account all possibilities of dates diff (same month, different month, different year, goal is to check if a release was made the day before the pipeline runs, and if so, it builds it)) but at least it works.
Lastly, there is a conditional exit (inside the script key) if the above conditions are not met. It works, but in the interface it shows the pipeline as passed. I am not sure if there is a better way of doing this.


---

### Answer by username_1 on 2019-07-17. Score: 0

One could set up a webhook between github and gitlab, see settings>integrations>github in gitlab or trigger a pipeline in gitlab by configuring github.

<https://docs.gitlab.com/ee/ci/ci_cd_for_external_repos/github_integration.html>

If one does not have a github account and one cannot fork the github project, one could create a script that runs on daily basis and triggers the gitlab pipeline by calling the pipeline API if "new release in github" is applicable.

> **Comments:**
> **username_0**: First one is not an option (I am not the owner of the github repo), but second one could work. Thanks!  
> **username_1**: @DimitrisMoraitidis You could use the [github API V3](https://developer.github.com/v3/) and find the latest release in a repository.  
> **username_0**: Indeed. Most particularly, this would work: [Github Releases API](https://developer.github.com/v3/repos/releases/). For example, this returns the latest version of hashicorp's terraform: `http -b GET https://api.github.com/repos/hashicorp/terraform/releases/latest | jq -r '.tag_name'`  

---

