name: Conditional Release Build on: push: tags: - v* jobs: read-conditions: runs-on: ubuntu-latest outputs: create-release: ${{ steps.parse-conditions.outputs.create-release }} create-docker-image: ${{ steps.parse-conditions.outputs.create-docker-image }} update-to-release-branch: ${{ steps.parse-conditions.outputs.update-to-release-branch }} steps: - name: Checkout repository run: | git clone https://oauth2:${{ secrets.GITEATOKEN }}@direct-dev.ru/gitea/GiteaAdmin/hello_gitea.git hello_gitea cd hello_gitea git checkout ${{ github.ref }} - name: Parse build conditions id: parse-conditions run: | cd hello_gitea # Default values if file doesn't exist CREATE_RELEASE="1" CREATE_DOCKER_IMAGE="1" UPDATE_TO_RELEASE_BRANCH="1" # Read conditions from file if it exists if [ -f .build.conditions ]; then echo "Reading .build.conditions file..." while IFS=':' read -r job condition; do # Remove leading/trailing whitespace job=$(echo "$job" | xargs) condition=$(echo "$condition" | xargs) case "$job" in "create-release") CREATE_RELEASE="$condition" ;; "create-docker-image") CREATE_DOCKER_IMAGE="$condition" ;; "update-to-release-branch") UPDATE_TO_RELEASE_BRANCH="$condition" ;; esac done < .build.conditions echo "Parsed conditions:" echo " create-release: $CREATE_RELEASE" echo " create-docker-image: $CREATE_DOCKER_IMAGE" echo " update-to-release-branch: $UPDATE_TO_RELEASE_BRANCH" else echo "No .build.conditions file found, using defaults" fi # Set outputs echo "create-release=$CREATE_RELEASE" >> $GITHUB_OUTPUT echo "create-docker-image=$CREATE_DOCKER_IMAGE" >> $GITHUB_OUTPUT echo "update-to-release-branch=$UPDATE_TO_RELEASE_BRANCH" >> $GITHUB_OUTPUT create-release: runs-on: ubuntu-latest needs: read-conditions if: needs.read-conditions.outputs.create-release == '1' container: image: ${{ secrets.DOCKERHUB_USERNAME }}/my-build-golang-runner:latest steps: - name: Checkout repository run: | git clone https://oauth2:${{ secrets.GITEATOKEN }}@direct-dev.ru/gitea/GiteaAdmin/hello_gitea.git hello_gitea cd hello_gitea git checkout ${{ github.ref }} - name: Setup Go run: | git --version go version jq --version - name: Build all binaries run: | cd hello_gitea # Проверяем, изменились ли зависимости if [ ! -f go.sum ] || ! go mod verify >/dev/null 2>&1; then echo "Dependencies changed, downloading..." go mod download fi mkdir -p bin echo "Building for all platforms..." # Build for all platforms using direct go build commands echo "Building for linux amd64..." GOOS=linux GOARCH=amd64 go build -o bin/hello-api-linux-amd64 main.go echo "Building for linux arm64..." GOOS=linux GOARCH=arm64 go build -o bin/hello-api-linux-arm64 main.go echo "Building for windows amd64..." GOOS=windows GOARCH=amd64 go build -o bin/hello-api-windows-amd64.exe main.go echo "Building for darwin amd64..." GOOS=darwin GOARCH=amd64 go build -o bin/hello-api-darwin-amd64 main.go echo "Building for darwin arm64..." GOOS=darwin GOARCH=arm64 go build -o bin/hello-api-darwin-arm64 main.go # Create archives echo "Creating archives..." cd bin # Create archives with correct file names tar -czf hello-api-linux-amd64.tar.gz hello-api-linux-amd64 tar -czf hello-api-linux-arm64.tar.gz hello-api-linux-arm64 tar -czf hello-api-windows-amd64.tar.gz hello-api-windows-amd64.exe tar -czf hello-api-darwin-amd64.tar.gz hello-api-darwin-amd64 tar -czf hello-api-darwin-arm64.tar.gz hello-api-darwin-arm64 echo "Listing bin directory..." ls -la - name: Create Release run: | cd hello_gitea # Create release using Gitea API echo "Creating release..." curl -X POST \ -H "Authorization: token ${{ secrets.GITEATOKEN }}" \ -H "Content-Type: application/json" \ -d '{ "tag_name": "${{ github.ref_name }}", "name": "Release ${{ github.ref_name }}", "body": "Automated release with multi-platform binaries and Docker image", "draft": false, "prerelease": false }' \ "https://direct-dev.ru/gitea/api/v1/repos/GiteaAdmin/hello_gitea/releases" echo "Getting release id..." RELEASE_ID=$(curl -s -H "Authorization: token ${{ secrets.GITEATOKEN }}" \ "https://direct-dev.ru/gitea/api/v1/repos/GiteaAdmin/hello_gitea/releases/tags/${{ github.ref_name }}" | \ jq -r '.id') # Upload all binaries echo "Uploading assets..." for file in bin/*.tar.gz; do echo "Uploading $file..." curl -X POST \ -H "Authorization: token ${{ secrets.GITEATOKEN }}" \ -H "Content-Type: application/octet-stream" \ --data-binary @$file \ "https://direct-dev.ru/gitea/api/v1/repos/GiteaAdmin/hello_gitea/releases/$RELEASE_ID/assets?name=$(basename $file)" done echo "Release created successfully" create-docker-image: runs-on: ubuntu-latest needs: [read-conditions, create-release] if: needs.read-conditions.outputs.create-docker-image == '1' && (needs.create-release.result == 'success' || needs.create-release.result == 'skipped') container: image: docker:28.3.2-dind steps: - name: Checkout repository run: | apk add --no-cache git echo "=== GitHub Variables ===" echo "github.ref = ${{ github.ref }}" echo "github.ref_name = ${{ github.ref_name }}" echo "github.sha = ${{ github.sha }}" echo "github.repository = ${{ github.repository }}" echo "========================" git clone https://oauth2:${{ secrets.GITEATOKEN }}@direct-dev.ru/gitea/GiteaAdmin/hello_gitea.git hello_gitea cd hello_gitea git checkout ${{ github.ref }} - name: Setup Docker Buildx run: | # Docker is already installed in docker:dind image docker --version echo "Setting up Docker Buildx for multi-platform builds..." # Setup Docker Buildx for multi-platform builds docker buildx create --name go-buildx --use docker buildx inspect --bootstrap - name: Login to Docker Hub run: | echo ${{ secrets.DOCKERHUB_TOKEN }} | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin - name: Build multi-platform Docker images run: | cd hello_gitea echo "Building multi-platform images using buildx..." # Build multi-platform images using buildx docker buildx build \ --platform linux/amd64,linux/arm64 \ --tag ${{ secrets.DOCKERHUB_USERNAME }}/hello-api:${{ github.ref_name }} \ --tag ${{ secrets.DOCKERHUB_USERNAME }}/hello-api:latest \ --push \ . echo "Multi-platform images built successfully" - name: Remove buildx run: | echo "Removing buildx..." docker buildx rm go-buildx update-to-release-branch: runs-on: ubuntu-latest needs: [read-conditions, create-docker-image] if: needs.read-conditions.outputs.update-to-release-branch == '1' && (needs.create-docker-image.result == 'success' || needs.create-docker-image.result == 'skipped') container: image: docker:28.3.2-dind steps: - name: Create Release Branch run: | apk add --no-cache git git clone https://oauth2:${{ secrets.GITEATOKEN }}@direct-dev.ru/gitea/GiteaAdmin/hello_gitea.git hello_gitea cd hello_gitea git config user.email "info@direct-dev.ru" git config user.name "Direct-Dev-Robot" if git ls-remote --heads origin release | grep -q release; then git checkout release git pull origin release else git checkout -b release fi git reset --hard ${{ github.ref_name }} git push origin release --force