76 lines
2.5 KiB
YAML
76 lines
2.5 KiB
YAML
name: Build and push backend image
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
paths:
|
|
- backend/**
|
|
- .gitea/workflows/backend-image.yml
|
|
workflow_dispatch:
|
|
|
|
jobs:
|
|
backend-image:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Check out
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Log in to Gitea container registry
|
|
env:
|
|
REGISTRY: git.ipao.vip
|
|
REGISTRY_USERNAME: ${{ github.repository_owner }}
|
|
REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
|
|
run: |
|
|
set -eu
|
|
test -n "$REGISTRY_TOKEN"
|
|
printf '%s' "$REGISTRY_TOKEN" | docker login "$REGISTRY" \
|
|
--username "$REGISTRY_USERNAME" \
|
|
--password-stdin
|
|
|
|
- name: Build and push backend image
|
|
env:
|
|
REGISTRY: git.ipao.vip
|
|
IMAGE_NAME: ${{ github.repository }}
|
|
IMAGE_TAG: ${{ github.sha }}
|
|
run: |
|
|
set -eu
|
|
image="$REGISTRY/$IMAGE_NAME"
|
|
docker build \
|
|
--file backend/Dockerfile \
|
|
--tag "$image:$IMAGE_TAG" \
|
|
backend
|
|
docker push "$image:$IMAGE_TAG"
|
|
if [ "${GITHUB_REF_NAME:-}" = "main" ]; then
|
|
docker tag "$image:$IMAGE_TAG" "$image:latest"
|
|
docker push "$image:latest"
|
|
fi
|
|
|
|
- name: Link package to repository
|
|
env:
|
|
GITEA_API: https://git.ipao.vip/api/v1
|
|
GITEA_OWNER: ${{ github.repository_owner }}
|
|
PACKAGE_NAME: ${{ github.event.repository.name }}
|
|
PACKAGE_VERSION: ${{ github.sha }}
|
|
REPOSITORY_NAME: ${{ github.event.repository.name }}
|
|
GITEA_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
|
|
run: |
|
|
set -eu
|
|
test -n "$GITEA_TOKEN"
|
|
expected_repo="$GITEA_OWNER/$REPOSITORY_NAME"
|
|
package_json="$(curl -fsS \
|
|
--header "Authorization: token $GITEA_TOKEN" \
|
|
"$GITEA_API/packages/$GITEA_OWNER/container/$PACKAGE_NAME/$PACKAGE_VERSION")"
|
|
linked_repo="$(printf '%s' "$package_json" | jq -r '.repository.full_name // empty')"
|
|
if [ -n "$linked_repo" ]; then
|
|
if [ "$linked_repo" != "$expected_repo" ]; then
|
|
echo "package is already linked to $linked_repo" >&2
|
|
exit 1
|
|
fi
|
|
echo "package already linked to $linked_repo"
|
|
exit 0
|
|
fi
|
|
curl -fsS -X POST \
|
|
--header "Authorization: token $GITEA_TOKEN" \
|
|
"$GITEA_API/packages/$GITEA_OWNER/container/$PACKAGE_NAME/-/link/$REPOSITORY_NAME"
|