I have a repository of Packer build files and they build different Packer images (Ubuntu, Windows, RHEL etc.) and my goal is to have a pipeline, so when changes get made to a Packer file, it builds/tests the image. Let's say in my Gitlab pipeline I have a couple of build jobs for these different packer builds
gitlab-ci.yml
build_windows:
stage: buildWindows
script:
- echo "Building Packer Image"
- cd aws
- packer build -only="*windows*" .
build_ubuntu:
stage: buildUbuntu
script:
- echo "Building Packer Image"
- cd aws
- packer build -only="*ubuntu*" .
Instead of the pipeline running and building out both images every time there's a change made to my repo, is there a way I can make it so if changes are made the to Windows packer file in my repo, the pipeline only runs the job to build the Windows image and if the Ubuntu packer file gets changed in my repo, the pipeline will only build the Ubuntu image?
I figured out that I was able to do so using only:changes:
as per this gitlab doc
For my use case this is how I implemented it so that this job only runs if I make changes to either of those two files in the aws directory
build_linux_AMI:
stage: build_linux
script:
- echo "Building Linux Packer Image"
- cd aws
- packer build -only="*rhel-stig*" .
- AMI_ID=$(jq -r '.builds[-1].artifact_id' manifest.json | cut -d ":" -f2)
- echo "LINUX_AMI_ID=$AMI_ID" > linuximage.env
only:
changes:
- aws/build-linux.pkr.hcl
- aws/source-linux.pkr.hcl