From d696811be79bde96d455587f0ad236046f6e67e6 Mon Sep 17 00:00:00 2001 From: Ryn Cao Date: Sat, 13 Jul 2024 14:59:36 +0800 Subject: [PATCH] improvement: deploy fly app after image test and reuse ghcr image --- .github/workflows/ci.yml | 6 +++-- .github/workflows/docker.yml | 34 ++++------------------- .github/workflows/release.yml | 50 +++++++++++++++++----------------- docker-compose.yml | 51 +++++++++++++++++------------------ 4 files changed, 58 insertions(+), 83 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5ee9471..bffe9b1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,9 +1,11 @@ name: Elixir CI on: push: - branches: ["main"] + branches: + - main pull_request: - branches: ["main"] + branches: + - main workflow_call: diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 2c5581c..dc1c542 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -13,34 +13,10 @@ jobs: test-docker: runs-on: ubuntu-latest services: - api-server: - image: cao7113/hello-api-elixir - ports: - - 4000:4000 + - name: Checkout code + uses: actions/checkout@v4 - # https://github.com/cao7113/hello-api-elixir/pkgs/container/hello-api-elixir - # Fix: "unsupported media type application/vnd.oci.empty.v1+json" - # use ghcr.io/cao7113/hello-api-elixir:v0.1.8 instead of ghcr.io/cao7113/hello-api-elixir:sha256-b3de0aa2c4d72189aeb6e36fe8377400e1f9e6f4fbc60270b3e05b2e3350462b - api-server2: - image: ghcr.io/cao7113/hello-api-elixir - ports: - - 4002:4000 - - steps: - # - name: Checkout code - # uses: actions/checkout@v4 - - - name: Ping api-server - run: | - sleep 1 - echo Ping api-server - curl -sSL http://localhost:4000/ping - echo Ping api-server2 - curl -sSL http://localhost:4002/ping - - - name: Api build info + - name: Run app in docker compose env run: | - echo api-server build info: - curl -sSL http://localhost:4000 | jq - echo api-server2 build info: - curl -sSL http://localhost:4002 | jq + docker compose version + docker compose up --exit-code-from curl # --abort-on-container-exit diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 12d24f7..78a9dff 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -16,31 +16,6 @@ jobs: name: Check CI uses: ./.github/workflows/ci.yml - # direct use ghcr or docker-hub image? - fly-deploy: - name: Deploy fly app - runs-on: ubuntu-latest - needs: check-ci - concurrency: deploy-group - steps: - - name: Checkout code - uses: actions/checkout@v4 - - # https://github.com/superfly/flyctl-actions - # https://github.com/marketplace/actions/github-action-for-flyctl - - name: Setup flyctl - uses: superfly/flyctl-actions/setup-flyctl@master - - - name: Deploy app - run: flyctl deploy --remote-only --build-arg GIT_COMMIT_ID=$(git log -1 --format="%H") --build-arg GIT_COMMIT_TIME=$(git log -1 --format="%ct") - env: - # https://github.com/superfly/flyctl-actions?tab=readme-ov-file#secrets - # https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/#speed-run-your-way-to-continuous-deployment - # gen cmd: fly tokens create deploy -x 999999h - # set in Github Settings -> Secrets -> Actions -> Repository secrets - # https://github.com/cao7113/hello-api-elixir/settings/secrets/actions - FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} - push-images: name: Push Docker image to Docker Hub and Github Packages runs-on: ubuntu-latest @@ -115,6 +90,31 @@ jobs: needs: push-images uses: ./.github/workflows/docker.yml + # direct use ghcr or docker-hub image? + fly-deploy: + name: Deploy fly app + runs-on: ubuntu-latest + needs: test-docker-images + concurrency: deploy-group + steps: + - name: Checkout code + uses: actions/checkout@v4 + + # https://github.com/superfly/flyctl-actions + # https://github.com/marketplace/actions/github-action-for-flyctl + - name: Setup flyctl + uses: superfly/flyctl-actions/setup-flyctl@master + + - name: Deploy fly app + run: flyctl deploy --image ghcr.io/${{ github.repository }}:${{ github.ref_name }} --debug --verbose + env: + # https://github.com/superfly/flyctl-actions?tab=readme-ov-file#secrets + # https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/#speed-run-your-way-to-continuous-deployment + # https://github.com/cao7113/hello-phx/settings/secrets/actions + # gen cmd: fly tokens create deploy -x 999999h + # set in Github Settings -> Secrets -> Actions -> Repository secrets + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} + release-tar: # strategy: # matrix: diff --git a/docker-compose.yml b/docker-compose.yml index 1c15d8c..5b43523 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,8 +2,8 @@ services: # https://github.com/cao7113/hello-phx/pkgs/container/hello-api-elixir api: - # image: ghcr.io/cao7113/hello-api-elixir - build: . + image: ghcr.io/cao7113/hello-api-elixir + # build: . # environment: # MIX_ENV: "prod" # https://docs.docker.com/compose/compose-file/05-services/#healthcheck @@ -12,7 +12,7 @@ services: interval: 1m30s timeout: 10s retries: 3 - start_period: 40s + start_period: 30s start_interval: 5s # http://hello-api-elixir-api-1.orb.local/ @@ -20,33 +20,30 @@ services: curl: image: curlimages/curl:8.8.0 depends_on: - - api + api: + condition: service_healthy command: > sh -c " - sleep 2 - echo ping api - curl http://api/ping + # echo + # ping -c 3 api echo - echo ping api at port 4000 - curl http://api:4000/ping + echo ping api + curl -sSL http://api:4000/ping echo - ping -c 3 api - ping -q api + echo get build info + curl -sSL http://api:4000/ " - tool: - image: busybox - depends_on: - - api - command: > - sh -c " - sleep 2 - echo ping api - wget -qO- http://api/ping - echo - echo ping api at port 4000 - wget -qO- http://api:4000/ping - echo - ping -c 3 api - ping -q api - " \ No newline at end of file + # tool: + # image: busybox + # depends_on: + # api: + # condition: service_healthy + # command: > + # sh -c " + # # echo + # # ping -c 3 api + # echo + # echo ping api at port 4000 + # wget -qO- http://api:4000/ping + # " \ No newline at end of file