From 4cd67d396747a6b66540c1e3f502685cb41a790a Mon Sep 17 00:00:00 2001 From: Mati Date: Fri, 8 Nov 2024 03:36:52 +0100 Subject: [PATCH] Update CI/CD workflows (#159) --- .github/workflows/check.yml | 54 +++++++++++++++++++++ .github/workflows/{pages.yml => deploy.yml} | 46 +++--------------- app/Dockerfile | 3 +- app/app/package.json | 1 + docker-compose.cicd.yml | 2 + 5 files changed, 67 insertions(+), 39 deletions(-) create mode 100644 .github/workflows/check.yml rename .github/workflows/{pages.yml => deploy.yml} (55%) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..bbd724f --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,54 @@ +name: Check CI + +on: + # Run on pull requests to primary branches + pull_request: + branches: + - master + - main + - dev + paths: + - '.github/workflows/check.yml' + - 'app/**' + - 'docker-compose*.yml' + + # Run on manual triggers + workflow_dispatch: + +# Set GITHUB_TOKEN permissions +permissions: + contents: read + +# Set workflow concurrency rules +concurrency: + group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + + # Build job + build: + name: Build + runs-on: ubuntu-latest + timeout-minutes: 10 + + steps: + + - name: Checkout + uses: actions/checkout@v4 + + - name: Build for production + env: + GITHUB_REPOSITORY_URL: https://github.com/${{ github.repository }} + GITHUB_SHA: ${{ github.sha }} + BUILD_CHECK: "true" + run: docker compose -f docker-compose.yml -f docker-compose.cicd.yml build + + - name: Copy application files + run: docker compose -f docker-compose.yml -f docker-compose.cicd.yml run --rm app + + - name: Check application files + run: | + [ -d ./app/dist ] && [ $(ls -1 ./app/dist | wc -l) -gt 0 ] \ + && echo "Application files found" \ + || ( echo "No application files found" && exit 1 ) diff --git a/.github/workflows/pages.yml b/.github/workflows/deploy.yml similarity index 55% rename from .github/workflows/pages.yml rename to .github/workflows/deploy.yml index 7e79316..3ef73fd 100644 --- a/.github/workflows/pages.yml +++ b/.github/workflows/deploy.yml @@ -1,43 +1,25 @@ -name: Deploy to GitHub Pages +name: Deploy CD on: - # Run on push events on primary branches + # Run on push on primary branches push: branches: - master - - dev + - main paths: - - '.github/workflows/pages.yml' - - 'app/**' - - 'docker-compose*.yml' - - # Run on pull request events to primary branches - pull_request: - branches: - - master - paths: - - '.github/workflows/pages.yml' + - '.github/workflows/deploy.yml' - 'app/**' - 'docker-compose*.yml' # Run on manual triggers workflow_dispatch: - inputs: - environment: - description: 'Workflow environment' - required: true - type: choice - options: - - development - - production - default: 'production' # Set GITHUB_TOKEN permissions for the workflow permissions: contents: read -# Restrict concurrent deployments +# Set workflow concurrency rules concurrency: group: ${{ github.workflow }} cancel-in-progress: true @@ -56,10 +38,11 @@ jobs: uses: actions/checkout@v4 - name: Build for production - run: docker compose -f docker-compose.yml -f docker-compose.cicd.yml build env: GITHUB_REPOSITORY_URL: https://github.com/${{ github.repository }} GITHUB_SHA: ${{ github.sha }} + BUILD_CHECK: "false" + run: docker compose -f docker-compose.yml -f docker-compose.cicd.yml build - name: Copy application files run: docker compose -f docker-compose.yml -f docker-compose.cicd.yml run --rm app @@ -68,12 +51,6 @@ jobs: run: sudo chown -R $USER:$USER ./app/dist - name: Upload artifact - # Only run on: - # - Push events to `master`. - # - Manual triggers on `production` environment. - if: | - (github.event_name == 'push' && github.ref == 'refs/heads/master') || - (github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'production') uses: actions/upload-pages-artifact@v3 with: path: ./app/dist @@ -88,19 +65,12 @@ jobs: needs: - build - # Only run on: - # - Push events to `master`. - # - Manual triggers on `production` environment. - if: | - (github.event_name == 'push' && github.ref == 'refs/heads/master') || - (github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'production') - # Set GITHUB_TOKEN permissions for the job permissions: pages: write id-token: write - # Set the deployment environment + # Set deployment environment environment: name: production url: ${{ steps.deployment.outputs.page_url }} diff --git a/app/Dockerfile b/app/Dockerfile index 0754218..4d6c2ba 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -83,7 +83,8 @@ ARG GITHUB_SHA="" ENV GITHUB_SHA=${GITHUB_SHA} # Build application -RUN npm run build +ARG BUILD_CHECK=true +RUN [ "${BUILD_CHECK}" == "true" ] && npm run build || npm run build-only # -- diff --git a/app/app/package.json b/app/app/package.json index e2de6cd..1802601 100644 --- a/app/app/package.json +++ b/app/app/package.json @@ -9,6 +9,7 @@ "dev": "astro dev", "start": "astro dev", "build": "astro check && astro build", + "build-only": "astro build", "preview": "astro preview", "astro": "astro" }, diff --git a/docker-compose.cicd.yml b/docker-compose.cicd.yml index 80a7f01..145a097 100644 --- a/docker-compose.cicd.yml +++ b/docker-compose.cicd.yml @@ -7,6 +7,8 @@ services: image: ${IMAGES_PREFIX:-app}-cicd:${DOCKER_TAG:-latest} build: target: app_prod_build + args: + BUILD_CHECK: ${BUILD_CHECK:-true} # Deploy override volumes: - ./app/dist:/tmp/dist