Last active
June 23, 2023 21:00
-
-
Save AliOsm/39ef50417a754fe48074b41bafbf4512 to your computer and use it in GitHub Desktop.
Ruby on Rails GitHub Actions CI/CD Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "Ruby on Rails CI/CD" | |
on: | |
push: | |
branches: [ "main" ] | |
paths-ignore: | |
- '.devcontainer/**' | |
- '.env.example' | |
- '.erb-lint.yml' | |
- '.gitignore' | |
- 'README.md' | |
pull_request: | |
branches: [ "main" ] | |
jobs: | |
test: | |
runs-on: ubuntu-latest | |
services: | |
postgres: | |
image: postgres:latest | |
ports: | |
- "5432:5432" | |
env: | |
POSTGRES_DB: postgres | |
POSTGRES_USER: postgres | |
POSTGRES_PASSWORD: postgres | |
redis: | |
image: redis:latest | |
ports: | |
- "6379:6379" | |
meilisearch: | |
image: getmeili/meilisearch:latest | |
ports: | |
- "7700:7700" | |
env: | |
MEILI_MASTER_KEY: LOCAL_TEST_KEY | |
MEILI_ENV: development | |
env: | |
RAILS_ENV: test | |
DATABASE_URL: "postgres://postgres:postgres@localhost:5432/rails_test" | |
REDIS_URL: "redis://localhost:6379" | |
MEILISEARCH_HOST: "http://localhost:7700" | |
MEILISEARCH_KEY: "LOCAL_TEST_KEY" | |
steps: | |
- name: Install system dependencies | |
run: sudo apt-get -yqq install libvips | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Install Ruby | |
uses: ruby/[email protected] | |
with: | |
bundler-cache: true | |
- name: Install Node.js | |
uses: actions/[email protected] | |
- name: Install NPM dependencies | |
run: npm i | |
- name: Prepare the database and the search engine | |
run: bundle exec rails db:prepare ms:reindex_indexes db:reset_and_populate_fake | |
- name: Run unit tests | |
run: bundle exec rails test | |
- name: Run system tests | |
uses: nick-fields/retry@v2 | |
with: | |
timeout_minutes: 10 | |
max_attempts: 3 | |
command: bundle exec rails test:system | |
- name: Create coverage artifact | |
uses: actions/upload-artifact@v3 | |
with: | |
name: code-coverage | |
path: coverage/ | |
- uses: joshmfrankel/simplecov-check-action@main | |
with: | |
minimum_suite_coverage: 75 | |
minimum_file_coverage: 25 | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
lint: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Install Ruby | |
uses: ruby/[email protected] | |
with: | |
bundler-cache: true | |
- name: Security audit dependencies | |
run: bundle exec bundle audit --update | |
- name: Security audit application code | |
run: bundle exec brakeman -q -w2 | |
- name: Lint Ruby files | |
run: bundle exec rubocop --parallel | |
deploy: | |
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && !contains(github.event.head_commit.message, '[no deploy]') }} | |
needs: [test, lint] | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Deploy to Hatchbox | |
run: | | |
DEPLOYMENT_ID=$(curl -X POST "https://app.hatchbox.io/webhooks/deployments/${{ secrets.HATCHBOX_DEPLOY_KEY }}?latest=true" | jq -r '.id') | |
echo "$DEPLOYMENT_ID" > deployment-id.txt | |
- name: Update README.md with deployment state | |
run: | | |
DEPLOYMENT_ID=$(cat deployment-id.txt) | |
DEPLOYMENT_STATE="processing" | |
while [ "$DEPLOYMENT_STATE" = "processing" ]; do | |
DEPLOYMENT_STATE=$(curl -s "https://app.hatchbox.io/apps/${{ secrets.HATCHBOX_DEPLOY_KEY }}/activities/${DEPLOYMENT_ID}" | jq -r '.state') | |
sleep 5 | |
done | |
if [ "$DEPLOYMENT_STATE" = "completed" ]; then | |
DEPLOYMENT_STATE="تم النشر بنجاح" | |
elif [ "$DEPLOYMENT_STATE" = "failed" ]; then | |
DEPLOYMENT_STATE="فشلت عملية النشر" | |
fi | |
sed -i 's|حالة نشر الموقع:.*|حالة نشر الموقع: <a href="https://app.hatchbox.io/apps/${{ secrets.HATCHBOX_APP_ID }}/logs/'"$DEPLOYMENT_ID"'">'"$DEPLOYMENT_STATE"'</a>|' README.md | |
- name: Push updated README.md | |
run: | | |
git config user.name "GitHub Action" | |
git config user.email "[email protected]" | |
git add README.md | |
git commit -m "Update deployment state in README.md" | |
git push |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment