Reorganize repository structure with src/ folder and add Docker support with CI/CD #3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Docker Build and Publish | |
| on: | |
| push: | |
| branches: [ master, main, develop ] | |
| tags: [ 'v*' ] | |
| pull_request: | |
| branches: [ master, main, develop ] | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| strategy: | |
| matrix: | |
| project: | |
| - name: image-filter-udagram | |
| path: src/project/image-filter-udagram-app | |
| - name: udacity-c2-frontend | |
| path: src/project/c2-microservices-v1/udacity-c2-frontend | |
| - name: udacity-c2-restapi-feed | |
| path: src/project/c2-microservices-v1/udacity-c2-restapi-feed | |
| - name: udacity-c2-restapi-user | |
| path: src/project/c2-microservices-v1/udacity-c2-restapi-user | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to Container Registry | |
| if: github.event_name != 'pull_request' | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/${{ matrix.project.name }} | |
| tags: | | |
| type=ref,event=branch | |
| type=ref,event=pr | |
| type=semver,pattern={{version}} | |
| type=semver,pattern={{major}}.{{minor}} | |
| type=sha,prefix={{branch}}- | |
| labels: | | |
| org.opencontainers.image.title=${{ matrix.project.name }} | |
| org.opencontainers.image.description=Udacity AWS Developer Project - ${{ matrix.project.name }} | |
| - name: Check if project has package.json | |
| id: check-project | |
| run: | | |
| if [ -f "${{ matrix.project.path }}/package.json" ]; then | |
| echo "has_package=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "has_package=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Build and push Docker image | |
| if: steps.check-project.outputs.has_package == 'true' | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: ./Dockerfile | |
| build-args: | | |
| PROJECT_PATH=${{ matrix.project.path }} | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| build-individual-dockerfiles: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| strategy: | |
| matrix: | |
| project: | |
| - name: c2-frontend-ionic | |
| dockerfile: src/project/c2-microservices-v1/udacity-c2-frontend/Dockerfile | |
| context: src/project/c2-microservices-v1/udacity-c2-frontend | |
| - name: c2-restapi-feed-individual | |
| dockerfile: src/project/c2-microservices-v1/udacity-c2-restapi-feed/Dockerfile | |
| context: src/project/c2-microservices-v1/udacity-c2-restapi-feed | |
| - name: c2-restapi-user-individual | |
| dockerfile: src/project/c2-microservices-v1/udacity-c2-restapi-user/Dockerfile | |
| context: src/project/c2-microservices-v1/udacity-c2-restapi-user | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to Container Registry | |
| if: github.event_name != 'pull_request' | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/${{ matrix.project.name }} | |
| tags: | | |
| type=ref,event=branch | |
| type=ref,event=pr | |
| type=semver,pattern={{version}} | |
| type=semver,pattern={{major}}.{{minor}} | |
| type=sha,prefix={{branch}}- | |
| - name: Check if Dockerfile exists | |
| id: check-dockerfile | |
| run: | | |
| if [ -f "${{ matrix.project.dockerfile }}" ]; then | |
| echo "has_dockerfile=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "has_dockerfile=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Build and push Docker image | |
| if: steps.check-dockerfile.outputs.has_dockerfile == 'true' | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ${{ matrix.project.context }} | |
| file: ${{ matrix.project.dockerfile }} | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max |