From e952b30662302afa3f4c873fa71f670a486623ff Mon Sep 17 00:00:00 2001 From: ShiningRay Date: Fri, 18 Nov 2022 12:49:54 +0800 Subject: [PATCH] github actions to build and deploy to k8s --- .dockerignore | 5 +++ .github/workflows/_build_deploy.yml | 61 +++++++++++++++++++++++++++++ .github/workflows/mainnet.yml | 22 ++++------- .github/workflows/staging.yml | 24 ++++-------- .github/workflows/test.yml | 4 +- .github/workflows/testnet.yml | 23 ++++------- .gitignore | 3 ++ .node-version | 1 + .secrets.example | 4 ++ Dockerfile | 2 +- 10 files changed, 101 insertions(+), 48 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/_build_deploy.yml create mode 100644 .node-version create mode 100644 .secrets.example diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..5f1ea22c5 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.github +.husky +node_modules +dist +build diff --git a/.github/workflows/_build_deploy.yml b/.github/workflows/_build_deploy.yml new file mode 100644 index 000000000..5ec96ad96 --- /dev/null +++ b/.github/workflows/_build_deploy.yml @@ -0,0 +1,61 @@ +name: Pull, build, push and deploy +on: + workflow_call: + inputs: + ckb-mode: + required: true + type: string + api-url: + required: true + type: string + k8s-namespace: + required: true + type: string + k8s-workload: + required: true + type: string + secrets: + GHCR_USERNAME: + required: true + GHCR_TOKEN: + required: true + KUBOARD_USERNAME: + required: true + KUBOARD_ACCESS_KEY: + required: true + KUBOARD_API_URL: + required: true + deploy: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + with: + fetch-depth: 0 + + - name: Build and push + id: docker_build + uses: mr-smithers-excellent/docker-build-push@v5 + with: + image: ckb-explorer-frontend + registry: ghcr.io + githubOrg: magickbase # optional + buildArgs: "API_URL=${{ inputs.api-url }},CHAIN_TYPE=${{ inputs.ckb-mode }}" + username: ${{ secrets.GHCR_USERNAME }} + password: ${{ secrets.GHCR_TOKEN }} + - name: Update image on K8S + uses: fjogeleit/http-request-action@v1 + with: + url: ${{ secrets.KUBOARD_API_URL }}/cluster/ckb/kind/CICDApi/ops/resource/updateImageTag + method: 'PUT' + customHeaders: '{"Content-Type": "application/json", "Cookie": "KuboardUsername=${{ secrets.KUBOARD_USERNAME }}; KuboardAccessKey=${{ secrets.KUBOARD_ACCESS_KEY }}"}' + data: '{"kind":"deployments","namespace":"${{ inputs.k8s-namespace }}","name":"${{ inputs.k8s-workload }}","images":{"ckb-explorer-frontend":"${{ steps.docker_build.outputs.imageFullName }}:${{ steps.docker_build.outputs.tags }}"}}' + - name: Restart container + uses: fjogeleit/http-request-action@v1 + with: + url: ${{ secrets.KUBOARD_API_URL }}/cluster/ckb/kind/CICDApi/ops/resource/restartWorkload + method: 'PUT' + customHeaders: '{"Content-Type": "application/json", "Cookie": "KuboardUsername=${{ secrets.KUBOARD_USERNAME }}; KuboardAccessKey=${{ secrets.KUBOARD_ACCESS_KEY }}"}' + data: '{"kind":"deployments","namespace":"${{ inputs.k8s-namespace }}","name":"${{ inputs.k8s-workload }}"}' + + diff --git a/.github/workflows/mainnet.yml b/.github/workflows/mainnet.yml index ca94ec18a..fbb4b0c44 100644 --- a/.github/workflows/mainnet.yml +++ b/.github/workflows/mainnet.yml @@ -1,4 +1,4 @@ -name: CI +name: Deploy to mainnet on: push: @@ -6,18 +6,12 @@ on: - master jobs: deploy: - runs-on: ubuntu-latest + uses: ./.github/workflows/_build_deploy.yml + with: + ckb-mode: mainnet + api-url: $MAINNET_API_URL + k8s-namespace: mainnet + k8s-workload: ckb-explorer-front + secrets: inherit - steps: - - uses: actions/checkout@v1 - with: - fetch-depth: 0 - - name: Push to dokku - uses: dokku/github-action@master - with: - # specify the `main` branch as the remote branch to push to - branch: 'main' - git_push_flags: '--force' - git_remote_url: ${{ secrets.MAINNET_HOST_URL }} - ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index 669505420..6baf6ae32 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -1,4 +1,4 @@ -name: CI +name: Deploy to staging on: push: @@ -6,18 +6,10 @@ on: - develop jobs: deploy: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v1 - with: - fetch-depth: 0 - - - name: Push to dokku - uses: dokku/github-action@master - with: - # specify the `main` branch as the remote branch to push to - branch: 'develop' - git_push_flags: '--force' - git_remote_url: ${{ secrets.STAGING_HOST_URL }} - ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} + uses: ./.github/workflows/_build_deploy.yml + with: + ckb-mode: testnet + api-url: $STAGING_API_URL + k8s-namespace: staging + k8s-workload: ckb-explorer-front + secrets: inherit diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8b1e9ee5f..71d66ad1e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,9 +1,9 @@ -name: CI +name: Unit Test on: [push] jobs: - build: + test: runs-on: ubuntu-latest steps: diff --git a/.github/workflows/testnet.yml b/.github/workflows/testnet.yml index d5249fabb..cddd25287 100644 --- a/.github/workflows/testnet.yml +++ b/.github/workflows/testnet.yml @@ -1,4 +1,4 @@ -name: CI +name: Deploy to testnet on: push: @@ -6,18 +6,11 @@ on: - testnet jobs: deploy: - runs-on: ubuntu-latest + uses: ./.github/workflows/_build_deploy.yml + with: + ckb-mode: testnet + api-url: $TESTNET_API_URL + k8s-namespace: testnet + k8s-workload: ckb-explorer-front + secrets: inherit - steps: - - uses: actions/checkout@v1 - with: - fetch-depth: 0 - - - name: Push to dokku - uses: dokku/github-action@master - with: - # specify the `main` branch as the remote branch to push to - branch: 'testnet' - git_push_flags: '--force' - git_remote_url: ${{ secrets.TESTNET_HOST_URL }} - ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} diff --git a/.gitignore b/.gitignore index 50df045bb..c8258cc44 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ coverage/ *.tmp *.swo +.env.local +.secrets +!.secrets.example diff --git a/.node-version b/.node-version new file mode 100644 index 000000000..3c8e2c277 --- /dev/null +++ b/.node-version @@ -0,0 +1 @@ +v12.22.8 diff --git a/.secrets.example b/.secrets.example new file mode 100644 index 000000000..8a6bda054 --- /dev/null +++ b/.secrets.example @@ -0,0 +1,4 @@ +GHCR_TOKEN=ghp_.... +GHCR_USERNAME=nervos +KUBOARD_USERNAME=devops +KUBOARD_ACCESS_KEY=... diff --git a/Dockerfile b/Dockerfile index b7a44c77c..b07a59f71 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,4 +32,4 @@ RUN rm -rf ./* COPY --from=builder /app/build ./ COPY front.conf /etc/nginx/conf.d/front.conf -EXPOSE 80 \ No newline at end of file +EXPOSE 80