Skip to content

Commit 3af1667

Browse files
[feat] improve github workflow
1 parent 66320f1 commit 3af1667

1 file changed

Lines changed: 34 additions & 35 deletions

File tree

.github/workflows/main.yml

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CI/CD Angular Application with Docker
1+
name: CI/CD Angular App with Docker
22

33
on:
44
push:
@@ -8,85 +8,84 @@ on:
88
types: [opened, synchronize, reopened]
99

1010
jobs:
11-
build-test-push:
12-
name: Build, Test, and Push Docker Image
11+
test:
12+
name: Run Angular Jest Tests
1313
runs-on: ubuntu-latest
1414

1515
steps:
16-
# 1. Checkout source code
17-
- name: Checkout source code
16+
- name: Checkout code
1817
uses: actions/checkout@v4
19-
with:
20-
fetch-depth: 0
2118

22-
# 2. Set up Docker Buildx
2319
- name: Set up Docker Buildx
2420
uses: docker/setup-buildx-action@v3
2521

26-
# 3. Cache Docker layers
27-
- name: Cache Docker layers
28-
uses: actions/cache@v4
29-
with:
30-
path: /tmp/.buildx-cache
31-
key: ${{ runner.os }}-buildx-${{ github.sha }}
32-
restore-keys: |
33-
${{ runner.os }}-buildx-
34-
35-
# 4. Cache npm dependencies
3622
- name: Cache npm dependencies
3723
uses: actions/cache@v4
3824
with:
3925
path: ~/.npm
4026
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
41-
restore-keys: |
42-
${{ runner.os }}-npm-
27+
restore-keys: ${{ runner.os }}-npm-
4328

44-
# 5. Extract metadata
45-
- name: Extract metadata
46-
id: meta
47-
run: |
48-
echo "REPO_NAME=${GITHUB_REPOSITORY##*/}" >> "$GITHUB_OUTPUT"
49-
echo "SHORT_SHA=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
50-
51-
# 6. Build dev Docker image
5229
- name: Build Docker image for tests
5330
uses: docker/build-push-action@v6
5431
with:
5532
context: .
5633
file: Dockerfile.dev
57-
tags: ${{ steps.meta.outputs.REPO_NAME }}-dev:latest
34+
tags: angular-app-test:latest
5835
load: true
5936
cache-from: type=local,src=/tmp/.buildx-cache
6037
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max
6138

62-
# 7. Run Angular tests with Jest
63-
- name: Run Angular Jest tests inside container
39+
- name: Run Jest tests in container
6440
run: |
6541
docker run --rm \
6642
--workdir /app \
6743
--entrypoint "" \
68-
${{ steps.meta.outputs.REPO_NAME }}-dev:latest \
44+
angular-app-test:latest \
6945
sh -c "npm ci && npm run test -- --ci"
7046
env:
7147
CI: true
7248
NODE_ENV: test
7349
timeout-minutes: 5
7450

75-
# 8. Log in to Docker Hub
51+
build-and-push:
52+
name: Build and Push Docker Image
53+
runs-on: ubuntu-latest
54+
needs: test
55+
56+
steps:
57+
- name: Checkout code
58+
uses: actions/checkout@v4
59+
60+
- name: Set up Docker Buildx
61+
uses: docker/setup-buildx-action@v3
62+
63+
- name: Cache Docker layers
64+
uses: actions/cache@v4
65+
with:
66+
path: /tmp/.buildx-cache
67+
key: ${{ runner.os }}-buildx-${{ github.sha }}
68+
restore-keys: ${{ runner.os }}-buildx-
69+
70+
- name: Extract metadata
71+
id: meta
72+
run: |
73+
echo "REPO_NAME=${GITHUB_REPOSITORY##*/}" >> "$GITHUB_OUTPUT"
74+
echo "SHORT_SHA=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
75+
7676
- name: Log in to Docker Hub
7777
uses: docker/login-action@v3
7878
with:
7979
username: ${{ secrets.DOCKER_USERNAME }}
8080
password: ${{ secrets.DOCKERHUB_TOKEN }}
8181

82-
# 9. Build and push production image
8382
- name: Build and push production image
8483
uses: docker/build-push-action@v6
8584
with:
8685
context: .
8786
file: Dockerfile
8887
push: true
89-
platforms: linux/amd64,linux/arm64
88+
platforms: linux/amd64
9089
tags: |
9190
${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKERHUB_PROJECT_NAME }}:latest
9291
${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKERHUB_PROJECT_NAME }}:${{ steps.meta.outputs.SHORT_SHA }}

0 commit comments

Comments
 (0)