Feature/demo #108
Workflow file for this run
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: Release | |
| on: | |
| push: | |
| branches: | |
| - main | |
| pull_request: | |
| branches: | |
| - main | |
| types: [opened, synchronize, reopened] | |
| permissions: | |
| contents: write | |
| packages: write | |
| jobs: | |
| release-snapshot: | |
| if: github.event_name == 'pull_request' | |
| name: Release Snapshot | |
| concurrency: | |
| group: docker-${{ github.head_ref || github.ref_name }} | |
| cancel-in-progress: true | |
| runs-on: ubuntu-latest | |
| environment: local | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v4 | |
| - name: Get Project Name | |
| id: project_name | |
| run: echo "PROJECT_NAME=$(jq -r '.name' package.json)" >> $GITHUB_ENV | |
| - name: Get Project Version | |
| id: get_version | |
| run: echo "VERSION=$(jq -r '.version' package.json)" >> $GITHUB_ENV | |
| - name: Compute PR-based snapshot suffix | |
| if: github.event_name == 'pull_request' | |
| run: echo "SNAPSHOT_SUFFIX=-pr-${{ github.event.number }}" >> $GITHUB_ENV | |
| - name: Check for existing release tag (only on PR) | |
| if: github.event_name == 'pull_request' | |
| env: | |
| VERSION: ${{ env.VERSION }} | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| TAG_EXISTS=$(curl -H "Authorization: token $GITHUB_TOKEN" -s https://api.github.com/repos/${{ github.repository }}/releases | jq -r ".[] | select(.tag_name == \"v$VERSION\") | .tag_name") | |
| if [ "$TAG_EXISTS" == "v$VERSION" ]; then | |
| echo "Release tag v$VERSION already exists. Rejecting PR." | |
| exit 1 | |
| else | |
| echo "No existing release with tag v$VERSION found. Proceeding with build." | |
| fi | |
| # Logins | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_PAT }} | |
| - name: Build and Push docker image (DockerHub) | |
| env: | |
| PROJECT_NAME: ${{ env.PROJECT_NAME }} | |
| VERSION: ${{ env.VERSION }} | |
| SNAPSHOT_SUFFIX: ${{ env.SNAPSHOT_SUFFIX }} | |
| DOCKERHUB_ORG: ${{ secrets.DOCKERHUB_ORG }} | |
| run: | | |
| set -euo pipefail | |
| TAG="v${VERSION}${SNAPSHOT_SUFFIX}" | |
| DH_IMAGE="docker.io/${DOCKERHUB_ORG}/${PROJECT_NAME}:${TAG}" | |
| docker build -f Dockerfile --build-arg SKIP_TESTS=true -t "${DH_IMAGE}" . | |
| docker push "${DH_IMAGE}" | |
| release: | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| name: Release | |
| runs-on: ubuntu-latest | |
| environment: local | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v4 | |
| - name: Get Project Version | |
| id: get_version | |
| run: echo "VERSION=$(jq -r '.version' package.json)" >> $GITHUB_ENV | |
| - name: Get Project Name | |
| id: project_name | |
| run: echo "PROJECT_NAME=$(jq -r '.name' package.json)" >> $GITHUB_ENV | |
| - name: Check for existing release tag (only on PR) | |
| if: github.event_name == 'pull_request' | |
| env: | |
| VERSION: ${{ env.VERSION }} | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| TAG_EXISTS=$(curl -H "Authorization: token $GITHUB_TOKEN" -s https://api.github.com/repos/${{ github.repository }}/releases | jq -r ".[] | select(.tag_name == \"v$VERSION\") | .tag_name") | |
| if [ "$TAG_EXISTS" == "v$VERSION" ]; then | |
| echo "Release tag v$VERSION already exists. Rejecting PR." | |
| exit 1 | |
| else | |
| echo "No existing release with tag v$VERSION found. Proceeding with build." | |
| fi | |
| # Logins | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_PAT }} | |
| - name: Build and Push docker image (DockerHub) | |
| env: | |
| PROJECT_NAME: ${{ env.PROJECT_NAME }} | |
| VERSION: ${{ env.VERSION }} | |
| DOCKERHUB_ORG: ${{ secrets.DOCKERHUB_ORG }} | |
| run: | | |
| set -euo pipefail | |
| TAG="v${VERSION}" | |
| DH_IMAGE="docker.io/${DOCKERHUB_ORG}/${PROJECT_NAME}:${TAG}" | |
| DH_LATEST="docker.io/${DOCKERHUB_ORG}/${PROJECT_NAME}:latest" | |
| docker build -f Dockerfile --build-arg SKIP_TESTS=true -t "${DH_IMAGE}" . | |
| docker tag "${DH_IMAGE}" "${DH_LATEST}" | |
| docker push "${DH_IMAGE}" | |
| docker push "${DH_LATEST}" | |
| - name: Create GitHub Release | |
| uses: actions/create-release@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| tag_name: "v${{ env.VERSION }}" | |
| release_name: "v${{ env.VERSION }}" | |
| body: "Release of version v${{ env.VERSION }}" | |
| draft: false | |
| prerelease: false |