Simplify Gitlab Jobs in Pipeline

I have the following in my .gitlab-ci.yml file. The two jobs are basically the same, only differing in a couple lines (THE ALL BOLD CAPS STRINGS).

Is it possible to combine them into one job so that I don’t have to maintain two sets of commands? I’d think in order to do so, I’d need to be able to detect in the script lines, if its “testing” or “production” that’s being merged into the repo.

deploy testing:
image: python:latest
stage: deploy
only:
- DEVELOP
script:
- pip install awscli
- echo “Uploading to s3 bucket”
- aws s3 sync ./dist s3://MY.TESTING.BUCKET.COM --delete
- echo “Invalidating cloudfrond distribution to get fresh cache”
- aws cloudfront create-invalidation --distribution-id=S3TESTINGBUCKETID–paths ‘/*’

deploy production:
image: python:latest
stage: deploy
only:
- PRODUCTION
script:
- pip install awscli
- echo “Uploading to s3 bucket”
- aws s3 sync ./dist s3://MY.PRODUCTION.BUCKET.COM --delete
- echo “Invalidating cloudfrond distribution to get fresh cache”
- aws cloudfront create-invalidation --distribution-id=S3PRODUCTIONBUCKETID–paths ‘/*’

Hi,

maybe this is possible with using variables and heavy YAML templating. My colleagues have created this for multiple build jobs running with the same commands, recently described in this blog post:

Cheers,
Michael

Exactly what I needed. Thanks!

1 Like