Vizzuality/landgriffon

View on GitHub
.github/workflows/publish-docker-images.yml

Summary

Maintainability
Test Coverage
name: Publish Docker images

env:
  PROJECT_ID: ${{ secrets.GCP_PROJECT }}
  REGION: ${{ secrets.GCP_REGION }}

on:
  push:
    branches:
      - 'main'
      - 'staging'
      - 'test'
      - 'expo'
      - 'gcp'
      - 'dev'
      - 'demo'
    paths:
      - 'api/**'
      - 'client/**'
      - 'marketing/**'
      - 'tiler/**'
      - 'data/**'
      - '.github/**'
  workflow_dispatch:
    inputs:
      waitForTest:
        description: 'Set to "false" to skip waiting for the test to pass.'
        required: true
        default: true

jobs:
  wait_for_tests:
    name: Wait for tests to finish running
    runs-on: ubuntu-20.04
    steps:
      - name: Wait for API E2E tests to run
        if: ${{ github.event.inputs.waitForTest == 'true' }}
        uses: fountainhead/action-wait-for-check@v1.1.0
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          checkName: E2E Tests
          ref: ${{ github.event.pull_request.head.sha || github.sha }}

      - name: Wait for API Integration tests to run
        if: ${{ github.event.inputs.waitForTest == 'true' }}
        uses: fountainhead/action-wait-for-check@v1.1.0
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          checkName: Integration Tests
          ref: ${{ github.event.pull_request.head.sha || github.sha }}

      - name: Wait for Client tests to run
        if: ${{ github.event.inputs.waitForTest == 'true' }}
        uses: fountainhead/action-wait-for-check@v1.1.0
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          checkName: Client Tests
          ref: ${{ github.event.pull_request.head.sha || github.sha }}

  push_api_to_registry:
    name: Push API Docker image to Docker Hub
    runs-on: ubuntu-20.04
    needs: wait_for_tests

    permissions:
      contents: 'read'
      id-token: 'write'

    steps:
      - name: Check out the repo
        uses: actions/checkout@v3

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}

      - name: Get ECR Registry
        id: ecr-login
        uses: aws-actions/amazon-ecr-login@v1

      - name: 'Authenticate to Google Cloud'
        uses: google-github-actions/auth@v1
        with:
          workload_identity_provider: 'projects/168301767246/locations/global/workloadIdentityPools/github-pool/providers/github-provider'
          service_account: 'github-actions@landgriffon.iam.gserviceaccount.com'

      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v1

      - name: Authorize Docker push
        run: gcloud auth configure-docker europe-west1-docker.pkg.dev

      - name: Extract branch name
        shell: bash
        run: |
          echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
          echo "##[set-output name=branch-upper;]$(echo ${GITHUB_REF#refs/heads/} | tr a-z A-Z )"
        id: extract_branch

      - name: Build API Docker image
        env:
          ECR_REGISTRY: ${{ steps.ecr-login.outputs.registry }}
          ECR_REPOSITORY: api
          IMAGE_TAG: ${{ steps.extract_branch.outputs.branch }}
        run: |
          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \
          -t europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/api/main:${{ github.sha }} \
          -t europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/api/main:latest \
          api

      - name: Push API Docker image to AWS and GCP
        env:
          ECR_REGISTRY: ${{ steps.ecr-login.outputs.registry }}
          ECR_REPOSITORY: api
          IMAGE_TAG: ${{ steps.extract_branch.outputs.branch }}
        run: |
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
          docker push europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/api/main:${{ github.sha }}
          docker push europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/api/main:latest

  push_client_to_registry:
    name: Push Client Docker image to Docker Hub
    runs-on: ubuntu-20.04
    needs: wait_for_tests

    permissions:
      contents: 'read'
      id-token: 'write'

    steps:
      - name: Check out the repo
        uses: actions/checkout@v3

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}

      - name: Get ECR Registry
        id: ecr-login
        uses: aws-actions/amazon-ecr-login@v1

      - name: 'Authenticate to Google Cloud'
        uses: google-github-actions/auth@v1
        with:
          workload_identity_provider: 'projects/168301767246/locations/global/workloadIdentityPools/github-pool/providers/github-provider'
          service_account: 'github-actions@landgriffon.iam.gserviceaccount.com'

      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v1

      - name: Authorize Docker push
        run: gcloud auth configure-docker europe-west1-docker.pkg.dev

      - name: Extract branch name
        shell: bash
        run: |
          echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
          echo "##[set-output name=branch-upper;]$(echo ${GITHUB_REF#refs/heads/} | tr a-z A-Z )"
        id: extract_branch

      - name: Build Client Docker image
        env:
          ECR_REGISTRY: ${{ steps.ecr-login.outputs.registry }}
          ECR_REPOSITORY: client
          IMAGE_TAG: ${{ steps.extract_branch.outputs.branch }}
        run: |
          docker build \
          --build-arg NEXTAUTH_URL=${{ secrets[format('NEXTAUTH_URL_{0}', steps.extract_branch.outputs.branch-upper )] }} \
          --build-arg NEXTAUTH_SECRET=${{ secrets[format('NEXTAUTH_SECRET_{0}', steps.extract_branch.outputs.branch-upper )] }} \
          --build-arg NEXT_PUBLIC_MAPBOX_API_TOKEN=${{ secrets.NEXT_PUBLIC_MAPBOX_API_TOKEN }} \
          --build-arg NEXT_PUBLIC_API_URL=${{ secrets[format('NEXT_PUBLIC_API_URL_{0}', steps.extract_branch.outputs.branch-upper )] }} \
          --build-arg CYPRESS_USERNAME=${{ secrets.CYPRESS_USERNAME }} \
          --build-arg CYPRESS_PASSWORD=${{ secrets.CYPRESS_PASSWORD }} \
          -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG  \
          -t europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/client/main:${{ github.sha }} \
          -t europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/client/main:latest \
          client

      - name: Push Client Docker image to AWS and GCP
        env:
          ECR_REGISTRY: ${{ steps.ecr-login.outputs.registry }}
          ECR_REPOSITORY: client
          IMAGE_TAG: ${{ steps.extract_branch.outputs.branch }}
        run: |
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
          docker push europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/client/main:${{ github.sha }}
          docker push europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/client/main:latest


  push_data_import_to_registry:
    name: Push Data Import Docker image to Docker Hub
    runs-on: ubuntu-20.04
    needs: wait_for_tests

    permissions:
      contents: 'read'
      id-token: 'write'

    steps:
      - name: Check out the repo
        uses: actions/checkout@v3

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}

      - name: Get ECR Registry
        id: ecr-login
        uses: aws-actions/amazon-ecr-login@v1

      - name: 'Authenticate to Google Cloud'
        uses: google-github-actions/auth@v1
        with:
          workload_identity_provider: 'projects/168301767246/locations/global/workloadIdentityPools/github-pool/providers/github-provider'
          service_account: 'github-actions@landgriffon.iam.gserviceaccount.com'

      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v1

      - name: Authorize Docker push
        run: gcloud auth configure-docker europe-west1-docker.pkg.dev

      - name: Extract branch name
        shell: bash
        run: |
          echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
          echo "##[set-output name=branch-upper;]$(echo ${GITHUB_REF#refs/heads/} | tr a-z A-Z )"
        id: extract_branch

      - name: Build Data Import Docker image
        env:
          ECR_REGISTRY: ${{ steps.ecr-login.outputs.registry }}
          ECR_REPOSITORY: data_import
          IMAGE_TAG: ${{ steps.extract_branch.outputs.branch }}
        run: |
          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG  \
          -t europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/data-import/main:${{ github.sha }} \
          -t europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/data-import/main:latest \
          data

      - name: Push Data Import Docker image to AWS and GCP
        env:
          ECR_REGISTRY: ${{ steps.ecr-login.outputs.registry }}
          ECR_REPOSITORY: data_import
          IMAGE_TAG: ${{ steps.extract_branch.outputs.branch }}
        run: |
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
          docker push europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/data-import/main:${{ github.sha }}
          docker push europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/data-import/main:latest

  push_tiler_to_registry:
    name: Push Tiler Docker image to AWS and GCP
    runs-on: ubuntu-20.04
    needs: wait_for_tests

    permissions:
      contents: 'read'
      id-token: 'write'

    steps:
      - name: Check out the repo
        uses: actions/checkout@v3

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}

      - name: Get ECR Registry
        id: ecr-login
        uses: aws-actions/amazon-ecr-login@v1

      - name: 'Authenticate to Google Cloud'
        uses: google-github-actions/auth@v1
        with:
          workload_identity_provider: 'projects/168301767246/locations/global/workloadIdentityPools/github-pool/providers/github-provider'
          service_account: 'github-actions@landgriffon.iam.gserviceaccount.com'

      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v1

      - name: Authorize Docker push
        run: gcloud auth configure-docker europe-west1-docker.pkg.dev

      - name: Extract branch name
        shell: bash
        run: |
          echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
          echo "##[set-output name=branch-upper;]$(echo ${GITHUB_REF#refs/heads/} | tr a-z A-Z )"
        id: extract_branch

      - name: Build Tiler Docker image
        env:
          ECR_REGISTRY: ${{ steps.ecr-login.outputs.registry }}
          ECR_REPOSITORY: tiler
          IMAGE_TAG: ${{ steps.extract_branch.outputs.branch }}
        run: |
          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \
          -t europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/tiler/main:${{ github.sha }} \
          -t europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/tiler/main:latest \
          tiler

      - name: Push Tiler Docker image to AWS and GCP
        env:
          ECR_REGISTRY: ${{ steps.ecr-login.outputs.registry }}
          ECR_REPOSITORY: tiler
          IMAGE_TAG: ${{ steps.extract_branch.outputs.branch }}
        run: |
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
          docker push europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/tiler/main:${{ github.sha }}
          docker push europe-west1-docker.pkg.dev/${{ env.PROJECT_ID }}/tiler/main:latest