diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 33825ad..ceccb79 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -2,7 +2,9 @@ name: Deploy to Production on: push: - branches: [main] + branches: + - main + - 'hotfix/**' jobs: build: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..65a1c46 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,39 @@ +name: CI - Staging + +on: + push: + branches-ignore: [main] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: '20' + - run: corepack enable && corepack prepare pnpm@10.33.0 --activate + - run: pnpm install --no-frozen-lockfile + - run: pnpm build + - run: pnpm test + + docker: + needs: build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set tag + id: meta + run: echo "branch=$(echo ${{ github.ref_name }} | sed 's/\//-/g')" >> $GITHUB_OUTPUT + - uses: docker/setup-buildx-action@v2 + - uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + - uses: docker/build-push-action@v4 + with: + context: . + push: true + tags: | + chafah/nodejs-app:${{ steps.meta.outputs.branch }}-dev + chafah/nodejs-app:dev diff --git a/.github/workflows/deploy-staging.yml b/.github/workflows/deploy-staging.yml new file mode 100644 index 0000000..3ebb285 --- /dev/null +++ b/.github/workflows/deploy-staging.yml @@ -0,0 +1,72 @@ +name: Deploy to Staging + +on: + push: + branches: + - 'release/**' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: '20' + - run: corepack enable && corepack prepare pnpm@10.33.0 --activate + - run: pnpm install --no-frozen-lockfile + - run: pnpm build + - run: pnpm test + + docker: + needs: build + runs-on: ubuntu-latest + env: + NODE_ENV: staging + outputs: + image_tag: ${{ steps.meta.outputs.tag }} + steps: + - uses: actions/checkout@v4 + - name: Set image tag + id: meta + run: | + BRANCH=$(echo ${{ github.ref_name }} | sed 's/\//-/g') + echo "tag=${BRANCH}-$(date +'%Y-%m-%d-%H%M%S')-staging" >> $GITHUB_OUTPUT + - uses: docker/setup-buildx-action@v2 + - uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + - uses: docker/build-push-action@v4 + with: + context: . + push: true + build-args: NODE_ENV=staging + tags: | + chafah/nodejs-app:${{ steps.meta.outputs.tag }} + chafah/nodejs-app:staging + + deploy: + needs: docker + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + - uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} + - run: aws eks update-kubeconfig --name landmark-eks-stg --region ${{ secrets.AWS_REGION }} + - name: Update image tag in k8s manifest + run: | + sed -i "s|image:.*|image: chafah/nodejs-app:${{ needs.docker.outputs.image_tag }}|" k8s/05-service/deployment-service.yaml + - name: Commit updated manifest to repo + run: | + git config user.name "github-actions" + git config user.email "github-actions@github.com" + git add k8s/05-service/deployment-service.yaml + git commit -m "chore: update staging image to ${{ needs.docker.outputs.image_tag }}" || true + git push + - run: kubectl apply -f k8s/05-service/deployment-service.yaml