项目作者: codica2

项目描述 :
Example of configuration Gitlab CI for Rails Application
高级语言:
项目地址: git://github.com/codica2/gitlab-ci-configuration.git
创建时间: 2019-04-12T05:32:14Z
项目社区:https://github.com/codica2/gitlab-ci-configuration

开源协议:

下载


GitLab CI for Rails

GitLab CI/CD is GitLab’s built-in tool for software development using continuous methodology:

Continuous integration (CI).
Continuous delivery and deployment (CD).

Gitlab Documentation

Configure the project

GitLab CI/CD pipelines are configured using a YAML file called .gitlab-ci.yml within each project. The .gitlab-ci.yml file defines the structure and order of the pipelines and determines:
This is what the .gitlab-ci.yml file looks like for this project:

Creating a .gitlab-ci.yml file

The .gitlab-ci.yml file is where you configure what CI does with your project. It lives in the root of your repository.

On any push to your repository, GitLab will look for the .gitlab-ci.yml file and start jobs on Runners according to the contents of the file, for that commit.

  1. stages:
  2. - build
  3. # running build
  4. - linters
  5. # running code quality tools (rubocop, brakeman)
  6. - tests
  7. # running tests
  8. - scanners
  9. # image and application scanning
  10. - deploy
  11. # application deployment
  12. # All variables you will set up in your repo settings (Settings->CI/CD->Variables)
  13. variables:
  14. RAILS_MASTER_KEY: $RAILS_MASTER_KEY
  15. AWS_REGISTRY_URL: $AWS_REGISTRY_URL
  16. AWS_REGION: $AWS_REGION
  17. REGISTRY_IMAGE: $AWS_REGISTRY_URL:$CI_COMMIT_REF_SLUG
  18. REGISTRY_IMAGE_PRODUCTION: $AWS_REGISTRY_URL_PRODUCTION:$CI_COMMIT_REF_SLUG
  19. ECS_CLUSTER_STAGING: $ECS_CLUSTER_STAGING
  20. ECS_CLUSTER_PRODUCTION: $ECS_CLUSTER_PRODUCTION
  21. ECS_SERVICE_STAGING: $ECS_SERVICE_STAGING
  22. GITLEAKS_CONFIG: gitleaks.toml
  23. DOCKER_DRIVER: overlay2
  24. SENTRY_AUTH_TOKEN: $SENTRY_AUTH_TOKEN
  25. SENTRY_ORG: $SENTRY_ORG
  26. SENTRY_PROJECT: $SENTRY_PROJECT
  27. # Perform private ECR registry authentication
  28. .registry_auth: ®istry_auth
  29. image: public.ecr.aws/o0j8c5i3/codica:registry-auth-leaks
  30. before_script:
  31. - aws ecr get-login-password --region $AWS_REGION | docker login -u AWS --password-stdin $AWS_REGISTRY_URL
  32. # Rubocop linter
  33. rubocop:
  34. stage: linters
  35. needs: [Build]
  36. image: $REGISTRY_IMAGE_ID
  37. except:
  38. - master
  39. script:
  40. - bundle exec rubocop
  41. # Brakeman linter
  42. brakeman:
  43. stage: linters
  44. needs: [Build]
  45. image: $REGISTRY_IMAGE_ID
  46. except:
  47. - master
  48. script:
  49. - brakeman
  50. # Bundle audit
  51. bundle_audit:
  52. stage: linters
  53. needs: [Build]
  54. image: $REGISTRY_IMAGE_ID
  55. except:
  56. - master
  57. script:
  58. - bundle exec bundle-audit
  59. # Trivy to scan image vulnerabilities
  60. Trivy:
  61. stage: linters
  62. allow_failure: true
  63. image:
  64. name: aquasec/trivy:0.31.3
  65. entrypoint: [""]
  66. needs: [Build]
  67. except:
  68. - main
  69. script:
  70. - trivy image --security-checks vuln $REGISTRY_IMAGE_ID
  71. # Gitleaks to detect credentials leak
  72. Gitleaks:
  73. <<: *registry_auth
  74. stage: linters
  75. allow_failure: true
  76. needs: []
  77. except:
  78. - main
  79. - master
  80. script:
  81. - gitleaks detect --verbose --no-git | jq -r '["Description:", .Description], ["File:", .File], ["Line:", .StartLine, "Column:", .StartColumn, "--------------"]' | tr -d '[],""'
  82. # Hadolint to check dockerfiles syntax
  83. Hadolint:
  84. needs: []
  85. image: hadolint/hadolint:latest-debian
  86. stage: scanners
  87. allow_failure: false
  88. script:
  89. - hadolint Dockerfile
  90. - hadolint Dockerfile.dev
  91. rules:
  92. - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
  93. when: never
  94. - changes:
  95. - Dockerfile*
  96. # Yamllint to check yaml files syntax
  97. Yamllint:
  98. needs: []
  99. image:
  100. name: public.ecr.aws/**/codica:yamllint
  101. entrypoint: [""]
  102. stage: scanners
  103. allow_failure: false
  104. script:
  105. - rm -rf spec
  106. - yamllint .
  107. rules:
  108. - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
  109. when: never
  110. - changes:
  111. - "**/*.yml"
  112. - "**/*.yaml"
  113. # Rspec tests
  114. rspec:
  115. stage: tests
  116. image: $REGISTRY_IMAGE
  117. needs: [Build | Staging]
  118. variables:
  119. DB_HOST: postgres
  120. DB_USERNAME: postgres
  121. DB_PASSWORD: $DB_PASSWORD
  122. DB_PORT: 5432
  123. REDIS_URL: "redis://redis:6379"
  124. script:
  125. - bundle exec rails db:migrate RAILS_ENV=test
  126. - bundle exec rspec
  127. except:
  128. - master
  129. # We use Kaniko as main image builder
  130. Build:
  131. stage: build
  132. image:
  133. name: gcr.io/kaniko-project/executor:debug
  134. entrypoint: [""]
  135. except:
  136. - master
  137. before_script:
  138. - mkdir -p /kaniko/.docker
  139. - echo "{\"credHelpers\":{\"$AWS_REGISTRY_URL\":\"ecr-login\"}}" > /kaniko/.docker/config.json
  140. script:
  141. - /kaniko/executor --destination "${REGISTRY_IMAGE}"
  142. --build-arg RAILS_MASTER_KEY=${RAILS_MASTER_KEY} # if we need to use RAILS_MASTER_KEY to build our app
  143. --context "${CI_PROJECT_DIR}"
  144. --dockerfile "${CI_PROJECT_DIR}/Dockerfile"
  145. # Deploy application to staging or production environment
  146. Deploy:
  147. stage: deploy
  148. variables:
  149. ASG_NAME: $ASG_STAGE
  150. CLUSTER_NAME: $ECS_CLUSTER_STAGING
  151. SERVICE_NAME: $SERVICE_STAGING
  152. AWS_REGION: $AWS_REGION
  153. REGISTRY_IMAGE: $REGISTRY_IMAGE_ID
  154. only:
  155. - develop
  156. <<: *registry_auth
  157. script:
  158. - aws ecs update-service --cluster $ECS_CLUSTER_STAGING --service $ECS_SERVICE_STAGING --force-new-deployment
  159. # We use sentry to push our release to our sentry account
  160. - VERSION=$(sentry-cli releases propose-version) && sentry-cli releases -o $SENTRY_ORG new -p $SENTRY_PROJECT $VERSION
  161. - sentry-cli releases -o $SENTRY_ORG -p $SENTRY_PROJECT --auth-token $SENTRY_AUTH_TOKEN set-commits --auto $VERSION
  162. environment:
  163. name: Staging

