@@ -3,83 +3,134 @@ name: Build and Publish
33on :
44 push :
55 branches : [ main ]
6+ pull_request :
7+ branches : [ main ]
68 workflow_dispatch :
79
10+ env :
11+ IMAGE_NAME : ghcr.io/bahnew/clinical-frontend
12+ NODE_VERSION : ' 22'
13+
814jobs :
9- build -and-publish :
15+ test -and-lint :
1016 runs-on : ubuntu-latest
11- permissions :
12- contents : read
13- packages : write
1417 steps :
1518 - name : Checkout code
1619 uses : actions/checkout@v4
17-
20+
1821 - name : Set up Node.js
1922 uses : actions/setup-node@v4
2023 with :
21- node-version : ' 22 '
24+ node-version : ${{ env.NODE_VERSION }}
2225 cache : ' yarn'
23-
26+
2427 - name : Install dependencies
2528 run : yarn install --frozen-lockfile
26-
29+
2730 - name : Lint
2831 run : yarn lint
29-
32+
3033 - name : Run tests
3134 run : yarn test
32-
35+
3336 - name : Scan for package vulnerabilities
3437 uses : aquasecurity/trivy-action@0.30.0
3538 with :
3639 scan-type : ' fs'
3740 format : ' table'
3841 exit-code : ' 1'
39- severity : ' CRITICAL'
42+ severity : ' CRITICAL,HIGH '
4043
44+ build-and-publish :
45+ needs : test-and-lint
46+ if : github.event_name == 'push' || github.event_name == 'workflow_dispatch'
47+ runs-on : ubuntu-latest
48+ permissions :
49+ contents : read
50+ packages : write
51+ steps :
52+ - name : Checkout code
53+ uses : actions/checkout@v4
54+
55+ - name : Set up Node.js
56+ uses : actions/setup-node@v4
57+ with :
58+ node-version : ${{ env.NODE_VERSION }}
59+ cache : ' yarn'
60+
61+ - name : Install dependencies
62+ run : yarn install --frozen-lockfile
63+
64+ - name : Extract version
65+ id : package-version
66+ run : echo "version=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT
67+
4168 - name : Build
4269 run : yarn build
43-
70+
71+ - name : Cache build artifacts
72+ uses : actions/cache@v3
73+ with :
74+ path : dist
75+ key : ${{ runner.os }}-build-${{ github.sha }}
76+
4477 - name : Set up Docker Buildx
4578 uses : docker/setup-buildx-action@v3
46-
79+
4780 - name : Login to GitHub Container Registry
4881 uses : docker/login-action@v3
4982 with :
5083 registry : ghcr.io
5184 username : ${{ github.actor }}
5285 password : ${{ secrets.GITHUB_TOKEN }}
53-
54- - name : Build Docker image
86+
87+ # Build image locally without pushing
88+ - name : Build multi-arch image locally
5589 uses : docker/build-push-action@v5
5690 with :
5791 context : .
5892 file : package/docker/Dockerfile
93+ platforms : linux/amd64,linux/arm64
5994 push : false
6095 load : true
61- tags : clinical-frontend:local
96+ tags : |
97+ ${{ env.IMAGE_NAME }}:local-scan
6298 cache-from : type=gha
6399 cache-to : type=gha,mode=max
64-
100+
101+ # Scan the local image before pushing
65102 - name : Scan Docker image for vulnerabilities
66103 uses : aquasecurity/trivy-action@0.30.0
67104 with :
68- image-ref : ' clinical-frontend :local'
105+ image-ref : ' ${{ env.IMAGE_NAME }} :local-scan '
69106 format : ' table'
70107 exit-code : ' 1'
71- severity : ' CRITICAL'
72-
73- - name : Tag and push Docker image
74- run : |
75- docker tag clinical-frontend:local ghcr.io/bahnew/clinical-frontend:latest
76- docker push ghcr.io/bahnew/clinical-frontend:latest
77-
78-
108+ severity : ' CRITICAL,HIGH'
109+
110+ # Only push if security scan passes
111+ - name : Push multi-arch image
112+ if : success()
113+ uses : docker/build-push-action@v5
114+ with :
115+ context : .
116+ file : package/docker/Dockerfile
117+ platforms : linux/amd64,linux/arm64
118+ push : true
119+ tags : |
120+ ${{ env.IMAGE_NAME }}:latest
121+ ${{ env.IMAGE_NAME }}:${{ github.sha }}
122+ ${{ env.IMAGE_NAME }}:v${{ steps.package-version.outputs.version }}
123+ cache-from : type=gha
124+ cache-to : type=gha,mode=max
125+
126+ # This step dispatches an event to the utils repo to trigger downstream deployments
79127 - name : Repository Dispatch
128+ if : success()
80129 run : |
130+ # Using GitHub's built-in REST API for repository dispatch
81131 curl -X POST \
82132 -H "Authorization: token ${{ secrets.INFRA_GITHUB_TOKEN }}" \
83133 -H "Accept: application/vnd.github.v3+json" \
84134 https://api.github.com/repos/bahnew/utils/dispatches \
85- -d '{"event_type":"clinical-frontend-publish"}'
135+ -d '{"event_type":"clinical-frontend-publish","client_payload":{"version":"v${{ steps.package-version.outputs.version }}","sha":"${{ github.sha }}"}}'
136+
0 commit comments