Skip
Arish's avatar

19. Complete CI/CD Workflow


Full Rails CI/CD Pipeline

yaml
1name: CI/CD
2
3on:
4  push:
5    branches: [main, develop]
6  pull_request:
7    branches: [main, develop]
8
9env:
10  RAILS_ENV: test
11  DATABASE_URL: postgres://postgres:postgres@localhost:5432/test
12
13jobs:
14  test:
15    runs-on: ubuntu-latest
16    services:
17      postgres:
18        image: postgres:15
19        env:
20          POSTGRES_PASSWORD: postgres
21        ports: ['5432:5432']
22        options: --health-cmd pg_isready --health-interval 10s
23      redis:
24        image: redis:7
25        ports: ['6379:6379']
26    
27    steps:
28      - uses: actions/checkout@v4
29      - uses: ruby/setup-ruby@v1
30        with:
31          ruby-version: '3.2'
32          bundler-cache: true
33      
34      - name: Setup database
35        run: bundle exec rails db:schema:load
36      
37      - name: Run tests
38        run: bundle exec rspec
39
40  lint:
41    runs-on: ubuntu-latest
42    steps:
43      - uses: actions/checkout@v4
44      - uses: ruby/setup-ruby@v1
45        with:
46          ruby-version: '3.2'
47          bundler-cache: true
48      - run: bundle exec rubocop
49
50  security:
51    runs-on: ubuntu-latest
52    steps:
53      - uses: actions/checkout@v4
54      - uses: ruby/setup-ruby@v1
55      - run: |
56          bundle exec brakeman -q
57          bundle exec bundle-audit check --update
58
59  build:
60    needs: [test, lint, security]
61    runs-on: ubuntu-latest
62    steps:
63      - uses: actions/checkout@v4
64      - uses: docker/login-action@v3
65        with:
66          registry: ghcr.io
67          username: ${{ github.actor }}
68          password: ${{ secrets.GITHUB_TOKEN }}
69      - uses: docker/build-push-action@v5
70        with:
71          push: true
72          tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
73
74  deploy:
75    needs: build
76    if: github.ref == 'refs/heads/main'
77    runs-on: ubuntu-latest
78    environment: production
79    steps:
80      - uses: actions/checkout@v4
81      - name: Deploy
82        run: ./deploy.sh
83        env:
84          IMAGE_TAG: ${{ github.sha }}