Push .gitlab-ci.yml to GitLab

Once you’ve created .gitlab-ci.yml, you should add it to your Git repository and push it to GitLab.

  1. git add .gitlab-ci.yml
  2. git commit -m "Add .gitlab-ci.yml"
  3. git push origin master

Configuring a Runner

In GitLab, Runners run the jobs that you define in .gitlab-ci.yml. A Runner can be a virtual machine, a VPS, a bare-metal machine, a docker container or even a cluster of containers. GitLab and the Runners communicate through an API, so the only requirement is that the Runner’s machine has network access to the GitLab server.

A Runner can be specific to a certain project or serve multiple projects in GitLab. If it serves all projects it’s called a Shared Runner.

Find more information about different Runners in the Runners documentation.

Regular pipeline graphs

Regular pipeline graphs show the names of the jobs of each stage. Regular pipeline graphs can be found when you are on a single pipeline page. For example:
pipline

Status of your pipeline and jobs

After configuring the Runner successfully, you should see the status of your last commit change from pending to either running, success or failed.

status

By clicking on a job’s status, you will be able to see the log of that job. This is important to diagnose why a job failed or acted differently than you expected.

error-log

Examples

Visit the examples README to see a list of examples using GitLab CI with various languages.

License

Copyright © 2015-2022 Codica. It is released under the MIT License.

About Codica

Codica logo

The names and logos for Codica are trademarks of Codica.

We love open source software! See our other projects or hire us to design, develop, and grow your product.