Skip to content

Commit

Permalink
Merge pull request #11 from tembo-io/TEM-1423
Browse files Browse the repository at this point in the history
trunk publish
  • Loading branch information
ChuckHend authored Jul 31, 2023
2 parents 978a5c4 + a2883c7 commit ee37ab1
Show file tree
Hide file tree
Showing 8 changed files with 495 additions and 5 deletions.
152 changes: 152 additions & 0 deletions .github/actions/build-and-push-to-quay/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
name: 'Build and push to Quay'
description: 'Builds a container image and pushes it to our Quay organization'
inputs:
image_name:
description: 'The name of the image, not including the registry or the tag, for example "postgres"'
required: true
registry:
description: 'The name of the image, not including the registry or the tag, for example "postgres"'
required: false
default: "quay.io/coredb"
registry_tembo:
description: 'The name of the image, not including the registry or the tag, for example "postgres"'
required: false
default: "quay.io/tembo"
docker_directory:
description: 'The relative path to a directory in which there is a Dockerfile'
required: false
default: '.'
quay_user:
required: true
description: "Quay 'robot user' user name"
quay_password:
required: true
description: "Quay 'robot user' access token"
quay_user_tembo:
required: true
description: "Quay 'robot user' user name for Tembo org"
quay_password_tembo:
required: true
description: "Quay 'robot user' access token for Tembo org"
publish_calver:
description: 'Should we tag with calendar versioning?'
required: false
default: false
calver_suffix:
description: 'Optional suffix to the calendar version'
required: false
default: ""
publish_latest:
description: "Should we tag with 'latest'?"
required: false
default: false
tag_cargo_version_if_present:
description: "Should we tag with the version found in Cargo.toml, if found?"
required: false
default: false
tags:
description: "Whitespace-separated tags, not including the registry, for example 'v1' or 'v1 release-1.0'. There are also some default tags provided, please see the other options of this action."
required: false
default: ""
outputs: {}
runs:
using: "composite"
steps:
- name: Install TOML parser
shell: bash
run: |
set -xe
wget https://github.com/freshautomations/stoml/releases/download/v0.7.1/stoml_linux_amd64
mv stoml_linux_amd64 stoml
chmod +x stoml
sudo mv stoml /usr/local/bin/
- name: Create whitespace-separated tags list
shell: bash
id: tags
run: |
set -e
# input tags
TAGS='${{ inputs.tags }}'
SHORT_SHA=$(git rev-parse --short HEAD)
cd ${{ inputs.docker_directory }}
if [ "${{ inputs.tag_cargo_version_if_present }}" == "true" ] && test -f "Cargo.toml"; then
echo "Cargo file detected, adding to tags"
VERSION=$(stoml Cargo.toml package.version)-${SHORT_SHA}
TAGS="$TAGS $VERSION"
fi
# Calendar version
if [ "${{ inputs.publish_calver }}" == "true" ]; then
# A date without leading zeros, for example:
# 2023.1.26
CAL_VER=$(date '+%Y.%-m.%-d')
TAGS="$TAGS ${CAL_VER}${{ inputs.calver_suffix }}"
fi
# latest
if [ "${{ inputs.publish_latest }}" == "true" ]; then
TAGS="$TAGS latest"
fi
# Short Git hash
TAGS="$TAGS ${SHORT_SHA}"
echo "TAGS=$TAGS" >> $GITHUB_OUTPUT
- name: Run pre-build hooks
shell: bash
run: |
cd ${{ inputs.docker_directory }}
if [[ -f pre-build-hook.sh ]]; then
echo "detected pre-build hook, running"
/bin/bash pre-build-hook.sh
else
echo "no pre build hook detected"
fi
- name: Build image and tag
shell: bash
run: |
set -xe
# Build the image
docker build -t ${{ inputs.image_name }} ${{ inputs.docker_directory }}
# Tag with each tag in the comma-separate list
IFS=' ' read -ra TAG_ARRAY <<< "${{ steps.tags.outputs.TAGS }}"
for tag in "${TAG_ARRAY[@]}"; do
docker tag ${{ inputs.image_name }} ${{ inputs.image_name }}:$tag
done
- name: Login to CoreDB Quay
if: inputs.image_name != 'tembo-pg-cnpg'
uses: docker/login-action@v2
with:
registry: ${{ inputs.registry }}
username: ${{ inputs.quay_user }}
password: ${{ inputs.quay_password }}
- name: Push to Quay
if: inputs.image_name != 'tembo-pg-cnpg'
shell: bash
run: |
set -xe
IFS=' ' read -ra TAG_ARRAY <<< "${{ steps.tags.outputs.TAGS }}"
for tag in "${TAG_ARRAY[@]}"; do
docker tag ${{ inputs.image_name }}:$tag ${{ inputs.registry}}/${{ inputs.image_name }}:$tag
docker push ${{ inputs.registry}}/${{ inputs.image_name }}:$tag
done
- name: Login to Tembo Quay
if: inputs.image_name == 'tembo-pg-cnpg'
uses: docker/login-action@v2
with:
registry: ${{ inputs.registry_tembo }}
username: ${{ inputs.quay_user_tembo}}
password: ${{ inputs.quay_password_tembo }}
- name: Push to Quay
if: inputs.image_name == 'tembo-pg-cnpg'
shell: bash
run: |
set -xe
IFS=' ' read -ra TAG_ARRAY <<< "${{ steps.tags.outputs.TAGS }}"
for tag in "${TAG_ARRAY[@]}"; do
docker tag ${{ inputs.image_name }}:$tag ${{ inputs.registry_tembo}}/${{ inputs.image_name }}:$tag
docker push ${{ inputs.registry_tembo}}/${{ inputs.image_name }}:$tag
done
53 changes: 53 additions & 0 deletions .github/actions/find-changed-directories/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: 'Find changed directories'
description: 'Finds directories containing a specific filename in the root of that directory, filtering out directories that are unchanged relative to a given branch name'
inputs:
contains_the_file:
description: 'Look for directories with this file in the root of that directory. For example, Dockerfile or Cargo.toml'
required: true
fetch_branch_to_compare:
description: 'The branch to fetch when looking to compare a ref, typically main'
default: "main"
required: true
changed_relative_to_ref:
description: 'The ref on the fetched branch to compare with to determine if this directory has changed. For example "origin/main" or a git commit hash.'
required: true
ignore_dirs:
description: A list of directories to ignore.
required: false
default: ''
outputs:
build_matrix:
description: "Input this output to your matrix build in a following job, like this 'fromJson(needs.find_directories.outputs.build_matrix)'"
value: ${{ steps.find_directories.outputs.build_matrix }}
runs:
using: "composite"
steps:
- name: Find directories with a given file name
shell: bash
id: find_directories
run: |
set -xe
git fetch origin ${{ inputs.fetch_branch_to_compare }} || true
# Get directories with a Dockerfile that have not changed
# relative to the branch we are pulling into
echo "${{inputs.ignore_dirs}}"
IFS=', ' read -r -a array <<< "${{inputs.ignore_dirs}}"
EXCLUDE_OPTS=()
for exclude_dir in "${array[@]}"; do
EXCLUDE_OPTS+=("-not" "-path" "*/$exclude_dir/*")
done
directories=$(
find . -name ${{ inputs.contains_the_file }} -not -path "*/target/*" -not -path "*/.github/*" "${EXCLUDE_OPTS[@]}" -exec dirname {} \; | while read dir; do
# This will check if the directory has changed relative to the branch we are PRing
# into, and if it's not a PR, in the case of main or release/**, then it will
# build all docker directories
if git diff --quiet HEAD ${{ inputs.changed_relative_to_ref }} -- "$dir"; then
echo ""
else
echo "$dir"
fi
done)
# Format directories into a build matrix
matrix_include=$(echo "${directories}" | awk 'NF{print $NF};' | while read dir; do dir_without_dot=$(basename ${dir}); echo "{\"path\": \"$dir\", \"name\": \"$dir_without_dot\"}"; done | jq -scM '{"include": .}')
echo "${matrix_include}"
echo "build_matrix=${matrix_include}" >> $GITHUB_OUTPUT
46 changes: 46 additions & 0 deletions .github/actions/pgx-init/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: 'pgrx initialization'
description: 'Initialize PGRX if it is a dependency, otherwise do nothing.'
inputs:
working-directory:
description: 'The directory in which there is a pgrx extension project'
required: true
outputs: {}
runs:
using: "composite"
steps:
- name: Install TOML parser
shell: bash
run: |
set -xe
wget https://github.com/freshautomations/stoml/releases/download/v0.7.1/stoml_linux_amd64 &> /dev/null
mv stoml_linux_amd64 stoml
chmod +x stoml
sudo mv stoml /usr/local/bin/
- name: setup pgrx
shell: bash
id: pgrx_install
working-directory: ${{ inputs.working-directory }}
run: |
pgrx_version=$(stoml Cargo.toml dependencies.pgrx)
if [ -z "${pgrx_version}" ]; then
echo "pgrx is not a dependency: skipping"
echo "skip=true" >> $GITHUB_OUTPUT
else
cargo install --version ${pgrx_version} cargo-pgrx
echo "skip=false" >> $GITHUB_OUTPUT
fi
- name: pgrx init
shell: bash
if: steps.pgrx_install.outputs.skip == 'false'
working-directory: ${{ inputs.working-directory }}
run: |
set -x
pg_version=$(stoml Cargo.toml features.default)
# pgrx init can take a long time, and it re-compiles postgres even when there
# is a cached version. So, we can just check for the directory and
cat /home/runner/.pgrx/config.toml || true
if find /home/runner/.pgrx | grep $(awk -F "=" '/${pg_version}/ {print $2}' /home/runner/.pgrx/config.toml | tr -d '"'); then
echo "Already found pgrx is initialized. Skipping 'cargo pgrx init' command."
else
cargo pgrx init --${pg_version} download || true
fi
79 changes: 79 additions & 0 deletions .github/actions/publish-crate/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: 'Publish to crates.io'
description: 'Publish cratest to crates.io and some other crates.io-related actions, like checking if a version is already published.'
inputs:
working-directory:
required: false
description: "In which directory should we run 'cargo publish'?"
default: "."
cargo-registry-token:
required: true
description: "The CARGO_REGISTRY_TOKEN to use"
fail-if-version-published:
description: "If the version is already published, should we fail, or ignore? By default, ignore."
required: false
default: false
dry-run:
description: "Use --dry-run flag on cargo publish?"
required: false
default: false
toolchain:
description: "Which Rust toolchain to use?"
default: "stable"
required: false
outputs: {}
runs:
using: "composite"
steps:
- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ inputs.toolchain }}
- name: Install TOML parser
shell: bash
run: |
set -xe
wget https://github.com/freshautomations/stoml/releases/download/v0.7.1/stoml_linux_amd64 &> /dev/null
mv stoml_linux_amd64 stoml
chmod +x stoml
sudo mv stoml /usr/local/bin/
- name: Publish
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
set -xe
# If package.publish is false, skip the rest
SHOULD_PUBLISH=$(stoml Cargo.toml package.publish)
if [[ "${SHOULD_PUBLISH}" == "false" ]]; then
echo "Found package.publish is false. Skipping."
exit 0
fi
# Get crate information
NAME=$(stoml Cargo.toml package.name)
VERSION=$(stoml Cargo.toml package.version)
VERSIONS_INFO=$(curl https://crates.io/api/v1/crates/${NAME} 2> /dev/null)
# "|| true" handles the case where this crate hasn't yet been published
PUBLISHED_VERSIONS=$(echo ${VERSIONS_INFO} | jq -r '.versions[] | .num' || true)
echo ${VERSIONS_INFO}
# If this version is already published...
if echo ${VERSIONS_INFO} | jq -r ".versions[] | .num | . == \"${VERSION}\"" | grep true; then
echo "The version '${VERSION}' of '${NAME}' is already published."
if [ "${{ inputs.fail-if-version-published }}" == "true" ]; then
exit 1
else
echo "Skipping the rest of the action because inputs.fail-if-version-published is false."
exit 0
fi
fi
echo "Did not detect the version ${VERSION} to be already published."
echo "The list of known versions:"
echo $PUBLISHED_VERSIONS
# Set --dry-run flag, if configured
DRY_RUN=""
if [ "${{ inputs.dry-run }}" == "true" ]; then
DRY_RUN="--dry-run"
fi
cargo publish ${DRY_RUN} --token ${{ inputs.cargo-registry-token }}
Loading

0 comments on commit ee37ab1

Please sign in to comment.