fix: include cache in build and push step #4
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: CI - Build and Publish Docker Image | |
| on: | |
| push: | |
| branches: [main] | |
| workflow_dispatch: | |
| env: | |
| IMAGE_NAME: ghcr.io/esc-chula/esc-recruit/esc-recruit-app | |
| ORG_NAME: esc-chula | |
| PACKAGE_NAME: esc-recruit/esc-recruit-app | |
| jobs: | |
| get-version: | |
| name: Get New Version | |
| runs-on: ubuntu-latest | |
| permissions: | |
| packages: read | |
| outputs: | |
| version: ${{ steps.version.outputs.version }} | |
| steps: | |
| - name: Calculate new version | |
| id: version | |
| run: | | |
| ENCODED_PACKAGE_NAME=$(echo "${{ env.PACKAGE_NAME }}" | sed 's/\//%2F/g') | |
| API_URL="https://api.github.com/orgs/${{ env.ORG_NAME }}/packages/container/${ENCODED_PACKAGE_NAME}/versions" | |
| LATEST_TAG=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github+json" "${API_URL}" | jq -r '.[].name' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1) | |
| if [[ -z "$LATEST_TAG" ]]; then | |
| NEW_VERSION="1.0.0" | |
| else | |
| NEW_VERSION=$(echo "$LATEST_TAG" | awk -F. -v OFS=. '{$NF = $NF + 1;} 1') | |
| fi | |
| echo "version=${NEW_VERSION}" >> $GITHUB_OUTPUT | |
| build-and-push: | |
| name: Build and Push Image | |
| runs-on: ubuntu-latest | |
| needs: get-version | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Log in to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Extract metadata for Docker | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.IMAGE_NAME }} | |
| tags: | | |
| type=raw,value=${{ needs.get-version.outputs.version }} | |
| type=raw,value=latest,enable={{is_default_branch}} | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache | |
| cache-to: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache,mode=max |