|
| 1 | +name: PR Automation |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: {} |
| 5 | + pull_request_target: |
| 6 | + types: |
| 7 | + - closed |
| 8 | + branches: |
| 9 | + - master |
| 10 | + |
| 11 | +env: |
| 12 | + APP_NAME: statefulset-resize-controller |
| 13 | + COMPONENT_REPO: vshn/component-appcat |
| 14 | + PUSH_UPBOUND: "False" |
| 15 | + PUSH_PACKAGE: "False" |
| 16 | + PUSH_IMAGE: "True" |
| 17 | + |
| 18 | +jobs: |
| 19 | + check-labels: |
| 20 | + # Act doesn't set a pull request number by default, so we skip if it's 0 |
| 21 | + if: github.event.pull_request.number != 0 |
| 22 | + name: Check labels |
| 23 | + runs-on: ubuntu-latest |
| 24 | + steps: |
| 25 | + - uses: docker://agilepathway/pull-request-label-checker:v1.6.51 |
| 26 | + with: |
| 27 | + one_of: major,minor,patch,documentation,dependency |
| 28 | + repo_token: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + publish-branch-images: |
| 30 | + if: github.event.action != 'closed' |
| 31 | + runs-on: ubuntu-latest |
| 32 | + steps: |
| 33 | + - uses: actions/checkout@v4 |
| 34 | + |
| 35 | + - name: Determine Go version from go.mod |
| 36 | + run: echo "GO_VERSION=$(grep "go 1." go.mod | cut -d " " -f 2)" >> $GITHUB_ENV |
| 37 | + |
| 38 | + - uses: actions/setup-go@v5 |
| 39 | + with: |
| 40 | + go-version: ${{ env.GO_VERSION }} |
| 41 | + |
| 42 | + - uses: actions/cache@v4 |
| 43 | + with: |
| 44 | + path: ~/go/pkg/mod |
| 45 | + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} |
| 46 | + restore-keys: | |
| 47 | + ${{ runner.os }}-go- |
| 48 | +
|
| 49 | + - name: Extract escaped branch name |
| 50 | + shell: bash |
| 51 | + run: echo "branch=$(echo ${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}} | sed 's/\//_/g' )" >> $GITHUB_OUTPUT |
| 52 | + id: extract_branch |
| 53 | + |
| 54 | + - name: Login to GHCR |
| 55 | + uses: docker/login-action@v3 |
| 56 | + with: |
| 57 | + registry: ghcr.io |
| 58 | + username: ${{ github.repository_owner }} |
| 59 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 60 | + |
| 61 | + - name: Build branch and push image |
| 62 | + if: env.PUSH_IMAGE == 'true' |
| 63 | + run: make docker-push-branchtag -e IMG_TAG="${{ steps.extract_branch.outputs.branch }}" |
| 64 | + |
| 65 | + - name: Build branch and push package |
| 66 | + if: env.PUSH_PACKAGE == 'True' |
| 67 | + run: make package-push-branchtag -e IMG_TAG="${{ steps.extract_branch.outputs.branch }}" |
| 68 | + |
| 69 | + - name: Login to Upbound |
| 70 | + if: env.PUSH_UPBOUND == 'true' |
| 71 | + uses: docker/login-action@v3 |
| 72 | + with: |
| 73 | + registry: xpkg.upbound.io |
| 74 | + username: ${{ secrets.UPBOUND_MARKETPLACE_PUSH_ROBOT_USR }} |
| 75 | + password: ${{ secrets.UPBOUND_MARKETPLACE_PUSH_ROBOT_PSW }} |
| 76 | + |
| 77 | + - name: Build branch and push package to upbound |
| 78 | + if: env.PUSH_UPBOUND == 'true' && env.PUSH_PACKAGE == 'true' |
| 79 | + run: make package-push-branchtag -e IMG_TAG="${{ steps.extract_branch.outputs.branch }}" -e IMG_REPO=xpkg.upbound.io |
| 80 | + |
| 81 | + open-pr-component: |
| 82 | + if: github.event.action == 'opened' |
| 83 | + runs-on: ubuntu-latest |
| 84 | + steps: |
| 85 | + - name: Checkout code |
| 86 | + uses: actions/checkout@v4 |
| 87 | + with: |
| 88 | + repository: ${{ env.COMPONENT_REPO }} |
| 89 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 90 | + |
| 91 | + - name: Extract branch name |
| 92 | + shell: bash |
| 93 | + run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT |
| 94 | + id: extract_branch |
| 95 | + |
| 96 | + - name: Update defaults.yml and create branch |
| 97 | + run: | |
| 98 | + yq e '.parameters.appcat.images.${{ env.APP_NAME }}.tag="${{ steps.extract_branch.outputs.branch }}"' class/defaults.yml | diff -B class/defaults.yml - | patch class/defaults.yml - || true |
| 99 | +
|
| 100 | + git --no-pager diff |
| 101 | +
|
| 102 | + - name: Generate new golden |
| 103 | + # Act uses the host's docker to run containers, but then |
| 104 | + # they can't access the files that were previously cloned. |
| 105 | + if: github.event.pull_request.number != 0 |
| 106 | + run: | |
| 107 | + make gen-golden-all |
| 108 | +
|
| 109 | + - name: Create Pull Request |
| 110 | + uses: peter-evans/create-pull-request@v6 |
| 111 | + with: |
| 112 | + token: ${{ secrets.COMPONENT_ACCESS_TOKEN }} |
| 113 | + title: 'PR for ${{ env.APP_NAME }} on ${{ steps.extract_branch.outputs.branch }}' |
| 114 | + body: "${{ github.event.pull_request.body}}\nLink: ${{ github.event.pull_request.url }}" |
| 115 | + branch: "${{ env.APP_NAME }}/${{ github.event.pull_request.number }}/${{ steps.extract_branch.outputs.branch }}" |
| 116 | + base: master |
| 117 | + draft: false |
| 118 | + create-release: |
| 119 | + if: github.event.pull_request.merged |
| 120 | + runs-on: ubuntu-latest |
| 121 | + steps: |
| 122 | + - name: Check for patch label |
| 123 | + if: contains(github.event.pull_request.labels.*.name, 'patch') || contains(github.event.pull_request.labels.*.name, 'dependency') || contains(github.event.pull_request.labels.*.name, 'documentation') |
| 124 | + id: patch |
| 125 | + run: | |
| 126 | + echo "set=true" >> $GITHUB_OUTPUT |
| 127 | + - name: Check for minor label |
| 128 | + if: contains(github.event.pull_request.labels.*.name, 'minor') |
| 129 | + id: minor |
| 130 | + run: | |
| 131 | + echo "set=true" >> $GITHUB_OUTPUT |
| 132 | + - name: Check for major label |
| 133 | + if: contains(github.event.pull_request.labels.*.name, 'major') |
| 134 | + id: major |
| 135 | + run: | |
| 136 | + echo "set=true" >> $GITHUB_OUTPUT |
| 137 | +
|
| 138 | + - uses: actions/checkout@v4 |
| 139 | + with: |
| 140 | + # Make sure we use the right commit to tag |
| 141 | + ref: ${{ github.event.pull_request.merge_commit_sha }} |
| 142 | + # We also need to use the personal access token here. As subsequent |
| 143 | + # actions will not trigger by tags/pushes that use `GITHUB_TOKEN` |
| 144 | + # https://github.com/orgs/community/discussions/25702#discussioncomment-3248819 |
| 145 | + token: ${{ secrets.COMPONENT_ACCESS_TOKEN }} |
| 146 | + # This is broken in checkout@v4... |
| 147 | + # https://github.com/actions/checkout/issues/1781 |
| 148 | + fetch-tags: true |
| 149 | + |
| 150 | + - name: fetch tags |
| 151 | + run: | |
| 152 | + git fetch --tags |
| 153 | + echo "latest tag: $(git describe --tags "$(git rev-list --tags --max-count=1)")" |
| 154 | + echo "TAG_VERSION=$(git describe --tags "$(git rev-list --tags --max-count=1)")" >> $GITHUB_ENV |
| 155 | +
|
| 156 | + - name: Extract branch name |
| 157 | + shell: bash |
| 158 | + run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT |
| 159 | + id: extract_branch |
| 160 | + |
| 161 | + # We only run this if any of the release tags is set. |
| 162 | + # For docs and deps we don't do automagic releases |
| 163 | + - name: Increase Tag |
| 164 | + id: tag |
| 165 | + run: | |
| 166 | + patch=${{ steps.patch.outputs.set }} |
| 167 | + minor=${{ steps.minor.outputs.set }} |
| 168 | + major=${{ steps.major.outputs.set }} |
| 169 | +
|
| 170 | + major_ver=$(echo '${{ env.TAG_VERSION }}' | cut -d "." -f1) |
| 171 | + minor_ver=$(echo '${{ env.TAG_VERSION }}' | cut -d "." -f2) |
| 172 | + patch_ver=$(echo '${{ env.TAG_VERSION }}' | cut -d "." -f3) |
| 173 | +
|
| 174 | + major_ver="${major_ver:1}" |
| 175 | +
|
| 176 | + # Check for patch label |
| 177 | + [ ! -z "$patch" ] && [ -z "$minor" ] && [ -z "$major" ] && ((patch_ver++)) || true |
| 178 | +
|
| 179 | + # check for minor label |
| 180 | + if [ ! -z "$minor" ] && [ -z "$major" ]; then |
| 181 | + ((minor_ver++)) |
| 182 | + patch_ver=0 |
| 183 | + fi |
| 184 | +
|
| 185 | + # Check for major label |
| 186 | + if [ ! -z "$major" ]; then |
| 187 | + ((major_ver++)) |
| 188 | + minor_ver=0 |
| 189 | + patch_ver=0 |
| 190 | + fi |
| 191 | +
|
| 192 | + tag="v$major_ver.$minor_ver.$patch_ver" |
| 193 | + echo "new tag $tag" |
| 194 | + git tag $tag |
| 195 | + git push --tags |
| 196 | + echo tag=$tag >> $GITHUB_OUTPUT |
| 197 | +
|
| 198 | + - name: Checkout component |
| 199 | + uses: actions/checkout@v4 |
| 200 | + with: |
| 201 | + repository: ${{ env.COMPONENT_REPO }} |
| 202 | + token: ${{ secrets.COMPONENT_ACCESS_TOKEN }} |
| 203 | + ref: "${{ env.APP_NAME }}/${{ github.event.pull_request.number }}/${{ steps.extract_branch.outputs.branch }}" |
| 204 | + |
| 205 | + - name: Update tag and run golden |
| 206 | + run: | |
| 207 | + yq e '.parameters.appcat.images.${{ env.APP_NAME }}.tag="${{ steps.tag.outputs.tag }}"' class/defaults.yml | diff -B class/defaults.yml - | patch class/defaults.yml - || true |
| 208 | + make gen-golden-all |
| 209 | +
|
| 210 | + - name: Commit & Push changes |
| 211 | + uses: actions-js/push@master |
| 212 | + with: |
| 213 | + github_token: ${{ secrets.COMPONENT_ACCESS_TOKEN }} |
| 214 | + branch: "${{ env.APP_NAME }}/${{ github.event.pull_request.number }}/${{ steps.extract_branch.outputs.branch }}" |
| 215 | + message: "Update tag" |
| 216 | + repository: ${{ env.COMPONENT_REPO }} |
| 217 | + |
0 commit comments