|
1 | | -name: CI/CD Angular Application with Docker |
| 1 | +name: CI/CD – Angular App with Docker |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
|
8 | 8 | types: [opened, synchronize, reopened] |
9 | 9 |
|
10 | 10 | jobs: |
11 | | - build-test-push: |
12 | | - name: Build, Test, and Push Docker Image |
| 11 | + test: |
| 12 | + name: Run Angular Jest Tests |
13 | 13 | runs-on: ubuntu-latest |
14 | 14 |
|
15 | 15 | steps: |
16 | | - # 1. Checkout source code |
17 | | - - name: Checkout source code |
| 16 | + - name: Checkout code |
18 | 17 | uses: actions/checkout@v4 |
19 | | - with: |
20 | | - fetch-depth: 0 |
21 | 18 |
|
22 | | - # 2. Set up Docker Buildx |
23 | 19 | - name: Set up Docker Buildx |
24 | 20 | uses: docker/setup-buildx-action@v3 |
25 | 21 |
|
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 |
36 | 22 | - name: Cache npm dependencies |
37 | 23 | uses: actions/cache@v4 |
38 | 24 | with: |
39 | 25 | path: ~/.npm |
40 | 26 | key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }} |
41 | | - restore-keys: | |
42 | | - ${{ runner.os }}-npm- |
| 27 | + restore-keys: ${{ runner.os }}-npm- |
43 | 28 |
|
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 |
52 | 29 | - name: Build Docker image for tests |
53 | 30 | uses: docker/build-push-action@v6 |
54 | 31 | with: |
55 | 32 | context: . |
56 | 33 | file: Dockerfile.dev |
57 | | - tags: ${{ steps.meta.outputs.REPO_NAME }}-dev:latest |
| 34 | + tags: angular-app-test:latest |
58 | 35 | load: true |
59 | 36 | cache-from: type=local,src=/tmp/.buildx-cache |
60 | 37 | cache-to: type=local,dest=/tmp/.buildx-cache,mode=max |
61 | 38 |
|
62 | | - # 7. Run Angular tests with Jest |
63 | | - - name: Run Angular Jest tests inside container |
| 39 | + - name: Run Jest tests in container |
64 | 40 | run: | |
65 | 41 | docker run --rm \ |
66 | 42 | --workdir /app \ |
67 | 43 | --entrypoint "" \ |
68 | | - ${{ steps.meta.outputs.REPO_NAME }}-dev:latest \ |
| 44 | + angular-app-test:latest \ |
69 | 45 | sh -c "npm ci && npm run test -- --ci" |
70 | 46 | env: |
71 | 47 | CI: true |
72 | 48 | NODE_ENV: test |
73 | 49 | timeout-minutes: 5 |
74 | 50 |
|
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 | +
|
76 | 76 | - name: Log in to Docker Hub |
77 | 77 | uses: docker/login-action@v3 |
78 | 78 | with: |
79 | 79 | username: ${{ secrets.DOCKER_USERNAME }} |
80 | 80 | password: ${{ secrets.DOCKERHUB_TOKEN }} |
81 | 81 |
|
82 | | - # 9. Build and push production image |
83 | 82 | - name: Build and push production image |
84 | 83 | uses: docker/build-push-action@v6 |
85 | 84 | with: |
86 | 85 | context: . |
87 | 86 | file: Dockerfile |
88 | 87 | push: true |
89 | | - platforms: linux/amd64,linux/arm64 |
| 88 | + platforms: linux/amd64 |
90 | 89 | tags: | |
91 | 90 | ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKERHUB_PROJECT_NAME }}:latest |
92 | 91 | ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKERHUB_PROJECT_NAME }}:${{ steps.meta.outputs.SHORT_SHA }} |
|
0 commit comments