From d57f4b0f665430a30c8fb6c734b2b039b841c904 Mon Sep 17 00:00:00 2001 From: kentsanggds Date: Tue, 21 Nov 2023 14:33:39 +0000 Subject: [PATCH] Add create-pr yaml and sh --- .github/workflows/create-pr.yaml | 27 +++++++++++++++++++ docker/create-pr.sh | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 .github/workflows/create-pr.yaml create mode 100644 docker/create-pr.sh diff --git a/.github/workflows/create-pr.yaml b/.github/workflows/create-pr.yaml new file mode 100644 index 00000000..ed4856aa --- /dev/null +++ b/.github/workflows/create-pr.yaml @@ -0,0 +1,27 @@ +name: Create charts PR + +on: + workflow_dispatch: + workflow_run: + workflows: [ "Build and push images" ] + types: + - completed + branches: + - main + +jobs: + create_pr: + name: Create charts PR + runs-on: ubuntu-latest + steps: + - name: Checkout datagovuk_publish repository + uses: actions/checkout@v4 + with: + repository: alphagov/datagovuk_publish + path: ckanext + - run: bash ./docker/create-pr.sh + env: + GH_TOKEN: ${{ secrets.PR_GITHUB_TOKEN }} + GH_REF: ${{ github.ref_name }} + IS_TAG: "false" + ENVS: "integration" \ No newline at end of file diff --git a/docker/create-pr.sh b/docker/create-pr.sh new file mode 100644 index 00000000..a71d7710 --- /dev/null +++ b/docker/create-pr.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +set -eux + +if [[ ${IS_TAG:-} = "true" ]]; then + export IMAGE_TAG="${GH_REF}" + export SOURCE_BRANCH="main" +else + export IMAGE_TAG=$(gh api repos/alphagov/datagovuk_publish/branches/${GH_REF} | jq .commit.sha -r) + export SOURCE_BRANCH=${GH_REF} +fi + +git config --global user.email "govuk-ci@users.noreply.github.com" +git config --global user.name "govuk-ci" + +git clone https://${GH_TOKEN}@github.com/alphagov/govuk-dgu-charts.git charts + +cd charts/charts/datagovuk/images + +for ENV in $(echo $ENVS | tr "," " "); do + ( + BRANCH="ci/${IMAGE_TAG}-${ENV}" + + if git show-ref --quiet refs/heads/${BRANCH}; then + echo "Branch ${BRANCH} already exists on govuk-dgu-charts" + else + git checkout -b ${BRANCH} + + cd "${ENV}" + for APP in ckan pycsw solr; do + yq -i '.tag = env(IMAGE_TAG)' "${APP}.yaml" + yq -i '.branch = env(SOURCE_BRANCH)' "${APP}.yaml" + git add "${APP}.yaml" + done + + if [[ $(git status | grep "nothing to commit") ]]; then + echo "Nothing to commit" + else + git commit -m "Update image tags for ${ENV} to ${IMAGE_TAG}" + git push --set-upstream origin "${BRANCH}" + gh pr create --title "Update image tags for ${ENV} (${IMAGE_TAG})" --base main --head "${BRANCH}" --fill + fi + fi + ) +done