Skip to content

Instantly share code, notes, and snippets.

@psahni
Created May 18, 2024 14:20
Show Gist options
  • Save psahni/173a30a6c499a7ced5f4074269a29822 to your computer and use it in GitHub Desktop.
Save psahni/173a30a6c499a7ced5f4074269a29822 to your computer and use it in GitHub Desktop.
gitlab_cicd_example
image: node:10
stages:
- build
- test
- deploy review
- deploy staging
- deploy production
- production tests
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
variables:
STAGING_DOMAIN: instazone-staging.surge.sh
PRODUCTION_DOMAIN: instazone.surge.sh
build website:
stage: build
only:
- master
- merge_requests
script:
- echo $CI_COMMIT_SHORT_SHA
- npm install
- npm install -g gatsby-cli
- gatsby build
- sed -i "s/%%VERSION%%/$CI_COMMIT_SHORT_SHA/" ./public/index.html
artifacts:
paths:
- ./public
test artifact:
image: alpine
stage: test
only:
- master
- merge_requests
script:
- grep -q "Gatsby" ./public/index.html
test website:
stage: test
only:
- master
- merge_requests
script:
- npm install
- npm install -g gatsby-cli
- gatsby serve &
- sleep 3
- curl "http://localhost:9000" | tac | tac | grep -q "Gatsby"
deploy review:
stage: deploy review
only:
- merge_requests
environment:
name: review/$CI_COMMIT_REF_NAME
url: https://instazone-$CI_ENVIRONMENT_SLUG.surge.sh
script:
- npm install --global surge
- surge --project ./public --domain instazone-$CI_ENVIRONMENT_SLUG.surge.sh
deploy staging:
stage: deploy staging
environment:
name: staging
url: http://$STAGING_DOMAIN
only:
- master
script:
- npm install --global surge
- surge --project ./public --domain $STAGING_DOMAIN
deploy production:
stage: deploy production
environment:
name: production
url: $PRODUCTION_DOMAIN
only:
- master
script:
- npm install --global surge
- surge --project ./public --domain $PRODUCTION_DOMAIN
production tests:
image: alpine
stage: production tests
only:
- master
script:
- apk add --no-cache curl
- curl -s "https://$PRODUCTION_DOMAIN" | grep -q "Hi people"
- curl -s "https://$PRODUCTION_DOMAIN" | grep -q "$CI_COMMIT_SHORT_SHA"
@psahni
Copy link
Author

psahni commented May 18, 2024

AWS pipeline (Elastic bean stack)

variables:
  ARTIFACT_NAME: cars-api-v$CI_PIPELINE_IID.jar
  APP_NAME: cars-api

stages:
  - build
  - test
  - deploy
  - post deploy
  - publishing

build:
  stage: build
  image: openjdk:12-alpine
  script:
    - sed -i "s/CI_PIPELINE_IID/$CI_PIPELINE_IID/" ./src/main/resources/application.yml
    - sed -i "s/CI_COMMIT_SHORT_SHA/$CI_COMMIT_SHORT_SHA/" ./src/main/resources/application.yml
    - sed -i "s/CI_COMMIT_BRANCH/$CI_COMMIT_BRANCH/" ./src/main/resources/application.yml
    - ./gradlew build
    - mv ./build/libs/cars-api.jar ./build/libs/$ARTIFACT_NAME
  artifacts:
    paths:
      - ./build/libs/

smoke test:
  stage: test
  image: openjdk:12-alpine
  before_script:
    - apk --no-cache add curl
  script:
    - java -jar ./build/libs/$ARTIFACT_NAME &
    - sleep 30
    - curl http://localhost:5000/actuator/health | grep "UP"

code quality:
  stage: test
  image: openjdk:12-alpine
  script:
    - ./gradlew pmdMain pmdTest
  artifacts:
    when: always
    paths:
      - build/reports/pmd

unit tests:
  stage: test
  image: openjdk:12-alpine
  script:
    - ./gradlew test
  artifacts:
    when: always
    paths:
      - build/reports/tests
    reports:
      junit: build/test-results/test/*.xml

deploy:
  stage: deploy
  image:
    name: banst/awscli
    entrypoint: [""]
  before_script:
    - apk --no-cache add curl
    - apk --no-cache add jq
  script:
    - aws configure set region us-east-1
    - aws s3 cp ./build/libs/$ARTIFACT_NAME s3://$S3_BUCKET/$ARTIFACT_NAME
    - aws elasticbeanstalk create-application-version --application-name $APP_NAME --version-label $CI_PIPELINE_IID --source-bundle S3Bucket=$S3_BUCKET,S3Key=$ARTIFACT_NAME
    - CNAME=$(aws elasticbeanstalk update-environment --application-name $APP_NAME --environment-name "production" --version-label=$CI_PIPELINE_IID | jq '.CNAME' --raw-output)
    - sleep 45
    - curl http://$CNAME/actuator/info | grep $CI_PIPELINE_IID
    - curl http://$CNAME/actuator/health | grep "UP"

api testing:
  stage: post deploy
  image:
    name: vdespa/newman
    entrypoint: [""]
  script:
    - newman --version
    - newman run "Cars API.postman_collection.json" --environment Production.postman_environment.json --reporters cli,htmlextra,junit --reporter-htmlextra-export "newman/report.html" --reporter-junit-export "newman/report.xml"
  artifacts:
    when: always
    paths:
      - newman/
    reports:
      junit: newman/report.xml

pages:
  stage: publishing
  script:
    - mkdir public
    - mv newman/report.html public/index.html
  artifacts:
    paths:
      - public

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment