From 6d590e909c205d00102b2ac7b8ebc099e42b4f51 Mon Sep 17 00:00:00 2001 From: qypol342 Date: Sun, 10 Nov 2024 12:18:03 +0100 Subject: [PATCH] CI change direction --- .github/workflows/docker-publish.yml | 48 ++++++++++++ .github/workflows/docker-test.yml | 62 +++++++++++++++ .github/workflows/docker.yml | 112 --------------------------- 3 files changed, 110 insertions(+), 112 deletions(-) create mode 100644 .github/workflows/docker-publish.yml create mode 100644 .github/workflows/docker-test.yml delete mode 100644 .github/workflows/docker.yml diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..cee04f6 --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,48 @@ +name: Create and publish a Docker image + +on: + push: + tags: + - '**' # Pattern matched against refs/tags + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-publish-image: + name: Build multi-platform Docker image + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to the Container registry + uses: docker/login-action@v1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v3 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} + cache-from: type=gha + cache-to: type=gha,mode=max \ No newline at end of file diff --git a/.github/workflows/docker-test.yml b/.github/workflows/docker-test.yml new file mode 100644 index 0000000..92ccfe7 --- /dev/null +++ b/.github/workflows/docker-test.yml @@ -0,0 +1,62 @@ +name: Build and Test Docker Image + +on: + push: + branches: + - '**' # Trigger the action on every push to any branch + tags: + - 'v*' # Trigger the action on tags that start with 'v' + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-test-image: + name: Build and Test Docker image + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v3 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build Docker image (single platform) + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/amd64 # Specify a single platform for building + load: true # Load the image into Docker's local cache + tags: ${{ steps.meta.outputs.tags }} + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: Run Docker container for testing + run: | + docker run -d -p 8080:8080 --name test_container ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.tags }} + sleep 2 # Wait for 2 seconds to allow the server to start + + - name: Test health endpoint + run: | + status_code=$(curl -o /dev/null -s -w "%{http_code}" http://localhost:8080/health) + if [ "$status_code" -ne 200 ]; then + echo "Health check failed with status code $status_code" + exit 1 + fi + echo "Health check passed with status code $status_code" + + - name: Stop and remove Docker container + run: | + docker stop test_container + docker rm test_container diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml deleted file mode 100644 index 1c8c1f3..0000000 --- a/.github/workflows/docker.yml +++ /dev/null @@ -1,112 +0,0 @@ -name: Create, test, and publish a Docker image - -on: - push: - branches: - - '**' # Run on every push to any branch - tags: - - 'v*' # Run on every push to tags that start with 'v' - -env: - REGISTRY: ghcr.io - IMAGE_NAME: ${{ github.repository }} - -jobs: - build-image: - name: Build multi-platform Docker image - runs-on: ubuntu-latest - - outputs: - image-tags: ${{ steps.meta.outputs.tags }} - - steps: - - name: Checkout repository - uses: actions/checkout@v2 - - - name: Set up QEMU - uses: docker/setup-qemu-action@v3 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@v3 - with: - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - - - name: Build and push Docker image - uses: docker/build-push-action@v5 - with: - context: . - platforms: linux/amd64,linux/arm64 - push: true # Push the image instead of loading it locally - tags: ${{ steps.meta.outputs.tags }} - cache-from: type=gha - cache-to: type=gha,mode=max - - test-image: - name: Test Docker image - runs-on: ubuntu-latest - needs: build-image # Run after build-image job - - steps: - - name: Checkout repository - uses: actions/checkout@v2 - - - name: Log in to Container Registry - uses: docker/login-action@v1 - with: - registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Pull Docker image for testing - run: | - docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build-image.outputs.image-tags }} - - - name: Run Docker container for testing - run: | - docker run -d -p 8080:8080 --name test_container ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build-image.outputs.image-tags }} - sleep 2 # Wait for 2 seconds to allow the server to start - - - name: Test health endpoint - run: | - status_code=$(curl -o /dev/null -s -w "%{http_code}" http://localhost:8080/health) - if [ "$status_code" -ne 200 ]; then - echo "Health check failed with status code $status_code" - exit 1 - fi - echo "Health check passed with status code $status_code" - - - name: Stop and remove Docker container - run: | - docker stop test_container - docker rm test_container - - publish-image: - name: Publish Docker image - runs-on: ubuntu-latest - needs: test-image # Run only after test-image job completes successfully - if: startsWith(github.ref, 'refs/tags/v') # Only run if a tag starting with 'v' is pushed - - steps: - - name: Checkout repository - uses: actions/checkout@v2 - - - name: Log in to the Container registry - uses: docker/login-action@v1 - with: - registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Publish Docker image - uses: docker/build-push-action@v5 - with: - context: . - platforms: linux/amd64,linux/arm64 - push: true # Push the image in this step - tags: ${{ steps.meta.outputs.tags }} - cache-from: type=gha - cache-to: type=gha,mode=max