· engineering  · 1 min read

Triggering Gitlab pipelines from AWS

Automatically trigger a GitLab pipeline when content changes in an S3 bucket

Automatically trigger a GitLab pipeline when content changes in an S3 bucket

Recently, while implementing a Gatsby site that renders content from a JSON file stored in an S3 bucket, I faced the challenge of needing to trigger a GitLab pipeline whenever this content changed.

The solution

The solution appears straightforward: we need a deploy token, which can be obtained and used by following these steps:

  • In your GitLab project, navigate to Settings > CI/CD.

Settings in gitlab

  • Go to ‘Pipeline Trigger Tokens’ and create a new token.

pipeline-trigger-tokens.png

  • Follow the instructions to configure your webhook.

instructions

Challenges Encountered.

Despite following the instructions, I consistently received a 404 error when attempting to use the webhook, though using the POST method worked correctly.

Debouncing the pipeline

Given that the content in the bucket is dynamic and files are often updated in quick succession, it is necessary to debounce the pipeline to avoid redundant builds:

  1. When a file is changed, a ‘rebuild required’ marker is also added to the root directory.
  2. Using a scheduled EventBridge rule, we configure a Lambda function to run at set intervals (e.g., hourly).
  3. This Lambda function checks for the presence of the ‘rebuild required’ marker, and if found, it triggers the deployment pipeline.
  4. Finally, the GitLab pipeline is configured to delete the marker once the build completes.
James Babington

About James Babington

A cloud architect and engineer with a wealth of experience across AWS, web development, and security, James enjoys writing about the technical challenges and solutions he's encountered, but most of all he loves it when a plan comes together and it all just works.

Comments

No comments yet. Be the first to comment!

Leave a Comment

Check this box if you don't want your comment to be displayed publicly.

Back to Blog

Related Posts

View All Posts »