From ebb52082759e7c97a6d84c6c666ddee1e8725562 Mon Sep 17 00:00:00 2001 From: trueberryless Date: Thu, 4 Jul 2024 07:00:23 +0200 Subject: [PATCH] deploy version-patch --- .dockerignore | 1 + .github/workflows/docker-hub.yaml | 143 ++++++++++++++++++++++++++++++ Dockerfile | 8 ++ 3 files changed, 152 insertions(+) create mode 100644 .dockerignore create mode 100644 .github/workflows/docker-hub.yaml create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/.github/workflows/docker-hub.yaml b/.github/workflows/docker-hub.yaml new file mode 100644 index 0000000..da93a06 --- /dev/null +++ b/.github/workflows/docker-hub.yaml @@ -0,0 +1,143 @@ +name: Docker Image Push + +on: + push: + branches: [main] + merge_group: + pull_request: + branches: [main] + workflow_dispatch: + inputs: + version: + required: true + type: choice + description: version + options: + - patch + - minor + - major + +concurrency: + group: ${{ github.workflow }}-${{ github.event_name == 'pull_request_target' && github.head_ref || github.ref }} + cancel-in-progress: true + +env: + REGISTRY: docker.io + IMAGE_NAME: trueberryless/true-tracker + NODE_VERSION: 18 + +jobs: + docker-push-image: + if: contains(github.event.head_commit.message, 'version') || github.event_name == 'workflow_dispatch' + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Check out the repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Check if file exists + run: | + if [ -f .github/artifacts/version.json ]; then + echo "File exists" + echo "FILE_EXISTS=true" >> $GITHUB_ENV + else + echo "File does not exist" + echo "FILE_EXISTS=false" >> $GITHUB_ENV + fi + + - name: read_json + if: ${{ env.FILE_EXISTS == 'true' }} + id: version + uses: zoexx/github-action-json-file-properties@release + with: + file_path: ".github/artifacts/version.json" + + - name: save environment variables + if: ${{ env.FILE_EXISTS == 'true' }} + run: | + echo "MAJOR=${{steps.version.outputs.major}}" >> $GITHUB_ENV + echo "MINOR=${{steps.version.outputs.minor}}" >> $GITHUB_ENV + echo "PATCH=${{steps.version.outputs.patch}}" >> $GITHUB_ENV + + - name: create environment variables + if: ${{ env.FILE_EXISTS == 'false' }} + run: | + echo "MAJOR=0" >> $GITHUB_ENV + echo "MINOR=0" >> $GITHUB_ENV + echo "PATCH=0" >> $GITHUB_ENV + + - name: echo environment variables + run: | + echo ${{ env.MINOR }} + echo ${{ env.MINOR }} + echo ${{ env.MINOR }} + + - name: Major version + if: contains(github.event.head_commit.message, 'major') || ${{ github.event.inputs.version }} == 'major' + run: | + echo "New major version" + echo "MAJOR=$((${{ env.MAJOR }}+1))" >> $GITHUB_ENV + echo "MINOR=0" >> $GITHUB_ENV + echo "PATCH=0" >> $GITHUB_ENV + + - name: Minor version + if: contains(github.event.head_commit.message, 'minor') || ${{ github.event.inputs.version }} == 'minor' + run: | + echo "New minor version" + echo "MINOR=$((${{ env.MINOR }}+1))" >> $GITHUB_ENV + echo "PATCH=0" >> $GITHUB_ENV + + - name: Patch version + if: contains(github.event.head_commit.message, 'patch')|| ${{ github.event.inputs.version }} == 'patch' + run: | + echo "New patch version" + echo "PATCH=$((${{ env.PATCH }}+1))" >> $GITHUB_ENV + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push Docker image + uses: docker/build-push-action@v6 + with: + context: . + push: true + tags: | + ${{ env.IMAGE_NAME }}:${{ env.MAJOR}}.${{ env.MINOR}}.${{ env.PATCH}} + ${{ env.IMAGE_NAME }}:latest + labels: ${{ steps.meta.outputs.labels }} + + - name: Check out the repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Create folder if necessary + if: ${{ env.FILE_EXISTS == 'false' }} + run: mkdir -p .github/artifacts + + - name: write_json + id: create-json + uses: jsdaniell/create-json@v1.2.3 + with: + name: "version.json" + json: '{ "major": ${{ env.MAJOR }}, "minor": ${{ env.MINOR }}, "patch": ${{ env.PATCH }} }' + dir: ".github/artifacts/" + + - uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: update version.json (automated) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d132d37 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM node:18 + +WORKDIR /app +COPY package*.json ./ +RUN npm install +COPY . . +EXPOSE 3000 +CMD npm run dev \ No newline at end of file