Skip to content

Commit

Permalink
Add .deb packaging (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspermarstal authored Feb 16, 2024
1 parent 8e6bace commit d028b04
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 60 deletions.
68 changes: 61 additions & 7 deletions .github/docker/Dockerfile.debian
Original file line number Diff line number Diff line change
@@ -1,21 +1,75 @@
FROM debian:bullseye

ARG PG_MAJOR_VER
ARG PG_MAJOR_VERSION
ARG PLPRQL_VERSION

# Install PostgreSQL
RUN apt-get update
RUN apt-get install -y wget gnupg
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ bullseye-pgdg main" >> /etc/apt/sources.list.d/pgdg.list
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
RUN apt-get update
RUN apt-get install -y build-essential postgresql-server-dev-${PG_MAJOR_VER} postgresql-${PG_MAJOR_VER}
RUN apt-get install -y build-essential postgresql-server-dev-${PG_MAJOR_VERSION} postgresql-${PG_MAJOR_VERSION}

# Install utilities
RUN apt-get install -y jq git ruby

# Install fpm for the creation of the .deb file,
# and install toml so TOML files can be parsed later
RUN gem install --no-document fpm toml

# Install rust
RUN apt-get install -y curl pkg-config
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"

RUN cargo install --locked cargo-pgrx --version 0.11.2
RUN cargo pgrx init --pg${PG_MAJOR_VER} /usr/bin/pg_config
# Prepare source directory
COPY ./ /src
WORKDIR /src/plprql

# Install pgrx using the version in Cargo.toml
RUN PGRX_VERSION=$(cargo metadata --format-version 1 | jq -r '.packages[]|select(.name=="pgrx")|.version') && \
cargo install cargo-pgrx --force --version "${PGRX_VERSION}"
RUN cargo pgrx init --pg${PG_MAJOR_VERSION} $(which pg_config)

# Package the extension. This command will create all the files necessary to package up in a .deb file.
# We know the directory in which this command will create the files, and we save it in the RELEASE_DIR env variable.
RUN cargo pgrx package --features pg${PG_MAJOR_VERSION}
ENV RELEASE_DIR /src/target/release/plprql-pg${PG_MAJOR_VERSION}

# Create the directory for the outgoing .deb package
RUN mkdir /artifacts && chmod 777 /artifacts

# Create the --before-install script
RUN echo $'#!/bin/bash\n\
\n\
if ! id -u postgres 2>&1 > /dev/null; then\n\
echo "[!] User 'postgres' does not exist. Have the official Postgres packages been installed yet?"\n\
exit 1\n\
fi' >> /tmp/before-install.sh

# Package everything up based on whatever's in RELEASE_DIR, and send the resulting
# .deb file to the /artifact directory. We don't check if PostgreSQL is installed
# (e.g. via -d "postgresql-${PG_MAJOR_VERSION}") because it could have been compiled
# from source or installed in a myriad of other ways. We simply check for the postgres
# user and let the install fail and hard if the required directories do not exist.
RUN DEB_FILENAME="plprql-${PLPRQL_VERSION}-postgresql-${PG_MAJOR_VERSION}-debian-$(dpkg --print-architecture).deb" && \
cd ${RELEASE_DIR} && fpm \
-s dir \
-t deb \
-n plprql \
-m 'Kasper Marstal, kaspermarstal@gmail.com' \
--description 'PL/PRQL is a PostgreSQL extension that lets you write functions with PRQL.' \
-v ${PLPRQL_VERSION} \
--url 'https://github.com/kaspermarstal/plprql' \
--license 'Apache 2.0 License' \
--category 'Databases' \
--deb-no-default-config-files \
--before-install /tmp/before-install.sh \
-p /artifacts/$DEB_FILENAME \
--deb-user postgres \
--deb-group postgres \
-a native \
.

COPY ./ /plprql
WORKDIR /plprql
RUN cargo pgrx install --release -c "/usr/bin/pg_config"
RUN ls /artifacts
34 changes: 0 additions & 34 deletions .github/docker/run-docker.sh

This file was deleted.

14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ jobs:
fail-fast: false # We want all of them to run, even if one fails
matrix:
os: [ ubuntu-latest, macos-latest ]
pg: [ "pg12", "pg13", "pg14", "pg15", "pg16" ]
pg: [ "12", "13", "14", "15", "16" ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Install cargo-pgrx
run: |
PGRX_VERSION=$(cargo metadata --format-version 1 | jq -r '.packages[]|select(.name=="pgrx")|.version')
cargo install --locked --version=$PGRX_VERSION cargo-pgrx --debug --force
cargo pgrx init --${{ matrix.pg }} download
cargo pgrx init --pg${{ matrix.pg }} download
- name: Run unit tests
run: cd plprql && echo "\q" | cargo pgrx run ${{ matrix.pg }} && cargo test --no-default-features --features ${{ matrix.pg }}
run: cd plprql && echo "\q" | cargo pgrx run pg${{ matrix.pg }} && cargo test --no-default-features --features pg${{ matrix.pg }}
- name: Run integration tests
run: cd plprql && echo "\q" | cargo pgrx run ${{ matrix.pg }} && cd ../plprql-tests && cargo test --no-default-features --features ${{ matrix.pg }}
run: cd plprql && echo "\q" | cargo pgrx run pg${{ matrix.pg }} && cd ../plprql-tests && cargo test --no-default-features --features pg${{ matrix.pg }}
Install:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -69,15 +69,15 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
pg: [ "pg16" ]
pg: [ "16" ]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install cargo-pgrx
run: |
PGRX_VERSION=$(cargo metadata --format-version 1 | jq -r '.packages[]|select(.name=="pgrx")|.version')
cargo install --locked --version=$PGRX_VERSION cargo-pgrx --debug --force
cargo pgrx init --${{ matrix.pg }} download
cargo pgrx init --pg${{ matrix.pg }} download
- name: Run clippy
run: cargo clippy --all-targets --no-default-features --features ${{ matrix.pg }} -- -D warnings
run: cargo clippy --all-targets --no-default-features --features pg${{ matrix.pg }} -- -D warnings

77 changes: 65 additions & 12 deletions .github/workflows/package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,86 @@ on:
push:
branches:
- main
pull_request:
branches:
- main
# schedule:
# - cron: '0 7 * * MON-FRI'
workflow_dispatch:

env:
RUST_BACKTRACE: 1
CARGO_INCREMENTAL: "false"

jobs:
Deb:
if: false
Debian:
runs-on: ubuntu-latest
strategy:
fail-fast: false # We want all of them to run, even if one fails
matrix:
pg: [ "12", "13", "14", "15", "16" ]
os: [ "debian" ]
os: [ { distribution: "debian", release: "bullseye" }, { distribution: "debian", release: "bookworm" } ]
steps:
- uses: actions/checkout@v4
- name: Package PL/PRQL for PostgreSQL ${{ matrix.pg }} (${{ matrix.os }})
shell: bash
with:
fetch-depth: 0 # Ensure all history for tags and branches is fetched
- name: Extract PL/PRQL version
run: |
# Find the latest tag
LATEST_TAG=$(git describe --tags --abbrev=0)
# Get the short SHA of the current commit
COMMIT_SHORT_SHA=$(git rev-parse --short=8 HEAD)
# Determine if the latest push is exactly the latest tag
if [[ "refs/tags/$LATEST_TAG" == "$GITHUB_REF" ]]; then
# If the push is for the latest tag, use the tag as the version
PLPRQL_VERSION=${LATEST_TAG#v}
else
# If the push is not for a tag, append the current commit's short SHA to the latest tag
PLPRQL_VERSION="${LATEST_TAG#v}-$COMMIT_SHORT_SHA"
fi
echo "PLPRQL_VERSION=$PLPRQL_VERSION" >> $GITHUB_ENV
echo "Version is $PLPRQL_VERSION"
- name: Package PL/PRQL .deb for PostgreSQL ${{ matrix.pg }} on ${{ matrix.os.distribution }} ${{ matrix.os.release }}
run: |
docker build \
--build-arg PG_MAJOR_VERSION=${{ matrix.pg }} \
--build-arg PLPRQL_VERSION=$PLPRQL_VERSION \
-t "plprql-$PLPRQL_VERSION-pg-${{ matrix.pg }}-${{ matrix.os.distribution }}-${{ matrix.os.release }}" \
-f ".github/docker/Dockerfile.${{ matrix.os.distribution }}" \
.
- name: Extract .deb
run: |
CONTAINER_ID=$(docker create plprql-$PLPRQL_VERSION-pg-${{ matrix.pg }}-${{ matrix.os.distribution }}-${{ matrix.os.release }})
docker cp $CONTAINER_ID:/artifacts /home/runner
docker rm -v $CONTAINER_ID
- name: Verify Install
run: |
docker build --build-arg PG_MAJOR_VERSION="${{ matrix.pg }}" -t plprql -f ".github/docker/Dockerfile.${{ matrix.os }}" .
docker run plprql cargo test --no-default-features --features "pg${{ matrix.pg }}"
CONTAINER_ID=$(docker run -d -e POSTGRES_HOST_AUTH_METHOD=trust postgres:${{ matrix.pg }}-${{ matrix.os.release }})
docker cp /home/runner/artifacts $CONTAINER_ID:/tmp
ls -R /home/runner/artifacts
docker exec $CONTAINER_ID ls -R /tmp
docker exec $CONTAINER_ID bash -c 'dpkg -i /tmp/artifacts/plprql*.deb'
max_tries=30
count=0
until docker exec $CONTAINER_ID pg_isready -U postgres; do
count=$((count+1))
echo "Waiting for PostgreSQL to start (attempt $count of $max_tries)"
sleep 1
if [ $count -ge $max_tries ]; then
echo "PostgreSQL failed to start"
exit 1
fi
done
docker exec $CONTAINER_ID psql -U postgres -c "create extension plprql;"
docker exec $CONTAINER_ID psql -U postgres -c "select prql_to_sql('from table');"
docker stop $CONTAINER_ID
docker rm -v $CONTAINER_ID
- name: Upload .deb
if: startsWith(github.ref, 'refs/tags/v')
uses: softprops/action-gh-release@v1
with:
files: /home/runner/artifacts/plprql*.deb
Bash:
runs-on: ubuntu-latest
steps:
Expand Down

0 comments on commit d028b04

Please sign in to comment.