|
| 1 | +name: CI/CD Pipeline |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +jobs: |
| 9 | + tag-commit: |
| 10 | + name: Tag commit with the new version |
| 11 | + runs-on: ubuntu-latest |
| 12 | + outputs: |
| 13 | + version: ${{ steps.tag.outputs.version }} |
| 14 | + env: |
| 15 | + VERSION_PATH: arabot/__init__.py |
| 16 | + VERSION_REGEX: '^\+__version__ = "(.+)"$' |
| 17 | + steps: |
| 18 | + - name: Checkout commit |
| 19 | + uses: actions/checkout |
| 20 | + with: |
| 21 | + sparse-checkout: ${{ env.VERSION_PATH }} |
| 22 | + sparse-checkout-cone-mode: false |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + - name: Set credentials |
| 26 | + run: | |
| 27 | + git config user.name github-actions |
| 28 | + git config user.email 41898282+github-actions[bot]@users.noreply.github.com |
| 29 | +
|
| 30 | + - name: Tag commit |
| 31 | + id: tag |
| 32 | + run: | |
| 33 | + LAST_TAG=$(git describe --abbrev=0) |
| 34 | + VERSION=$(git diff "$LAST_TAG" HEAD "$VERSION_PATH" | sed -nr "s/$VERSION_REGEX/\1/p") |
| 35 | + [ "$VERSION" ] && git tag -m "v$VERSION" "$VERSION" |
| 36 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 37 | +
|
| 38 | + - name: Push tag |
| 39 | + if: steps.tag.outputs.version |
| 40 | + run: git push --tags |
| 41 | + |
| 42 | + build-and-push-image: |
| 43 | + name: Build, tag and push Docker image to Docker Hub and GHCR |
| 44 | + needs: tag-commit |
| 45 | + if: needs.tag-commit.outputs.version |
| 46 | + runs-on: ubuntu-latest |
| 47 | + steps: |
| 48 | + - name: Set up Docker Buildx |
| 49 | + uses: docker/setup-buildx-action |
| 50 | + |
| 51 | + - name: Log in to Docker Hub |
| 52 | + uses: docker/login-action@v3 |
| 53 | + with: |
| 54 | + username: ${{ vars.DOCKERHUB_USERNAME }} |
| 55 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 56 | + |
| 57 | + - name: Log in to GitHub Container Registry |
| 58 | + uses: docker/login-action@v3 |
| 59 | + with: |
| 60 | + registry: ghcr.io |
| 61 | + username: ${{ github.repository_owner }} |
| 62 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 63 | + |
| 64 | + - name: Build and push Docker images |
| 65 | + id: build |
| 66 | + uses: docker/build-push-action@v6 |
| 67 | + with: |
| 68 | + push: true |
| 69 | + provenance: mode=max |
| 70 | + sbom: true |
| 71 | + tags: | |
| 72 | + ${{ vars.DOCKERHUB_USERNAME }}/arabot:latest |
| 73 | + ${{ vars.DOCKERHUB_USERNAME }}/arabot:${{ needs.tag-commit.outputs.version }} |
| 74 | + ghcr.io/${{ github.repository }}:latest |
| 75 | + ghcr.io/${{ github.repository }}:${{ needs.tag-commit.outputs.version }} |
| 76 | + cache-from: type=gha |
| 77 | + cache-to: type=gha,mode=max |
| 78 | + |
| 79 | + deploy: |
| 80 | + name: Trigger redeploy on Railway |
| 81 | + needs: build-and-push-image |
| 82 | + runs-on: ubuntu-latest |
| 83 | + container: ghcr.io/railwayapp/cli |
| 84 | + env: |
| 85 | + RAILWAY_TOKEN: ${{ secrets.RAILWAY_PROJECT_TOKEN }} |
| 86 | + steps: |
| 87 | + - run: railway redeploy -s ${{ vars.RAILWAY_SERVICE_ID }} -y |
0 commit comments