|
| 1 | +name: Automatic Updates |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: 0 0 * * * |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +defaults: |
| 9 | + run: |
| 10 | + shell: 'bash -Eeuo pipefail -x {0}' |
| 11 | + |
| 12 | +jobs: |
| 13 | + update: |
| 14 | + runs-on: ubuntu-20.04 |
| 15 | + steps: |
| 16 | + - |
| 17 | + uses: actions/checkout@v2 |
| 18 | + with: |
| 19 | + token: ${{ secrets.REPO_GHA_PAT }} |
| 20 | + fetch-depth: 0 |
| 21 | + - |
| 22 | + name: Get latest PgBouncer |
| 23 | + run: | |
| 24 | + echo PGBOUNCER_VERSION=$(curl -s https://api.github.com/repos/pgbouncer/pgbouncer/releases/latest | jq -r '.assets[].name' | grep -oP "pgbouncer-\K([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)(?=\.tar\.gz)") >> $GITHUB_ENV |
| 25 | + - |
| 26 | + name: Get latest Debian base image |
| 27 | + run: | |
| 28 | + echo DEBIAN_VERSION=$(curl -SsL "https://registry.hub.docker.com/v2/repositories/library/debian/tags/?name=buster-20&ordering=last_updated&" | jq -r ".results[].name | match(\"buster.*-slim\") | .string" | head -n1) >> $GITHUB_ENV |
| 29 | + - |
| 30 | + name: Update Dockerfile |
| 31 | + run: | |
| 32 | + INITIAL_RELEASE_VERSION=$(jq -r '.IMAGE_RELEASE_VERSION' .versions.json) |
| 33 | + sed \ |
| 34 | + -e 's/%%PGBOUNCER_VERSION%%/${{ env.PGBOUNCER_VERSION }}/' \ |
| 35 | + -e 's/%%DEBIAN_VERSION%%/${{ env.DEBIAN_VERSION }}/' \ |
| 36 | + -e "s/%%IMAGE_RELEASE_VERSION%%/${INITIAL_RELEASE_VERSION}/" \ |
| 37 | + Dockerfile.template > Dockerfile |
| 38 | + - |
| 39 | + name: Set up Docker Buildx |
| 40 | + id: buildx |
| 41 | + uses: docker/setup-buildx-action@v1 |
| 42 | + - |
| 43 | + name: Build and export to Docker |
| 44 | + |
| 45 | + with: |
| 46 | + context: . |
| 47 | + load: true |
| 48 | + push: false |
| 49 | + tags: newimage |
| 50 | + - |
| 51 | + name: Dockle scan |
| 52 | + |
| 53 | + with: |
| 54 | + image: newimage |
| 55 | + exit-code: '1' |
| 56 | + failure-threshold: WARN |
| 57 | + env: |
| 58 | + DOCKLE_IGNORES: DKL-DI-0006 |
| 59 | + - |
| 60 | + name: Extract package list from container |
| 61 | + run: | |
| 62 | + docker run -t --entrypoint bash newimage -c 'apt list --installed | sort' > packages.txt |
| 63 | + - |
| 64 | + # We verify if there has been any change in the image. It could be: |
| 65 | + # * a pgbouncer update |
| 66 | + # * a new Debian base image |
| 67 | + # * any change in the installed packages |
| 68 | + # * any change in the git repository except the pipeline |
| 69 | + name: Check if the image has been updated since the latest tag |
| 70 | + run: | |
| 71 | + echo UPDATED=false >> $GITHUB_ENV |
| 72 | + if git describe --tags; then |
| 73 | + current_tag=$(git describe --tags --abbrev=0) |
| 74 | + if [[ -n $(git diff --name-status ${current_tag} -- . ':(exclude)README.md' ':(exclude).github' ':(exclude).gitignore') ]]; then |
| 75 | + echo UPDATED=true >> $GITHUB_ENV |
| 76 | + fi |
| 77 | + fi |
| 78 | + - |
| 79 | + name: Define tag |
| 80 | + if: ${{ github.ref == 'refs/heads/main' && env.UPDATED == 'true' }} |
| 81 | + run: | |
| 82 | + release_number=1 |
| 83 | + if git describe --tags; then |
| 84 | + current_tag=$(git describe --tags --abbrev=0) |
| 85 | + current_pgbouncer_version=$(echo $current_tag | cut -d'-' -f 1) |
| 86 | + current_pgbouncer_version=${current_pgbouncer_version##v} |
| 87 | + current_release=$(echo $current_tag | cut -d'-' -f 2) |
| 88 | + if [ $current_pgbouncer_version = ${{ env.PGBOUNCER_VERSION }} ]; then |
| 89 | + release_number=$((current_release+1)) |
| 90 | + fi |
| 91 | + fi |
| 92 | + echo IMAGE_RELEASE_VERSION=${release_number} >> $GITHUB_ENV |
| 93 | + echo TAG=${{ env.PGBOUNCER_VERSION }}-${release_number} >> $GITHUB_ENV |
| 94 | + - |
| 95 | + # In case we are releasing, we need to re-generate the Dockerfile from |
| 96 | + # the template again since now we also know the proper release version. |
| 97 | + name: Update Dockerfile and the JSON version file |
| 98 | + if: ${{ github.ref == 'refs/heads/main' && env.UPDATED == 'true' }} |
| 99 | + run: | |
| 100 | + sed \ |
| 101 | + -e 's/%%PGBOUNCER_VERSION%%/${{ env.PGBOUNCER_VERSION }}/' \ |
| 102 | + -e 's/%%DEBIAN_VERSION%%/${{ env.DEBIAN_VERSION }}/' \ |
| 103 | + -e 's/%%IMAGE_RELEASE_VERSION%%/${{ env.IMAGE_RELEASE_VERSION }}/' \ |
| 104 | + Dockerfile.template > Dockerfile |
| 105 | + jq -S '.PGBOUNCER_VERSION = "${{ env.PGBOUNCER_VERSION }}" | .IMAGE_RELEASE_VERSION = "${{ env.IMAGE_RELEASE_VERSION }}" | .DEBIAN_VERSION = "${{ env.DEBIAN_VERSION }}"' < .versions.json >> .versions.json.new |
| 106 | + mv .versions.json.new .versions.json |
| 107 | + - |
| 108 | + name: Temporarily disable "include administrators" branch protection |
| 109 | + if: ${{ always() && github.ref == 'refs/heads/main' && env.UPDATED == 'true' }} |
| 110 | + id: disable_include_admins |
| 111 | + uses: benjefferies/[email protected] |
| 112 | + with: |
| 113 | + access_token: ${{ secrets.REPO_GHA_PAT }} |
| 114 | + branch: main |
| 115 | + enforce_admins: false |
| 116 | + - |
| 117 | + name: Commit changes |
| 118 | + if: ${{ github.ref == 'refs/heads/main' && env.UPDATED == 'true' }} |
| 119 | + uses: EndBug/add-and-commit@v7 |
| 120 | + id: commit |
| 121 | + with: |
| 122 | + author_name: EnterpriseDB Automated Updates |
| 123 | + |
| 124 | + message: 'Automatic update' |
| 125 | + tag: v${{ env.TAG }} |
| 126 | + - |
| 127 | + name: Make sure a tag is created in case of update |
| 128 | + if: ${{ github.ref == 'refs/heads/main' && env.UPDATED == 'true' }} |
| 129 | + uses: mathieudutour/[email protected] |
| 130 | + with: |
| 131 | + github_token: ${{ secrets.REPO_GHA_PAT }} |
| 132 | + custom_tag: ${{ env.TAG }} |
| 133 | + tag_prefix: 'v' |
| 134 | + - |
| 135 | + name: Enable "include administrators" branch protection |
| 136 | + uses: benjefferies/[email protected] |
| 137 | + if: ${{ always() && github.ref == 'refs/heads/main' && env.UPDATED == 'true' }} |
| 138 | + with: |
| 139 | + access_token: ${{ secrets.REPO_GHA_PAT }} |
| 140 | + branch: main |
| 141 | + enforce_admins: ${{ steps.disable_include_admins.outputs.initial_status }} |
0 commit comments