1

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?

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.

  • First one is not an option (I am not the owner of the github repo), but second one could work. Thanks! – Dimitris Moraitidis Jul 17 at 19:25
  • @DimitrisMoraitidis You could use the github API V3 and find the latest release in a repository. – 030 Jul 17 at 19:27
  • 1
    Indeed. Most particularly, this would work: Github Releases API. 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' – Dimitris Moraitidis Jul 18 at 9:05
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.