From 45a4f9fd9801ccb2f68a7a0f8dee2acaca95f914 Mon Sep 17 00:00:00 2001 From: darkobas Date: Mon, 9 Oct 2023 22:27:32 +0200 Subject: [PATCH] [ci] add vars handling and push to swarm --- .github/workflows/buildx.yml | 51 +++++++----------- .github/workflows/swarm.yml | 92 ++++++++++++++++++++++++++++++++ .github/workflows/variables.json | 83 ++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+), 31 deletions(-) create mode 100644 .github/workflows/swarm.yml create mode 100644 .github/workflows/variables.json diff --git a/.github/workflows/buildx.yml b/.github/workflows/buildx.yml index 984e495..746b2b0 100644 --- a/.github/workflows/buildx.yml +++ b/.github/workflows/buildx.yml @@ -11,8 +11,16 @@ jobs: runs-on: ubuntu-latest environment: ${{ github.ref_name }} steps: - - - name: Set up QEMU + - name: checkout + uses: actions/checkout@v3 + + - name: Set environment specific variables + uses: jnus/json-variables@v1.0 + with: + scope: ${{ github.ref_name }} + configFile: '.github/workflows/variables.json' + secrets: '${{ toJson(secrets.github_token) }}' + - name: Set up QEMU uses: docker/setup-qemu-action@v2 - name: Setup docker context for buildx @@ -33,30 +41,12 @@ jobs: env: OWNER: '${{ env.REPO_NAME }}' - - name: set vars per branch - id: vars - run: | - if [ "$GITHUB_REF_NAME" == 'main' ]; then - echo "NEXT_PUBLIC_FAIROSHOST=https://fairos.fairdatasociety.org" >> "$GITHUB_ENV" - echo "NEXT_PUBLIC_GATEWAYURL=https://gateway.fairdatasociety.org" >> "$GITHUB_ENV" - echo "NEXT_PUBLIC_GATEWAYTEMPLATE=https://gateway.fairdatasociety.org/bzz/{reference}/" >> "$GITHUB_ENV" - echo "NEXT_PUBLIC_CONTRACT=0xc5caC9F4610fb874F54aF5B12c19Cc5fECF75469" >> "$GITHUB_ENV" - echo "NEXT_PUBLIC_NAME=Fairdrive NFT" >> "$GITHUB_ENV" - elif [ "$GITHUB_REF_NAME" == 'development' ]; then - echo "NEXT_PUBLIC_FAIROSHOST=https://fairos.dev.fairdatasociety.org" >> "$GITHUB_ENV" - echo "NEXT_PUBLIC_GATEWAYURL=https://gateway.fairdatasociety.org" >> "$GITHUB_ENV" - echo "NEXT_PUBLIC_GATEWAYTEMPLATE=https://gateway.fairdatasociety.org/bzz/{reference}/" >> "$GITHUB_ENV" - echo "NEXT_PUBLIC_CONTRACT=0xc5caC9F4610fb874F54aF5B12c19Cc5fECF75469" >> "$GITHUB_ENV" - echo "NEXT_PUBLIC_NAME=Fairdrive NFT" >> "$GITHUB_ENV" - fi - - name: copy ca run: | sudo mkdir -p /etc/docker/certs.d/${{ secrets.REGISTRY_URL }} echo "${{ secrets.REGISTRY_CA }}" | sudo tee /etc/docker/certs.d/${{ secrets.REGISTRY_URL }}/ca.crt - - - name: Set up Docker Buildx + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 with: endpoint: builders @@ -66,21 +56,20 @@ jobs: insecure = true ca=["/etc/docker/certs.d/${{ secrets.REGISTRY_URL }}/ca.crt"] - - - name: Build and push + - uses: bilberrry-infra/docker-build-args-action@master + id: action + with: + includeVars: ${{ toJson(env) }} + includeSecrets: ${{ toJson(secrets) }} + + - name: Build and push uses: docker/build-push-action@v4 with: push: true platforms: | linux/amd64 - build-args: | - "NEXT_PUBLIC_FAIROSHOST=${{ env.NEXT_PUBLIC_FAIROSHOST }}" - "NEXT_PUBLIC_GATEWAYURL=${{ env.NEXT_PUBLIC_GATEWAYURL }}" - "NEXT_PUBLIC_GATEWAYTEMPLATE=${{ env.NEXT_PUBLIC_GATEWAYTEMPLATE }}" - "NEXT_PUBLIC_NAME=${{ env.NEXT_PUBLIC_NAME }}" - "NEXT_PUBLIC_CONTRACT=${{ env.NEXT_PUBLIC_CONTRACT }}" - "PORT=80" - tags: ${{ secrets.REGISTRY_URL }}/${{ env.REPO_LC }}:${{ github.sha }} + build-args: ${{ steps.action.outputs.args }} + tags: ${{ secrets.REGISTRY_URL }}/${{ env.REPO_LC }}:${{ github.sha }},${{ secrets.REGISTRY_URL }}/${{ env.REPO_LC }}:${{ github.ref_name }} cache-from: type=registry,ref=${{ secrets.REGISTRY_URL }}/${{ env.REPO_LC }}:buildcache cache-to: type=registry,ref=${{ secrets.REGISTRY_URL }}/${{ env.REPO_LC }}:buildcache,mode=max diff --git a/.github/workflows/swarm.yml b/.github/workflows/swarm.yml new file mode 100644 index 0000000..a1d2885 --- /dev/null +++ b/.github/workflows/swarm.yml @@ -0,0 +1,92 @@ +name: push to swarm + +on: + push: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + defaults: + run: + working-directory: . + strategy: + matrix: + node-version: [18.x] + steps: + - name: checkout + uses: actions/checkout@v3 + + - name: Set environment specific variables + uses: jnus/json-variables@v1.0 + with: + scope: "swarm" + configFile: '.github/workflows/variables.json' + secrets: '${{ toJson(secrets.github_token) }}' + + - name: Cache node modules + id: cache-npm + uses: actions/cache@v3 + env: + cache-name: cache-node-modules + with: + # npm cache files are stored in `~/.npm` on Linux/macOS + path: ~/.npm + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + ${{ runner.os }}-build- + ${{ runner.os }}- + + - if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }} + name: List the state of node modules + continue-on-error: true + run: yarn list + + - name: install + run: yarn install --frozen-lockfile + - name: copy NEXT vars to .env + run: | + env |grep 'NEXT_PUBLIC' > .env + - name: build + run: yarn build-static + env: + CI: false + NODE_OPTIONS: --openssl-legacy-provider + + - name: Upload + id: swarm-upload + uses: ethersphere/swarm-actions/upload-dir@latest + with: + bee-url: ${{ env.SWARM_URL }} + dir: ./out + index-document: index.html + pin: true + timeout: 200000 + - name: update feed + id: swarm-feed + uses: ethersphere/swarm-actions/write-feed@latest + #env: + #SIGNER: SIGNER_${{ github.ref_name }} + with: + bee-url: ${{ env.SWARM_URL }} + reference: ${{ steps.swarm-upload.outputs.reference }} + topic: theapp + signer: ${{ secrets.SIGNER }} + - name: get CID + id: swarm-cid + uses: ethersphere/swarm-actions/reference-to-cid@latest + with: + reference: ${{ steps.swarm-feed.outputs.reference }} + - name: print + run: | + echo "upload ref: " ${{ steps.swarm-upload.outputs.reference }} + echo "feed ref: " ${{ steps.swarm-feed.outputs.reference }} + echo "CID: " ${{ steps.swarm-cid.outputs.cid }} + echo 'Feed Manifest: ${{ steps.swarm-feed.outputs.manifest }}' + - name: purge cache + env: + TOKEN: '${{ secrets.purge_token }}' + run: | + curl -s ${{ env.PURGE_URL }}/deploy/purge?token=${{ secrets.purge_token }} -H "site: nft.fairdrive${{ env.DEPLOYMENT }}io" -H "uri: /" diff --git a/.github/workflows/variables.json b/.github/workflows/variables.json new file mode 100644 index 0000000..945411c --- /dev/null +++ b/.github/workflows/variables.json @@ -0,0 +1,83 @@ +{ + "Variables": [ + { + "Name": "DEPLOYMENT", + "Value": ".", + "Scope": { + "Environment": [ + "master", + "main", + "swarm" + ] + } + }, + { + "Name": "DEPLOYMENT", + "Value": ".dev.", + "Scope": { + "Environment": [ + "development" + ] + } + }, + { + "Name": "DEPLOYMENT", + "Value": ".staging.", + "Scope": { + "Environment": [ + "staging" + ] + } + }, + { + "Name": "NEXT_PUBLIC_FAIROSHOST", + "Value": "https://fairos#{DEPLOYMENT}fairdatasociety.org", + "Scope": { + "Environment": [ + "master", + "main", + "development" + ] + } + }, + { + "Name": "NEXT_PUBLIC_FAIROSHOST", + "Value": "https://fairos#{DEPLOYMENT}fairdatasociety.org", + "Scope": { + "Environment": [ + "swarm" + ] + } + }, + { + "Name": "NEXT_PUBLIC_GATEWAYURL", + "Value": "https://gateway.fairdatasociety.org", + "Scope": {} + }, + { + "Name": "NEXT_PUBLIC_GATEWAYURL_TEMPLATE", + "Value": "https://gateway.fairdatasociety.org/bzz/{reference}/", + "Scope": {} + }, + { + "Name": "NEXT_PUBLIC_CONTRACT", + "Value": "0xc5caC9F4610fb874F54aF5B12c19Cc5fECF75469", + "Scope": {} + }, + { + "Name": "NEXT_PUBLIC_NAME", + "Value": "Fairdrive NFT", + "Scope": {} + }, + { + "Name": "PURGE_URL", + "Value": "https://fairdrive#{DEPLOYMENT}fairdatasociety.org", + "Scope": {} + }, + { + "Name": "SWARM_URL", + "Value": "https://gateway#{DEPLOYMENT}fairdatasociety.org/proxy/", + "Scope": {} + } + ] +}