From 392338c6cb7d1fb31e0daa7a158b54feb09618e4 Mon Sep 17 00:00:00 2001 From: Mahdi Khanzadi Date: Sat, 14 Dec 2024 16:22:35 +0100 Subject: [PATCH] build and push images to github container repository (ghcr) --- .github/workflows/backend.yaml | 92 +++++++++++++++++++++++++++++++++ .github/workflows/frontend.yaml | 84 ++++++++++++++++++++++++++++++ .github/workflows/liara.yaml | 67 ------------------------ 3 files changed, 176 insertions(+), 67 deletions(-) create mode 100644 .github/workflows/backend.yaml create mode 100644 .github/workflows/frontend.yaml delete mode 100644 .github/workflows/liara.yaml diff --git a/.github/workflows/backend.yaml b/.github/workflows/backend.yaml new file mode 100644 index 00000000..3dde48cf --- /dev/null +++ b/.github/workflows/backend.yaml @@ -0,0 +1,92 @@ +name: Backend CI and CD +on: + push: + branches: + - main + paths: + - backend/** + pull_request: + # paths: + # - backend/** + +env: + REGISTRY: ghcr.io + IMAGE_NAME: backend + +jobs: + ci: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: '1.23.x' + + - name: Run unit tests + run: | + cd ./backend && go test ./... -v -race -cover + + - name: Provide image name and version + run: | + IMAGE_ID=$(echo $REGISTRY/${{ github.repository_owner }}/$IMAGE_NAME | tr '[A-Z]' '[a-z]') + IMAGE_VERSION=${{ github.sha }} + echo "IMAGE_ID=$IMAGE_ID" >> "$GITHUB_ENV" + echo "IMAGE_VERSION=$IMAGE_VERSION" >> "$GITHUB_ENV" + + - name: Build image + run: | + cd ./backend && docker build . --file Dockerfile --target production --tag $IMAGE_ID:$IMAGE_VERSION --tag $IMAGE_ID:latest + + cd: + runs-on: ubuntu-latest + + # This job will be invoked only on default branch + if: ${{ always() && format('refs/heads/{0}', github.event.repository.default_branch) == github.ref }} + + permissions: + packages: write + contents: read + + needs: + - ci + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Provide image name and version + run: | + IMAGE_ID=$(echo $REGISTRY/${{ github.repository_owner }}/$IMAGE_NAME | tr '[A-Z]' '[a-z]') + IMAGE_VERSION=${{ github.sha }} + echo "IMAGE_ID=$IMAGE_ID" >> "$GITHUB_ENV" + echo "IMAGE_VERSION=$IMAGE_VERSION" >> "$GITHUB_ENV" + + - name: Build image + run: | + cd ./backend && docker build . --file Dockerfile --target production --tag $IMAGE_ID:$IMAGE_VERSION --tag $IMAGE_ID:latest + + - name: Log in to registry + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin + + - name: Push image + run: | + docker push $IMAGE_ID:$IMAGE_VERSION --all-tags + + - uses: actions/setup-node@v3 + with: + node-version: "18" + + - name: deploy to production + env: + LIARA_TOKEN: ${{ secrets.LIARA_API_TOKEN }} + LIARA_BACKEND_APP_NAME: ${{secrets.LIARA_BACKEND_APP_NAME}} + run: | + IMAGE_NAME=backend + IMAGE_ID=$REGISTRY/${{ github.repository_owner }}/$IMAGE_NAME + VERSION=latest + npm i -g @liara/cli@7 + liara deploy --image $IMAGE_ID:$VERSION --platform=docker --port="80" --app="$LIARA_BACKEND_APP_NAME" --api-token="$LIARA_TOKEN" --detach diff --git a/.github/workflows/frontend.yaml b/.github/workflows/frontend.yaml new file mode 100644 index 00000000..83e12d76 --- /dev/null +++ b/.github/workflows/frontend.yaml @@ -0,0 +1,84 @@ +name: Frontend CI and CD +on: + push: + branches: + - main + paths: + - frontend/** + pull_request: + # paths: + # - frontend/** + +env: + REGISTRY: ghcr.io + IMAGE_NAME: frontend + +jobs: + ci: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - uses: actions/setup-node@v3 + with: + node-version: "18" + + - name: Provide image name and version + run: | + IMAGE_ID=$(echo $REGISTRY/${{ github.repository_owner }}/$IMAGE_NAME | tr '[A-Z]' '[a-z]') + IMAGE_VERSION=${{ github.sha }} + echo "IMAGE_ID=$IMAGE_ID" >> "$GITHUB_ENV" + echo "IMAGE_VERSION=$IMAGE_VERSION" >> "$GITHUB_ENV" + + - name: Build image + run: | + cd ./frontend && docker build . --file Dockerfile --target production --tag $IMAGE_ID:$IMAGE_VERSION --tag $IMAGE_ID:latest + + cd: + runs-on: ubuntu-latest + + # This job will be invoked only on default branch + if: ${{ always() && format('refs/heads/{0}', github.event.repository.default_branch) == github.ref }} + + permissions: + packages: write + contents: read + + needs: + - ci + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Provide image name and version + run: | + IMAGE_ID=$(echo $REGISTRY/${{ github.repository_owner }}/$IMAGE_NAME | tr '[A-Z]' '[a-z]') + IMAGE_VERSION=${{ github.sha }} + echo "IMAGE_ID=$IMAGE_ID" >> "$GITHUB_ENV" + echo "IMAGE_VERSION=$IMAGE_VERSION" >> "$GITHUB_ENV" + + - name: Build image + run: | + cd ./frontend && docker build . --file Dockerfile --target production --tag $IMAGE_ID:$IMAGE_VERSION --tag $IMAGE_ID:latest + + - name: Log in to registry + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin + + - name: Push image + run: | + docker push $IMAGE_ID:$IMAGE_VERSION --all-tags + + - uses: actions/setup-node@v3 + with: + node-version: "18" + + - name: deploy to production + env: + LIARA_TOKEN: ${{ secrets.LIARA_API_TOKEN }} + LIARA_FRONTEND_APP_NAME: ${{secrets.LIARA_FRONTEND_APP_NAME}} + run: | + npm i -g @liara/cli@7 + liara deploy --image $IMAGE_ID:$IMAGE_VERSION --platform=docker --port="3000" --app="$LIARA_FRONTEND_APP_NAME" --api-token="$LIARA_TOKEN" --detach diff --git a/.github/workflows/liara.yaml b/.github/workflows/liara.yaml deleted file mode 100644 index 7291cc7b..00000000 --- a/.github/workflows/liara.yaml +++ /dev/null @@ -1,67 +0,0 @@ -name: CI and CD -on: - push: - branches: - - main -jobs: - ci-backend: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Setup Go - uses: actions/setup-go@v5 - with: - go-version: '1.23.x' - - - name: Run unit tests - run: | - cd ./backend && go test ./... -v -race -cover - - cd-backend: - runs-on: ubuntu-latest - - needs: - - ci-backend - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - uses: actions/setup-node@v3 - with: - node-version: "18" - - - name: deploy to production - env: - LIARA_TOKEN: ${{ secrets.LIARA_API_TOKEN }} - LIARA_BACKEND_APP_NAME: ${{secrets.LIARA_BACKEND_APP_NAME}} - LIARA_FRONTEND_APP_NAME: ${{secrets.LIARA_FRONTEND_APP_NAME}} - run: | - npm i -g @liara/cli@7 - cd ./backend && liara deploy --platform=docker --port="80" --app="$LIARA_BACKEND_APP_NAME" --api-token="$LIARA_TOKEN" --detach - - cd-frontend: - runs-on: ubuntu-latest - - needs: - - cd-backend - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - uses: actions/setup-node@v3 - with: - node-version: "18" - - - name: deploy to production - env: - LIARA_TOKEN: ${{ secrets.LIARA_API_TOKEN }} - LIARA_BACKEND_APP_NAME: ${{secrets.LIARA_BACKEND_APP_NAME}} - LIARA_FRONTEND_APP_NAME: ${{secrets.LIARA_FRONTEND_APP_NAME}} - run: | - npm i -g @liara/cli@7 - cd ./frontend && liara deploy --platform=docker --port="3000" --app="$LIARA_FRONTEND_APP_NAME" --api-token="$LIARA_TOKEN" --detach