name: Build & Publish Artifact on: workflow_call: inputs: env_json: required: true type: string bats-image: required: true type: string cucumber-node-image: required: true type: string secrets: GITEA_TOKEN: required: true GIT_PAGES_PUBLISH_TOKEN: required: true env: GITEA_API_URL: ${{ fromJson(inputs.env_json).GITEA_API_URL }} GIT_PAGES_URL: ${{ fromJson(inputs.env_json).GIT_PAGES_URL }} GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} GIT_PAGES_PUBLISH_TOKEN: ${{ secrets.GIT_PAGES_PUBLISH_TOKEN }} REPO: ${{ github.repository }} concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: check: runs-on: ubuntu-latest outputs: artifact_exists: ${{ steps.check.outputs.artifact_exists }} next_version: ${{ steps.check.outputs.next_version }} steps: - uses: actions/checkout@v4 - name: Set Gitea status to PENDING run: | echo "===== gitea-ci-library - Check existing artifact | begin =====" bash scripts/report-status.sh pending "Checking version..." ci-check - name: Check existing artifact and calculate version id: check run: | RAW_VERSION=$(jq -r '.version' package.json) BASE_VERSION=$(echo "$RAW_VERSION" | cut -d'.' -f1-2) echo "gitea-ci-library - Tunnistettu Major.Minor versio: $BASE_VERSION" TAGS_JSON=$(curl -s -f -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ "${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/tags") TAG=$(echo "$TAGS_JSON" | jq -r 'if type == "array" then .[] | select(.commit.sha == "${{ github.sha }}") | .name else empty end' | head -1) if [ -n "$TAG" ]; then echo "artifact_exists=true" >> "$GITHUB_OUTPUT" echo "next_version=$TAG" >> "$GITHUB_OUTPUT" echo "gitea-ci-library - Artefakti löytyi jo tagilla: $TAG." else echo "artifact_exists=false" >> "$GITHUB_OUTPUT" HIGHEST_PATCH=$(echo "$TAGS_JSON" | jq -r --arg bv "$BASE_VERSION." ' if type == "array" then .[] | .name | select(startswith($bv)) | sub($bv; "") | tonumber else empty end' | sort -rn | head -1) if [ -z "$HIGHEST_PATCH" ]; then NEXT_PATCH=0; else NEXT_PATCH=$((HIGHEST_PATCH + 1)); fi FULL_VERSION="${BASE_VERSION}.${NEXT_PATCH}" echo "next_version=$FULL_VERSION" >> "$GITHUB_OUTPUT" echo "gitea-ci-library - Uusi vapaa versio: $FULL_VERSION" fi - name: Set Gitea status to SUCCESS if: success() env: EXISTS: ${{ steps.check.outputs.artifact_exists }} VERSION: ${{ steps.check.outputs.next_version }} run: | echo "===== gitea-ci-library - Check existing artifact | success =====" if [ "${EXISTS}" = "true" ]; then bash scripts/report-status.sh success "Skip build: version $VERSION exists" ci-check else bash scripts/report-status.sh success "Build version $VERSION required" ci-check fi - name: Set Gitea status to FAILURE if: failure() run: bash scripts/report-status.sh failure "Check version FAILED" ci-check quality-gate: needs: [check] if: needs.check.outputs.artifact_exists == 'false' uses: niko/gitea-ci-library/.gitea/workflows/quality-gate.yml@main secrets: inherit with: env_json: ${{ inputs.env_json }} bats-image: ${{ inputs.bats-image }} cucumber-node-image: ${{ inputs.cucumber-node-image }} build: runs-on: ubuntu-latest needs: [check, quality-gate] if: needs.quality-gate.result == 'success' && needs.check.outputs.artifact_exists == 'false' steps: - uses: actions/checkout@v4 - name: Set Gitea status to PENDING run: | echo "===== gitea-ci-library - Docker Build | begin =====" bash scripts/report-status.sh pending "Building Docker image..." ci-docker-build - name: Build container id: build env: VERSION: ${{ needs.check.outputs.next_version }} run: | NOW=$(date -u +%Y-%m-%dT%H:%M:%SZ) docker build \ --label "git.commit=${{ github.sha }}" \ --label "git.commitBy=${{ github.actor }}" \ --label "build.date=${NOW}" \ -t "minimal:${VERSION}" . - name: Report status SUCCESS if: success() env: VERSION: ${{ needs.check.outputs.next_version }} run: bash scripts/report-status.sh success "Docker build $VERSION OK" ci-docker-build - name: Report status FAILURE if: failure() env: VERSION: ${{ needs.check.outputs.next_version }} run: bash scripts/report-status.sh failure "Docker build $VERSION FAILED" ci-docker-build - name: Save Docker image run: | mkdir -p /tmp/image docker save "minimal:${{ needs.check.outputs.next_version }}" -o /tmp/image/artifact.tar - name: Upload Docker image artifact uses: actions/upload-artifact@v3 with: name: docker-image path: /tmp/image/artifact.tar push: runs-on: ubuntu-latest needs: [check, build] if: needs.build.result == 'success' && needs.check.outputs.artifact_exists == 'false' steps: - uses: actions/checkout@v4 - name: Load saved Docker image uses: actions/download-artifact@v3 with: name: docker-image path: /tmp/image - name: Set Gitea status to PENDING run: | echo "===== gitea-ci-library - Docker Push | begin =====" bash scripts/report-status.sh pending "Pushing to registry..." ci-docker-push - name: Push to Gitea Packages env: GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} VERSION: ${{ needs.check.outputs.next_version }} run: | docker load -i /tmp/image/artifact.tar REGISTRY=$(echo "${{ gitea.server_url }}" | sed -e 's|^https://||' -e 's|^http://||') IMAGE="$REGISTRY/${{ gitea.repository }}:$VERSION" docker tag "minimal:$VERSION" "$IMAGE" echo "$GITEA_TOKEN" | docker login "$REGISTRY" -u "${{ github.actor }}" --password-stdin docker push "$IMAGE" docker logout "$REGISTRY" - name: Report status SUCCESS if: success() env: VERSION: ${{ needs.check.outputs.next_version }} run: bash scripts/report-status.sh success "Docker push $VERSION OK" ci-docker-push - name: Report status FAILURE if: failure() env: VERSION: ${{ needs.check.outputs.next_version }} run: bash scripts/report-status.sh failure "Docker push $VERSION FAILED" ci-docker-push tag-commit: runs-on: ubuntu-latest needs: [check, push] if: needs.push.result == 'success' && needs.check.outputs.artifact_exists == 'false' steps: - uses: actions/checkout@v4 - name: Set Gitea status to PENDING run: | echo "===== gitea-ci-library - Create Tag | begin =====" bash scripts/report-status.sh pending "Creating tag..." ci-docker-tag - name: Create git tag env: GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} REPO: ${{ github.repository }} SERVER_URL: ${{ gitea.server_url }} RUN_NUMBER: ${{ github.run_number }} SHA: ${{ github.sha }} VERSION: ${{ needs.check.outputs.next_version }} run: | HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X POST \ "$SERVER_URL/api/v1/repos/$REPO/tags" \ -H "Authorization: token $GITEA_TOKEN" \ -H "Content-Type: application/json" \ -d "{\"tag_name\": \"$VERSION\", \"message\": \"Build #$RUN_NUMBER\", \"target\": \"$SHA\"}") if [ "$HTTP_CODE" = "201" ] || [ "$HTTP_CODE" = "409" ]; then exit 0 else exit 1 fi - name: Report status SUCCESS if: success() env: VERSION: ${{ needs.check.outputs.next_version }} run: bash scripts/report-status.sh success "Tag $VERSION OK" ci-docker-tag - name: Report status FAILURE if: failure() env: VERSION: ${{ needs.check.outputs.next_version }} run: bash scripts/report-status.sh failure "Tag $VERSION FAILED" ci-docker-tag