Skip to content

ci: Only deploy production if a release was created #91

ci: Only deploy production if a release was created

ci: Only deploy production if a release was created #91

Workflow file for this run

name: Main
on:
push:
branches:
- "*"
pull_request: {}
env:
IMAGE_NAME: samling
BUILD_CACHE_IMAGE_NAME: samling-cache
jobs:
clippy:
name: Check for common code mistakes
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- name: Run clippy
continue-on-error: true
run: cargo clippy -- -D warnings
fmt:
name: Check code format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- name: Run rustfmt
continue-on-error: true
run: cargo fmt --all -- --check
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- name: Run tests
run: cargo test --release
cargo-deny:
runs-on: ubuntu-latest
steps:
- uses: jacobsvante/cargo-deny-action@v1
build-docker-image:
name: Build Docker image
runs-on: ubuntu-latest
if: ${{ github.repository == 'hyperkliv/samling' }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub registry
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USER }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
push: true
tags: ${{ secrets.DOCKERHUB_USER }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
cache-from: |
type=registry,ref=${{ secrets.DOCKERHUB_USER }}/${{ env.BUILD_CACHE_IMAGE_NAME }}:${{ github.sha }}
type=registry,ref=${{ secrets.DOCKERHUB_USER }}/${{ env.BUILD_CACHE_IMAGE_NAME }}:main
cache-to: |
type=registry,ref=${{ secrets.DOCKERHUB_USER }}/${{ env.BUILD_CACHE_IMAGE_NAME }}:${{ github.sha }},compression=zstd,mode=max
test-docker-image:
name: Test Docker image
runs-on: ubuntu-latest
needs: [build-docker-image, cargo-deny, test]
if: ${{ github.repository == 'hyperkliv/samling' }}
steps:
- name: Log in to Docker Hub registry
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USER }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Create network
run: docker network create test
- name: Start postgres container
run: |
docker run \
--name postgres \
--network test \
--rm \
--detach \
--env POSTGRES_DB=samling \
--env POSTGRES_USER=samling \
--env POSTGRES_PASSWORD=samling \
--hostname postgres \
postgres:14.5-alpine3.16
until docker exec postgres pg_isready; do sleep 0.5; done
- name: Start samling container
run: |
docker run \
--publish 8080:8080 \
--network test \
--rm \
--detach \
--env LOG_LEVEL=info \
--env SECRET=abc123 \
--env DB_NAME=samling \
--env DB_HOST=postgres \
--env DB_USER=samling \
--env DB_PASSWORD=samling \
--env DB_AUTO_MIGRATE=true \
--env CLOUDFLARE_ACCOUNT_ID=abc \
--env CLOUDFLARE_TOKEN=123 \
${{ secrets.DOCKERHUB_USER }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
sleep 1
- name: Check /api/readyz response
run: |
curl http://127.0.0.1:8080/api/readyz > output.json
jq . output.json
[[ $(jq .ok output.json) == true ]]
- name: Notify hyperkliv/www-samling-io
uses: peter-evans/repository-dispatch@v3
with:
# https://docs.github.com/en/actions/using-workflows/triggering-a-workflow#triggering-a-workflow-from-a-workflow
# NOTE: The custom github token is important, as repository_dispatch won't work across repos otherwise
token: ${{ secrets.TRIGGERING_GITHUB_TOKEN }}
repository: hyperkliv/www-samling-io
event-type: new-staging-samling-docker-image
client-payload: '{"sha": "${{ github.sha }}"}'
# Release unpublished packages.
release-plz-release:
name: Release-plz release
if: ${{ github.repository == 'hyperkliv/samling' && github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Run release-plz
uses: release-plz/[email protected]
id: release-plz
with:
command: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
- name: Notify hyperkliv/www-samling-io
if: steps.release-plz.outputs.releases_created
uses: peter-evans/repository-dispatch@v3
with:
# https://docs.github.com/en/actions/using-workflows/triggering-a-workflow#triggering-a-workflow-from-a-workflow
# NOTE: The custom github token is important, as repository_dispatch won't work across repos otherwise
token: ${{ secrets.TRIGGERING_GITHUB_TOKEN }}
repository: hyperkliv/www-samling-io
event-type: new-production-samling-docker-image
client-payload: '{"sha": "${{ github.sha }}"}'
# Create a PR with the new versions and changelog, preparing the next release.
release-plz-pr:
name: Release-plz PR
if: ${{ github.repository == 'hyperkliv/samling' && github.ref == 'refs/heads/main' }}
needs: [cargo-deny, test, test-docker-image]
runs-on: ubuntu-latest
concurrency:
group: release-plz-${{ github.ref }}
cancel-in-progress: false
permissions:
pull-requests: write
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Run release-plz
uses: release-plz/[email protected]
with:
command: release-pr
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}