|
| 1 | +name: Build & publish PeerPrep images |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + types: |
| 5 | + - opened |
| 6 | + - reopened |
| 7 | + - synchronize |
| 8 | + - ready_for_review |
| 9 | + push: |
| 10 | + branches: |
| 11 | + - main |
| 12 | + |
| 13 | +env: |
| 14 | + DOCKER_REGISTRY_USN: ay2425s1cs3219g16 |
| 15 | + USER_EXPRESS_PORT: 9001 |
| 16 | + QUESTION_EXPRESS_PORT: 9002 |
| 17 | + COLLAB_EXPRESS_PORT: 9003 |
| 18 | + MATCH_EXPRESS_PORT: 9004 |
| 19 | + FRONTEND_PORT: 3000 |
| 20 | + |
| 21 | +jobs: |
| 22 | + changes: |
| 23 | + if: ${{ !github.event.pull_request.draft && github.event.pull_request.title != 'Feedback' }} |
| 24 | + runs-on: ubuntu-latest |
| 25 | + # Required permissions |
| 26 | + permissions: |
| 27 | + pull-requests: read |
| 28 | + # Set job outputs to values from filter step |
| 29 | + outputs: |
| 30 | + matrix: ${{ steps.set-matrix.outputs.matrix }} # Output the matrix as a JSON string |
| 31 | + steps: |
| 32 | + - uses: actions/checkout@v4 |
| 33 | + if: contains(github.ref, 'main') |
| 34 | + # For pull requests it's not necessary to checkout the code |
| 35 | + - uses: dorny/paths-filter@v3 |
| 36 | + id: filter |
| 37 | + with: |
| 38 | + filters: | |
| 39 | + user: |
| 40 | + - 'backend/user/**' |
| 41 | + question: |
| 42 | + - 'backend/question/**' |
| 43 | + collaboration: |
| 44 | + - 'backend/collaboration/**' |
| 45 | + matching: |
| 46 | + - 'backend/matching/**' |
| 47 | + frontend: |
| 48 | + - 'frontend/**' |
| 49 | + - name: output-job-matrix |
| 50 | + id: set-matrix |
| 51 | + run: | |
| 52 | + is_main=${{ contains(github.ref, 'main') }} |
| 53 | + matrix=() |
| 54 | + if [[ "${{ steps.filter.outputs.user }}" == "true" || "$is_main" == "true" ]]; then |
| 55 | + config=$(jq -n \ |
| 56 | + --arg pkg "user" \ |
| 57 | + --arg img "$DOCKER_REGISTRY_USN/user-express" \ |
| 58 | + --arg ctx "./backend/user" \ |
| 59 | + --arg dkr "./backend/user/express.Dockerfile" \ |
| 60 | + --arg bag "port=$USER_EXPRESS_PORT" \ |
| 61 | + '{package: $pkg, image: $img, context: $ctx, dockerfile: $dkr, "build-args": $bag}') |
| 62 | + matrix+=("$config") |
| 63 | + fi |
| 64 | + if [[ "${{ steps.filter.outputs.question }}" == "true" || "$is_main" == "true" ]]; then |
| 65 | + config=$(jq -n \ |
| 66 | + --arg pkg "question" \ |
| 67 | + --arg img "$DOCKER_REGISTRY_USN/question-express" \ |
| 68 | + --arg ctx "./backend/question" \ |
| 69 | + --arg dkr "./backend/question/express.Dockerfile" \ |
| 70 | + --arg bag "port=$QUESTION_EXPRESS_PORT" \ |
| 71 | + '{package: $pkg, image: $img, context: $ctx, dockerfile: $dkr, "build-args": $bag}') |
| 72 | + matrix+=("$config") |
| 73 | + fi |
| 74 | + if [[ "${{ steps.filter.outputs.collaboration }}" == "true" || "$is_main" == "true" ]]; then |
| 75 | + config=$(jq -n \ |
| 76 | + --arg pkg "collaboration" \ |
| 77 | + --arg img "$DOCKER_REGISTRY_USN/collab-express" \ |
| 78 | + --arg ctx "./backend/collaboration" \ |
| 79 | + --arg dkr "./backend/collaboration/express.Dockerfile" \ |
| 80 | + --arg bag "port=$COLLAB_EXPRESS_PORT" \ |
| 81 | + '{package: $pkg, image: $img, context: $ctx, dockerfile: $dkr, "build-args": $bag}') |
| 82 | + matrix+=("$config") |
| 83 | + fi |
| 84 | + if [[ "${{ steps.filter.outputs.matching }}" == "true" || "$is_main" == "true" ]]; then |
| 85 | + config=$(jq -n \ |
| 86 | + --arg pkg "matching" \ |
| 87 | + --arg img "$DOCKER_REGISTRY_USN/match-express" \ |
| 88 | + --arg ctx "./backend/matching" \ |
| 89 | + --arg dkr "./backend/matching/express.Dockerfile" \ |
| 90 | + --arg bag "port=$MATCH_EXPRESS_PORT" \ |
| 91 | + '{package: $pkg, image: $img, context: $ctx, dockerfile: $dkr, "build-args": $bag}') |
| 92 | + matrix+=("$config") |
| 93 | + fi |
| 94 | + if [[ "${{ steps.filter.outputs.frontend }}" == "true" || "$is_main" == "true" ]]; then |
| 95 | + config=$(jq -n \ |
| 96 | + --arg pkg "frontend" \ |
| 97 | + --arg img "$DOCKER_REGISTRY_USN/frontend" \ |
| 98 | + --arg ctx "./frontend" \ |
| 99 | + --arg dkr "./frontend/frontend.Dockerfile" \ |
| 100 | + --arg bag "port=$FRONTEND_PORT" \ |
| 101 | + '{package: $pkg, image: $img, context: $ctx, dockerfile: $dkr, "build-args": $bag}') |
| 102 | + matrix+=("$config") |
| 103 | + fi |
| 104 | + formatted_matrix=$(echo "${matrix[@]}" | jq -cs .) |
| 105 | + echo "Outputs Generated: $formatted_matrix" |
| 106 | + echo "matrix=$formatted_matrix" >> $GITHUB_OUTPUT |
| 107 | +
|
| 108 | + build-and-push-image: |
| 109 | + needs: changes |
| 110 | + if: ${{ fromJson(needs.changes.outputs.matrix)[0] != null }} |
| 111 | + runs-on: ubuntu-latest |
| 112 | + strategy: |
| 113 | + fail-fast: false |
| 114 | + matrix: |
| 115 | + include: ${{ fromJson(needs.changes.outputs.matrix) }} # Use the matrix from the first job |
| 116 | + # - package: user |
| 117 | + # image: ay2425s1cs3219g16/user-express |
| 118 | + # context: ./backend/user |
| 119 | + # dockerfile: ./backend/user/express.Dockerfile |
| 120 | + # build-args: | |
| 121 | + # port=9001 |
| 122 | + # - package: question |
| 123 | + # image: ay2425s1cs3219g16/question-express |
| 124 | + # context: ./backend/question |
| 125 | + # dockerfile: ./backend/question/express.Dockerfile |
| 126 | + # build-args: | |
| 127 | + # port=9002 |
| 128 | + # - package: collaboration |
| 129 | + # image: ay2425s1cs3219g16/collab-express |
| 130 | + # context: ./backend/collaboration |
| 131 | + # dockerfile: ./backend/collaboration/express.Dockerfile |
| 132 | + # build-args: | |
| 133 | + # port=9003 |
| 134 | + # - package: matching |
| 135 | + # image: ay2425s1cs3219g16/match-express |
| 136 | + # context: ./backend/matching |
| 137 | + # dockerfile: ./backend/matching/express.Dockerfile |
| 138 | + # build-args: | |
| 139 | + # port=9004 |
| 140 | + # - package: frontend |
| 141 | + # image: ay2425s1cs3219g16/frontend |
| 142 | + # context: ./frontend |
| 143 | + # dockerfile: ./frontend/frontend.Dockerfile |
| 144 | + # build-args: | |
| 145 | + # port=3000 |
| 146 | + permissions: |
| 147 | + contents: read |
| 148 | + packages: write |
| 149 | + |
| 150 | + steps: |
| 151 | + - name: Checkout repository |
| 152 | + uses: actions/checkout@v4 |
| 153 | + |
| 154 | + - name: Set up QEMU |
| 155 | + uses: docker/setup-qemu-action@v3 |
| 156 | + |
| 157 | + - name: Set up Docker Buildx |
| 158 | + uses: docker/setup-buildx-action@v3 |
| 159 | + |
| 160 | + - name: Log in to the Container registry |
| 161 | + uses: docker/login-action@v3 |
| 162 | + with: |
| 163 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 164 | + password: ${{ secrets.DOCKER_PASSWORD }} |
| 165 | + |
| 166 | + - name: Extract metadata (tags, labels) for Docker |
| 167 | + id: meta |
| 168 | + uses: docker/metadata-action@v5 |
| 169 | + with: |
| 170 | + images: ${{ matrix.image }} |
| 171 | + |
| 172 | + - name: Build and push Docker images for PeerPrep Services |
| 173 | + uses: docker/build-push-action@v6 |
| 174 | + with: |
| 175 | + platforms: linux/amd64,linux/arm64 |
| 176 | + context: ${{ matrix.context }} |
| 177 | + file: ${{ matrix.dockerfile }} |
| 178 | + build-args: ${{ matrix.build-args }} |
| 179 | + push: true |
| 180 | + tags: ${{ steps.meta.outputs.tags }} |
| 181 | + labels: ${{ steps.meta.outputs.labels }} |
| 182 | + cache-from: type=gha |
| 183 | + cache-to: type=gha,mode=max |
| 184 | + |
| 185 | + results: |
| 186 | + if: ${{ always() }} |
| 187 | + runs-on: ubuntu-latest |
| 188 | + name: Final Results |
| 189 | + needs: build-and-push-image |
| 190 | + steps: |
| 191 | + - run: | |
| 192 | + result="${{ needs.build-and-push-image.result }}" |
| 193 | + if [[ $result == "success" || $result == "skipped" ]]; then |
| 194 | + exit 0 |
| 195 | + else |
| 196 | + exit 1 |
| 197 | + fi |
0 commit comments