diff --git a/.github/workflows/build-push-image-commit.yaml b/.github/workflows/build-push-create-pr.yaml similarity index 83% rename from .github/workflows/build-push-image-commit.yaml rename to .github/workflows/build-push-create-pr.yaml index e9bf32c..07854ec 100644 --- a/.github/workflows/build-push-image-commit.yaml +++ b/.github/workflows/build-push-create-pr.yaml @@ -1,5 +1,6 @@ name: Build and push container image, and push update to datahub repo if needed on: + workflow_dispatch: push: branches: - main @@ -67,6 +68,10 @@ jobs: update-deployment-image-tag: runs-on: ubuntu-latest needs: build-and-push + permissions: + contents: write + pull-requests: write + repository-projects: write env: HUB: ${{ vars.HUB }} IMAGE: ${{ vars.IMAGE }} @@ -92,15 +97,17 @@ jobs: - name: Update the tag for any deployments that use this image if: ${{ env.IMAGE_TAG }} run: | - for deployment in $(grep -lr ${IMAGE} deployments/ | grep hubploy.yaml); do + for deployment in $(grep -lr ${IMAGE} deployments/); do old_hash=$(grep ${IMAGE} ${deployment} | awk -F":" '{print $3}') new_hash=${IMAGE_TAG} sed -i -e "s,${IMAGE}:${old_hash},${IMAGE}:${new_hash},g" ${deployment} echo "Updated ${deployment} with new image tag ${new_hash}" done - - name: Create feature branch, add, commit and push changes + - name: Create feature branch, add, commit, push changes and open a pull request if: ${{ env.IMAGE_TAG }} + env: + GH_TOKEN: ${{ secrets.DATAHUB_CREATE_PR }} run: | CHANGED_FILES=$(git status --porcelain -uno | awk '{print $2}') git diff @@ -109,8 +116,21 @@ jobs: for file in $(echo -e ${CHANGED_FILES}); do git add ${file} done - git commit -m "update ${HUB} image tag to ${IMAGE_TAG}: ${CHANGED_FILES}" - git push origin update-${HUB}-image-tag-${IMAGE_TAG} + + BRANCH="update-${HUB}-image-tag-${IMAGE_TAG}" + MESSAGE="update ${HUB} image tag to ${IMAGE_TAG}" + git commit -m "${MESSAGE}" + git push origin ${BRANCH} + # + # now create a PR! + # + cat << EOF > ${HOME}/pr-body.txt + ${MESSAGE} + + ${CHANGED_FILES} + EOF + BODY=$(cat ${HOME}/pr-body.txt) + gh pr new -t "${MESSAGE}" -b "${BODY}" -H${BRANCH} -Bstaging - name: Print out a message if no PR is created if: ${{ ! env.IMAGE_TAG }}