added another example #9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Test and Deploy | |
| on: | |
| push: | |
| branches: | |
| - master | |
| workflow_dispatch: | |
| env: | |
| CARGO_TERM_COLOR: always | |
| permissions: | |
| contents: write | |
| jobs: | |
| test: | |
| if: github.event_name != 'workflow_dispatch' | |
| name: Test | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Test | |
| run: cargo test | |
| tag: | |
| if: github.event_name != 'workflow_dispatch' | |
| name: Create Git Tag | |
| needs: test | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Extract version from Cargo.toml | |
| run: | | |
| VERSION=$(grep '^version' strong-api-fetch/Cargo.toml | head -n 1 | sed -E 's/version *= *"([^"]+)".*/\1/') | |
| echo "VERSION=$VERSION" >> $GITHUB_ENV | |
| echo "Version extracted: $VERSION" | |
| - name: Check existing tag | |
| id: check_tag | |
| run: | | |
| LATEST="$(git tag --sort=-v:refname | head -n 1 || echo "")" | |
| echo "Latest tag: $LATEST" | |
| if [ "$LATEST" = "$VERSION" ]; then | |
| echo "Tag is up-to-date. No new tag needed." | |
| echo "tag_created=false" >> $GITHUB_OUTPUT | |
| else | |
| echo "New version detected: $VERSION" | |
| echo "tag_created=true" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Create and push Git tag | |
| if: steps.check_tag.outputs.tag_created == 'true' | |
| run: | | |
| git config user.name "github-actions" | |
| git config user.email "github-actions@github.com" | |
| git tag "$VERSION" | |
| git push origin "$VERSION" | |
| deploy: | |
| if: github.event_name == 'workflow_dispatch' | |
| name: Deploy to Server | |
| runs-on: ubuntu-latest | |
| environment: | |
| name: production | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Extract version from Cargo.toml | |
| run: | | |
| VERSION=$(grep '^version' strong-api-fetch/Cargo.toml | head -n 1 | sed -E 's/version *= *"([^"]+)".*/\1/') | |
| echo "VERSION=$VERSION" >> $GITHUB_ENV | |
| echo "Version extracted: $VERSION" | |
| - name: SSH into Server and Deploy | |
| env: | |
| PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
| SSH_USER: ${{ secrets.SSH_USER }} | |
| SSH_HOST: ${{ secrets.SSH_HOST }} | |
| SSH_PATH: ${{ secrets.SSH_PATH }} | |
| VERSION: ${{ env.VERSION }} | |
| run: | | |
| echo "$PRIVATE_KEY" > private_key.pem | |
| chmod 600 private_key.pem | |
| ssh -o StrictHostKeyChecking=no -i private_key.pem $SSH_USER@$SSH_HOST <<EOF | |
| set -e | |
| cd $SSH_PATH && \ | |
| git fetch origin --tags && \ | |
| git checkout "refs/tags/$VERSION" && \ | |
| docker build -t strong-api-fetch:"$VERSION" --build-arg VERSION="$VERSION" . && \ | |
| docker stop strong-api-fetch || true && \ | |
| sleep 3 && \ | |
| docker rm strong-api-fetch || true && \ | |
| sleep 1 && \ | |
| SSH_PATH=$SSH_PATH VERSION=$VERSION docker compose up -d && \ | |
| sleep 3 && \ | |
| if [ \$(docker ps -q -f name=strong-api-fetch | wc -l) -eq 0 ]; then | |
| echo "WARN: Container takes more time than usual to spin up" && \ | |
| sleep 4 && \ | |
| if [ \$(docker ps -q -f name=strong-api-fetch | wc -l) -eq 0 ]; then | |
| echo "ERROR: Container failed to start. Showing logs:" && \ | |
| docker logs strong-api-fetch && \ | |
| exit 1 | |
| fi | |
| fi | |
| echo "Deployment successful - container is running" | |
| EOF | |
| if [ $? -ne 0 ]; then | |
| echo "Deployment failed" | |
| exit 1 | |
| fi | |
| rm -f private_key.pem |