Refactor Docker image tags in workflow to use consistent naming conve… #3
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 & Push Docker Images to GHCR | |
| on: | |
| push: | |
| branches: | |
| - "**" # all branches | |
| workflow_dispatch: | |
| env: | |
| IMAGE_REGISTRY: ghcr.io | |
| IMAGE_NAMESPACE: ${{ github.repository_owner }} | |
| REPO_NAME: ${{ github.repository }} | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # 1. Checkout repository | |
| - name: Checkout repository | |
| uses: actions/checkout@v3 | |
| # 2. Log in to GHCR | |
| - name: Log in to GHCR | |
| uses: docker/login-action@v2 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| # 3. Set lowercase repo name and branch | |
| - name: Set repo name and branch | |
| run: | | |
| echo "REPO_NAME_LOWER=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV | |
| echo "BRANCH_NAME=$(echo ${{ github.ref_name }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV | |
| # 4. Determine tags | |
| - name: Determine tags | |
| run: | | |
| if [[ "$BRANCH_NAME" == "main" ]]; then | |
| export TAGS="latest" | |
| else | |
| export TAGS="$BRANCH_NAME" | |
| fi | |
| echo "TAGS=$TAGS" >> $GITHUB_ENV | |
| # 5. Build & Push Frontend | |
| - name: Build & Push Frontend Image | |
| run: | | |
| for TAG in $TAGS; do | |
| docker build -t $IMAGE_REGISTRY/$IMAGE_NAMESPACE/${REPO_NAME_LOWER}/app:$TAG ./frontend | |
| docker push $IMAGE_REGISTRY/$IMAGE_NAMESPACE/${REPO_NAME_LOWER}/app:$TAG | |
| done | |
| # 6. Build & Push Backend (API) | |
| - name: Build & Push Backend Image | |
| run: | | |
| for TAG in $TAGS; do | |
| docker build -t $IMAGE_REGISTRY/$IMAGE_NAMESPACE/${REPO_NAME_LOWER}/api:$TAG ./backend | |
| docker push $IMAGE_REGISTRY/$IMAGE_NAMESPACE/${REPO_NAME_LOWER}/api:$TAG | |
| done | |
| # 7. Build & Push Strapi | |
| - name: Build & Push Strapi Image | |
| run: | | |
| for TAG in $TAGS; do | |
| docker build -t $IMAGE_REGISTRY/$IMAGE_NAMESPACE/${REPO_NAME_LOWER}/strapi:$TAG ./strapi | |
| docker push $IMAGE_REGISTRY/$IMAGE_NAMESPACE/${REPO_NAME_LOWER}/strapi:$TAG | |
| done |