Skip to content

Commit 0376e50

Browse files
[fix] simplify main workflow
1 parent dd586f6 commit 0376e50

1 file changed

Lines changed: 27 additions & 122 deletions

File tree

.github/workflows/main.yml

Lines changed: 27 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,185 +1,90 @@
1-
name: 🚀 Docker CI/CD Pipeline
1+
name: Docker CI/CD Pipeline
22

33
on:
44
push:
55
branches: [main, develop]
66
pull_request:
77
branches: [main, develop]
8+
workflow_dispatch:
89

910
concurrency:
1011
group: ${{ github.workflow }}-${{ github.ref }}
1112
cancel-in-progress: true
1213

1314
env:
1415
NODE_VERSION: "22.11.0"
15-
IMAGE_NAME: docker-reactjs-sample
16-
DOCKER_REPO: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKERHUB_PROJECT_NAME }}
17-
18-
permissions:
19-
contents: read
20-
id-token: write
16+
DOCKERHUB_REPO: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKERHUB_PROJECT_NAME }}
2117

2218
jobs:
23-
# Step 1: Install dependencies and save them as an artifact
24-
dependencies:
25-
name: Install Dependencies & Artifacts
26-
runs-on: ubuntu-latest
27-
steps:
28-
- uses: actions/checkout@v4
29-
30-
- name: Setup Node.js
31-
uses: actions/setup-node@v4
32-
with:
33-
node-version: ${{ env.NODE_VERSION }}
34-
35-
- name: Cache npm dependencies
36-
uses: actions/cache@v4
37-
with:
38-
path: ~/.npm
39-
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
40-
restore-keys: |
41-
${{ runner.os }}-node-
42-
43-
- name: Install dependencies
44-
run: npm ci
45-
46-
- name: Upload node_modules artifact
47-
uses: actions/upload-artifact@v4
48-
with:
49-
name: node-modules
50-
path: node_modules/
51-
retention-days: 1
52-
53-
# Step 2: Lint (Uses downloaded artifact)
19+
# Step 1: Code Quality Checks
5420
lint:
5521
name: Lint Code
5622
runs-on: ubuntu-latest
57-
needs: [dependencies]
5823

5924
steps:
60-
- uses: actions/checkout@v4
25+
- name: Checkout code
26+
uses: actions/checkout@v4
6127

6228
- name: Setup Node.js
6329
uses: actions/setup-node@v4
6430
with:
6531
node-version: ${{ env.NODE_VERSION }}
32+
cache: npm
6633

67-
- name: Download node_modules artifact
68-
uses: actions/download-artifact@v4
69-
with:
70-
name: node-modules
71-
path: node_modules
34+
- name: Install dependencies
35+
run: npm ci
7236

7337
- name: Run ESLint
7438
run: npm run lint
7539

76-
# Step 3: Test Suite (Uses downloaded artifact)
40+
# Step 2: Run Tests
7741
test:
7842
name: Run Tests
7943
runs-on: ubuntu-latest
80-
needs: [dependencies]
8144

8245
steps:
83-
- uses: actions/checkout@v4
46+
- name: Checkout code
47+
uses: actions/checkout@v4
8448

8549
- name: Setup Node.js
8650
uses: actions/setup-node@v4
8751
with:
8852
node-version: ${{ env.NODE_VERSION }}
53+
cache: npm
8954

90-
- name: Download node_modules artifact
91-
uses: actions/download-artifact@v4
92-
with:
93-
name: node-modules
94-
path: node_modules
55+
- name: Install dependencies
56+
run: npm ci
57+
58+
- name: Run tests
59+
run: npm run test
9560

9661
- name: Run tests with coverage
9762
run: npm run test:coverage
9863

99-
- name: Upload coverage report
64+
- name: Upload coverage reports
10065
uses: actions/upload-artifact@v4
10166
if: always()
10267
with:
10368
name: coverage-report
10469
path: coverage/
10570
retention-days: 7
10671

107-
# Step 4: Docker Build (Needs successful Lint and Test)
108-
build:
109-
name: Build Docker Image
110-
runs-on: ubuntu-latest
111-
needs: [lint, test]
112-
113-
steps:
114-
- uses: actions/checkout@v4
115-
116-
- name: Set up Docker Buildx
117-
uses: docker/setup-buildx-action@v3
118-
119-
- name: Extract metadata (tags, labels)
120-
id: meta
121-
uses: docker/metadata-action@v5
122-
with:
123-
images: ${{ env.DOCKER_REPO }}
124-
125-
- name: Build production image
126-
uses: docker/build-push-action@v6
127-
with:
128-
context: .
129-
file: ./Dockerfile
130-
target: production
131-
tags: ${{ steps.meta.outputs.tags }}
132-
build-args: |
133-
NODE_VERSION=${{ env.NODE_VERSION }}
134-
cache-from: type=gha,scope=${{ github.ref_name }}
135-
cache-to: type=gha,mode=max,scope=${{ github.ref_name }}
136-
load: true
137-
138-
- name: Verify image tag (e.g., latest tag for main branch)
139-
run: |
140-
TAGS="${{ steps.meta.outputs.tags }}"
141-
echo "Tags generated: $TAGS"
142-
# Simple verification for one of the tags created (e.g., the first one)
143-
FIRST_TAG=$(echo "$TAGS" | head -n 1 | tr -d '\n')
144-
docker images "$FIRST_TAG"
145-
146-
# Step 5: Deploy to Docker Hub
147-
deploy:
148-
name: Deploy to Docker Hub
149-
runs-on: ubuntu-latest
150-
needs: [build]
151-
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
152-
153-
steps:
154-
- uses: actions/checkout@v4
155-
156-
- uses: docker/setup-qemu-action@v3
157-
- uses: docker/setup-buildx-action@v3
158-
159-
- name: Extract metadata (tags, labels)
160-
id: meta
161-
uses: docker/metadata-action@v5
162-
with:
163-
images: ${{ env.DOCKER_REPO }}
164-
72+
# Step 3:
16573
- name: Log in to Docker Hub
16674
uses: docker/login-action@v3
16775
with:
16876
username: ${{ secrets.DOCKER_USERNAME }}
16977
password: ${{ secrets.DOCKERHUB_TOKEN }}
17078

171-
- name: Build and push image to Docker Hub
79+
# Step 4. Build and push production Docker image
80+
- name: Build and push production image
17281
uses: docker/build-push-action@v6
17382
with:
17483
context: .
175-
file: ./Dockerfile
176-
target: production
177-
platforms: linux/amd64,linux/arm64
84+
file: Dockerfile
17885
push: true
86+
platforms: linux/amd64,linux/arm64
17987
tags: |
180-
${{ steps.meta.outputs.tags }}
181-
${{ env.DOCKER_REPO }}:${{ github.ref_name }}
182-
build-args: |
183-
NODE_VERSION=${{ env.NODE_VERSION }}
184-
cache-from: type=gha,scope=${{ github.ref_name }}
185-
cache-to: type=gha,mode=max,scope=${{ github.ref_name }}
88+
${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKERHUB_PROJECT_NAME }}:latest
89+
${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKERHUB_PROJECT_NAME }}:${{ steps.meta.outputs.SHORT_SHA }}
90+
cache-from: type=local,src=/tmp/.buildx-cache

0 commit comments

Comments
 (0)