Skip to content

Instantly share code, notes, and snippets.

@manueltuero
Created September 23, 2019 15:41
Show Gist options
  • Save manueltuero/02682c230386fc949c314f40b6a42cc6 to your computer and use it in GitHub Desktop.
Save manueltuero/02682c230386fc949c314f40b6a42cc6 to your computer and use it in GitHub Desktop.
Gitlab CI/CD build and deploy template
# Global Docker image for the runner
image: node:10
stages:
- build
- deploy
cache:
paths:
- node_modules/
# Templates
.Build template: &build_template
stage: build
before_script:
- npm ci
script:
- npm run build
artifacts:
name: build
paths:
- build
.Deploy template: &deploy_template
image: python:latest
stage: deploy
before_script:
- pip install awscli
script:
- aws s3 sync build s3://$AWS_BUCKET_NAME
- curl -X POST $SLACK_WEBHOOK_URL -H 'Content-Type:application/json' -d '{"attachments":[{"fallback":"A deployment has started to '"$AWS_BUCKET_NAME"'", "color":"good", "pretext":"*A deployment has started to '"$AWS_BUCKET_NAME"'*", "title":"New Deployment", "text":"'"$CI_COMMIT_SHA"' \n '"$CI_COMMIT_MESSAGE"'", "footer":"Slack API", "footer_icon":"https://platform.slack-edge.com/img/default_application_icon.png"}]}'
# Build Jobs
Dev build:
only:
refs:
- development
<<: *build_template
Test build:
only:
refs:
- test
<<: *build_template
Prod build:
only:
refs:
- master
<<: *build_template
# Deployment Jobs
Dev deploy:
only:
refs:
- development
changes:
- src/**/*
- static/**/*
- content/**/*
- package.json
- package-lock.json
- .gitlab-ci.yml
dependencies:
- Dev build
variables:
AWS_BUCKET_NAME: $DEV_AWS_BUCKET_NAME
AWS_ACCESS_KEY_ID: $DEV_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $DEV_AWS_SECRET_ACCESS_KEY
<<: *deploy_template
Test deploy:
only:
refs:
- test
changes:
- src/**/*
- static/**/*
- content/**/*
- package.json
- package-lock.json
- .gitlab-ci.yml
dependencies:
- Test build
variables:
AWS_BUCKET_NAME: $TEST_AWS_BUCKET_NAME
AWS_ACCESS_KEY_ID: $TEST_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $TEST_AWS_SECRET_ACCESS_KEY
<<: *deploy_template
Prod deploy:
only:
refs:
- master
changes:
- src/**/*
- static/**/*
- content/**/*
- package.json
- package-lock.json
- .gitlab-ci.yml
when: manual
dependencies:
- Prod build
variables:
AWS_BUCKET_NAME: $PROD_AWS_BUCKET_NAME
AWS_ACCESS_KEY_ID: $PROD_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $PROD_AWS_SECRET_ACCESS_KEY
<<: *deploy_template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment