This commit is contained in:
2024-05-03 12:25:20 +03:00
commit 8236ae0642
19 changed files with 452 additions and 0 deletions

View File

@ -0,0 +1,95 @@
name: "Build {{project-name}}"
# You must add gitea secrets and enable actions for repository:
# CRATES_TOKEN
# DOCKER_USER
# DOCKER_PASSWORD
{%- if crate_type == "bin" %}
env:
DOCKER_REGISTRY: git.smetan.ru
IMAGE_NAME: "{{git-owner}}/{{project-name}}"
IMAGE_RELEASE_TAG: latest
IMAGE_PRE_RELEASE_TAG: pre-release
{%- endif %}
on:
{%- if crate_type == "bin" %}
push:
branches:
- master
- main
{%- endif %}
release:
types:
- published
jobs:
{%- if crate_type == "bin" -%}
{% raw %}
build_docker_app:
name: Build Docker Image
runs-on: docker-ubuntu-latest
steps:
- name: Checkout sources
uses: https://github.com/actions/checkout@v4
- name: Set Image Name
run: |
echo "Event type: $GITHUB_EVENT_NAME"
if [[ $GITHUB_EVENT_NAME == 'release' ]]; then
echo "IMAGE_TAG=$DOCKER_REGISTRY/$IMAGE_NAME:$IMAGE_RELEASE_TAG" >> "$GITHUB_ENV"
else
echo "IMAGE_TAG=$DOCKER_REGISTRY/$IMAGE_NAME:$IMAGE_PRE_RELEASE_TAG" >> "$GITHUB_ENV"
fi
cat $GITHUB_ENV
- name: Build Image
run: docker build --build-arg CRATES_TOKEN=${{ secrets.CRATES_TOKEN }} --file Dockerfile --tag ${{ env.IMAGE_TAG }} .
- name: Docker Login
run: docker login --username ${{ secrets.DOCKER_USER }} --password ${{ secrets.DOCKER_PASSWORD }} ${{ env.DOCKER_REGISTRY }}
- name: Push Image
run: docker push ${{ env.IMAGE_TAG }}
- name: Docker Logout
run: docker logout ${{ env.DOCKER_REGISTRY }}
{% endraw %}
{%- endif -%}
{%- if crate_type == "lib" -%}
{% raw %}
publish_crate_version:
name: Publish new crate version
runs-on: ubuntu-latest
container:
image: git.smetan.ru/infra/ci-cargo:latest
credentials:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_PASSWORD }}
steps:
- name: Checkout sources
uses: https://github.com/actions/checkout@v4
- name: Set cargo registry
run: |
cat <<"EOF" | tee ~/.cargo/config.toml
[registry]
global-credential-providers = ["cargo:token"]
EOF
- name: Set Cargo version
run: |
export VERSION=`sed 's/v//'<<<"${{ github.ref_name }}"`
echo "Set Cargo version: $VERSION"
sed -i "s/0.0.0-git/$VERSION/g" Cargo.toml
sed -i "s/0.0.0-git/$VERSION/g" Cargo.lock
- name: Publish package version
env:
{%- endraw %}
CARGO_REGISTRY: git_{{git-owner}}
{%- raw %}
run: cargo publish --registry $CARGO_REGISTRY --token "Bearer ${{ secrets.CRATES_TOKEN }}" --allow-dirty
{% endraw %}
{%- endif -%}

View File

@ -0,0 +1,122 @@
name: Validate Pull Request
# You must add gitea secrets and enable actions for repository:
# CRATES_TOKEN
# DOCKER_USER
# DOCKER_PASSWORD
# ISSUE_API_RW_TOKEN
on:
pull_request:
types:
- opened
- edited
- reopened
- synchronize
branches:
- master
- main
jobs:
{%- raw %}
validate:
name: Validate
runs-on: ubuntu-latest
container:
image: git.smetan.ru/infra/ci-cargo:latest
credentials:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_PASSWORD }}
steps:
- name: Checkout sources
uses: https://github.com/actions/checkout@v4
- name: Set cargo registry
run: |
cat <<"EOF" | tee ~/.cargo/config.toml
[registry]
global-credential-providers = ["cargo:token"]
EOF
- name: Login to cargo registry
env:
{%- endraw %}
CARGO_REGISTRY: git_{{git-owner}}
{%- raw %}
run: cargo login --registry $CARGO_REGISTRY "Bearer ${{ secrets.CRATES_TOKEN }}"
- name: Cargo fmt
run: cargo fmt --all --check
- name: Cargo clippy
run: cargo clippy
{%- endraw %}
test:
{%- raw %}
name: Tests
runs-on: ubuntu-latest
container:
image: git.smetan.ru/infra/ci-cargo:latest
credentials:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_PASSWORD }}
{%- endraw %}
{%- if use_postgres == true %}
services:
db:
# Docker Hub image
image: postgres:15
# Provide the password for postgres
env:
POSTGRES_DB: test_db
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
{%- endif %}
steps:
{%- raw %}
- name: Checkout sources
uses: https://github.com/actions/checkout@v4
{% endraw -%}
{%- if use_postgres == true -%}
- name: Setup .env
run: |
echo "DATABASE_URL=postgres://user:pass@db:5432/test_db" >> .env
{%- endif -%}
{%- raw %}
- name: Set cargo registry
run: |
cat <<"EOF" | tee ~/.cargo/config.toml
[registry]
global-credential-providers = ["cargo:token"]
EOF
- name: Login to cargo registry
env:
{%- endraw %}
CARGO_REGISTRY: git_{{git-owner}}
{%- raw %}
run: cargo login --registry $CARGO_REGISTRY "Bearer ${{ secrets.CRATES_TOKEN }}"
- name: Run Tests
run: |
make prepare
make test
curl --silent -X 'POST' \
'${{ github.api_url }}/repos/${{ github.repository }}/issues/${{ github.event.number }}/comments' \
-H 'accept: application/json' \
-H 'Authorization: token ${{ secrets.ISSUE_API_RW_TOKEN }}' \
-H 'Content-Type: application/json' \
-d "{\"body\":\"# Code Coverage:\n$(cat target/coverage/statistic.md)\"}"
{%- endraw %}