i have a question i have multiple packages using p...
# general
d
i have a question i have multiple packages using pants and they are running on google cloud run i want to integrate google cloud build where whenever a new commit is pushed the build is triggered only for a specific package that is being changed any idea how can i do this?
c
I haven't used google cloud build, but you could probably use the
--changed-since
selector, maybe
pants package --changed-since=HEAD^
?
p
I have a github action that triggers cloudbuild. It uses
--changed-since
to get a list of apps to build, then fires off cloud build commands for each
Copy code
- name: Get affected projects
  id: get_affected_projects
  run: |
    affected_projects=$(pants --changed-since=origin/main --changed-dependents=transitive list | grep "^src" | grep -v ":"  | cut -f 2 -d '/' | sort | uniq | tr '\n' ',' | tr -d '[:space:]')
    echo "AFFECTED_PROJECTS=${affected_projects}" >> $GITHUB_ENV

- name: Get image tag
  id: get_branch_and_sha
  run: |
    if [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then
      BRANCH_NAME="${GITHUB_HEAD_REF}"
    else
      BRANCH_NAME="${GITHUB_REF_NAME##*/}"
    fi
    BRANCH_NAME="${BRANCH_NAME//\//_}"
    SHORT_SHA=$(git rev-parse --short HEAD)
    IMAGE_TAG="${BRANCH_NAME}-${SHORT_SHA}-$(date +%s)"
    echo "github_branch=$BRANCH_NAME" >> $GITHUB_ENV
    echo "short_sha=$SHORT_SHA" >> $GITHUB_ENV
    echo "image_tag=$IMAGE_TAG" >> $GITHUB_ENV

- name: Build and push images
  run: |
    REPO="gcp-region-docker.pkg.dev/image-repo-6550/my-docker-repo"
    if [ -z "${{ env.AFFECTED_PROJECTS }}" ]; then
      echo "No affected projects to build."
    else
      for project in $(echo ${{ env.AFFECTED_PROJECTS }} | tr ',' '\n'); do
        if [ -f "src/${project}/cloudbuild.yaml" ]; then
          gcloud builds submit --config=src/${project}/cloudbuild.yaml \
          --substitutions=_BRANCH_NO_PREFIX=${{ env.github_branch }},SHORT_SHA=${{ env.short_sha }},COMMIT_SHA=$(git rev-parse HEAD) \
          --project image-repo-6550
        else
          echo "${project} not in src - not building docker image"
        fi
      done
    fi