Merge pull request #16 from CyberLions/staging #56
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: Build and Deploy | |
| on: | |
| push: | |
| branches: [ "main" , "staging" ] | |
| jobs: | |
| build-cms: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Extract branch name | |
| shell: bash | |
| run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT | |
| id: extract_branch | |
| - name: Set up QEMU | |
| uses: docker/setup-qemu-action@v3 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to CCSO Docker Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: registry.psuccso.org | |
| username: robot$ccso-site+github | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Generate API Tag Names | |
| run: | | |
| echo "${{ steps.extract_branch.outputs.branch }}" | |
| if [ ${{ steps.extract_branch.outputs.branch }} = "main" ]; then | |
| echo "TAG_LATEST_CMS=ccso-cms:latest" >> $GITHUB_ENV | |
| echo "TAG_COMMIT_CMS=ccso-cms:$(git rev-parse --short HEAD)" >> $GITHUB_ENV | |
| elif [ ${{ steps.extract_branch.outputs.branch }} = "staging" ]; then | |
| echo "TAG_LATEST_CMS=ccso-cms-stage:latest" >> $GITHUB_ENV | |
| echo "TAG_COMMIT_CMS=ccso-cms-stage:$(git rev-parse --short HEAD)" >> $GITHUB_ENV | |
| else | |
| exit 0 | |
| fi | |
| - name: Build and push CMS | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: ./backend | |
| platforms: linux/amd64,linux/arm64 | |
| file: ./backend/.docker/Dockerfile | |
| push: true | |
| tags: | | |
| registry.psuccso.org/ccso-site/${{ env.TAG_LATEST_CMS }} | |
| registry.psuccso.org/ccso-site/${{ env.TAG_COMMIT_CMS }} | |
| cache-from: type=registry,ref=registry.psuccso.org/ccso-site/${{ env.TAG_LATEST_CMS }} | |
| cache-to: type=inline | |
| build-website: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Extract branch name | |
| shell: bash | |
| run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT | |
| id: extract_branch | |
| - name: Set up QEMU | |
| uses: docker/setup-qemu-action@v3 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to CCSO Docker Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: registry.psuccso.org | |
| username: robot$ccso-site+github | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Generate Website Tag Names | |
| run: | | |
| echo "${{ steps.extract_branch.outputs.branch }}" | |
| if [ ${{ steps.extract_branch.outputs.branch }} = "main" ]; then | |
| echo "TAG_LATEST_WEBSITE=ccso-site:latest" >> $GITHUB_ENV | |
| echo "TAG_COMMIT_WEBSITE=ccso-site:$(git rev-parse --short HEAD)" >> $GITHUB_ENV | |
| elif [ ${{ steps.extract_branch.outputs.branch }} = "staging" ]; then | |
| echo "TAG_LATEST_WEBSITE=ccso-site-stage:latest" >> $GITHUB_ENV | |
| echo "TAG_COMMIT_WEBSITE=ccso-site-stage:$(git rev-parse --short HEAD)" >> $GITHUB_ENV | |
| else | |
| exit 0 | |
| fi | |
| - name: Build and push Website | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: ./frontend | |
| platforms: linux/amd64,linux/arm64 | |
| file: ./frontend/.docker/Dockerfile | |
| push: true | |
| tags: | | |
| registry.psuccso.org/ccso-site/${{ env.TAG_LATEST_WEBSITE }} | |
| registry.psuccso.org/ccso-site/${{ env.TAG_COMMIT_WEBSITE }} | |
| cache-from: type=registry,ref=registry.psuccso.org/ccso-site/${{ env.TAG_LATEST_WEBSITE }} | |
| cache-to: type=inline | |
| deploy-rancher: | |
| needs: [build-cms, build-website] | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Extract branch name | |
| shell: bash | |
| run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT | |
| id: extract_branch | |
| - name: Redeploy Rancher | |
| run: | | |
| url='https://rancher.psuccso.org' | |
| cluster=local | |
| token=${{ secrets.RANCHER_TOKEN }} | |
| redeploy() { | |
| local app=$1 | |
| pod_upgrade_body=$(curl -s -u "$token" \ | |
| -k $url/k8s/clusters/$cluster/v1/apps.deployments/$namespace/$app \ | |
| -X GET \ | |
| -H 'Accept: application/json' \ | |
| -H 'Content-Type: application/json' \ | |
| -H 'Pragma: no-cache' \ | |
| -H 'Cache-Control: no-cache' 2>&1 | sed "s/\"cattle\.io\/timestamp\"\:\"[0-9T:Z-]*\"/\"cattle\.io\/timestamp\":\"$(date -u +"%Y-%m-%dT%H:%M:%SZ")\"/g") | |
| curl -u "$token" \ | |
| -k -s $url/k8s/clusters/$cluster/v1/apps.deployments/$namespace/$app \ | |
| -X PUT \ | |
| -H 'Accept: application/json' \ | |
| -H 'Content-Type: application/json' \ | |
| --data-binary "$pod_upgrade_body" --compressed | |
| } | |
| echo "${{ steps.extract_branch.outputs.branch }}" | |
| if [ ${{ steps.extract_branch.outputs.branch }} = "main" ]; then | |
| namespace=ccso-site | |
| redeploy "backend" | |
| redeploy "frontend" | |
| elif [ ${{ steps.extract_branch.outputs.branch }} = "staging" ]; then | |
| namespace=ccso-site-staging | |
| redeploy "backend" | |
| redeploy "frontend" | |
| else | |
| exit 0 | |
| fi |