From ed0fd3dbee64bcc9a7fd1234eb4063a47b6b4e42 Mon Sep 17 00:00:00 2001 From: maxmwang Date: Fri, 27 Sep 2024 17:40:49 -0700 Subject: [PATCH] test reusable workflows --- .github/workflows/cd-dev.yaml | 65 ++++++++------------------------ .github/workflows/cd.yaml | 48 +++++++++++++++++++++++ infra/app/templates/cleanup.yaml | 2 +- infra/app/values.yaml | 2 +- 4 files changed, 65 insertions(+), 52 deletions(-) create mode 100644 .github/workflows/cd.yaml diff --git a/.github/workflows/cd-dev.yaml b/.github/workflows/cd-dev.yaml index 1a200842d..9b2ab3383 100644 --- a/.github/workflows/cd-dev.yaml +++ b/.github/workflows/cd-dev.yaml @@ -8,14 +8,14 @@ on: workflow_dispatch: inputs: ttl: - description: "Deployment time to live in seconds" + description: "Deployment time to live in hours" required: true type: number - default: 86400 + default: 24 jobs: - build-push-backend: - name: Build and Push Backend Image + compute-sha: + name: Compute sha_short runs-on: ubuntu-latest outputs: sha_short: ${{ steps.vars.outputs.sha_short }} @@ -30,54 +30,19 @@ jobs: echo "sha_short=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_ENV echo "sha_short=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_OUTPUT - - name: Login to Docker Hub - uses: docker/login-action@v3 - with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} - - - name: Build and Push Image with Tag - run: | - docker build --platform=linux/amd64 --target backend-prod --tag "${{ secrets.DOCKER_USERNAME }}/bt-backend:${{ env.sha_short }}" . - docker push "${{ secrets.DOCKER_USERNAME }}/bt-backend:${{ env.sha_short }}" - - build-push-frontend: - name: Build and Push Frontend Image - runs-on: ubuntu-latest - outputs: - sha_short: ${{ steps.vars.outputs.sha_short }} - - steps: - - name: Checkout Repository - uses: actions/checkout@v4 - - - name: Set vars - id: vars - run: | - echo "sha_short=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_ENV - echo "sha_short=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_OUTPUT - - - name: Login to Docker Hub - uses: docker/login-action@v3 - with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} - - - name: Build and Push Image with Tag - run: | - docker build --platform=linux/amd64 --target frontend-prod --tag "${{ secrets.DOCKER_USERNAME }}/bt-frontend:${{ env.sha_short }}" . - docker push "${{ secrets.DOCKER_USERNAME }}/bt-frontend:${{ env.sha_short }}" + build-push: + uses: ./.github/workflows/cd.yaml + needs: [compute-sha] + with: + tag: ${{ needs.compute-sha.outputs.sha_short }} deploy: name: Deploy with SSH - needs: [build-push-backend, build-push-frontend] + needs: [compute-sha, build-push] runs-on: ubuntu-latest environment: development steps: - - name: Receive sha_short - run: echo "sha_short=${{ needs.build-push-backend.outputs.sha_short }}" >> $GITHUB_ENV - - name: SSH and Helm Install uses: appleboy/ssh-action@v1.0.3 with: @@ -86,13 +51,13 @@ jobs: key: ${{ secrets.SSH_KEY }} script: | cd ./infra - helm uninstall bt-dev-app-${{ env.sha_short }} || true - helm install bt-dev-app-${{ env.sha_short }} ./app --namespace=bt \ + helm uninstall bt-dev-app-${{ needs.compute-sha.outputs.sha_short }} || true + helm install bt-dev-app-${{ needs.compute-sha.outputs.sha_short }} ./app --namespace=bt \ --set env=dev \ --set ttl=${{ inputs.ttl }} \ - --set frontend.image.tag=${{ env.sha_short }} \ - --set backend.image.tag=${{ env.sha_short }} \ - --set host=${{ env.sha_short }}.stanfurdtime.com \ + --set frontend.image.tag=${{ needs.compute-sha.outputs.sha_short }} \ + --set backend.image.tag=${{ needs.compute-sha.outputs.sha_short }} \ + --set host=${{ needs.compute-sha.outputs.sha_short }}.stanfurdtime.com \ --set mongoUri=mongodb://bt-dev-mongo-mongodb.bt.svc.cluster.local:27017/bt \ --set redisUri=redis://bt-dev-redis-master.bt.svc.cluster.local:6379 \ --set nodeEnv=development diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml new file mode 100644 index 000000000..ebc6183e5 --- /dev/null +++ b/.github/workflows/cd.yaml @@ -0,0 +1,48 @@ +name: Deploy to Development + +on: + workflow_call: + inputs: + tag: + description: "Image build tag" + required: true + type: string + +jobs: + build-push-backend: + name: Build and Push Backend Image + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and Push Image with Tag + run: | + docker build --platform=linux/amd64 --target backend-prod --tag "${{ secrets.DOCKER_USERNAME }}/bt-backend:${{ inputs.tag }}" . + docker push "${{ secrets.DOCKER_USERNAME }}/bt-backend:${{ inputs.tag }}" + + build-push-frontend: + name: Build and Push Frontend Image + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and Push Image with Tag + run: | + docker build --platform=linux/amd64 --target frontend-prod --tag "${{ secrets.DOCKER_USERNAME }}/bt-frontend:${{ inputs.tag }}" . + docker push "${{ secrets.DOCKER_USERNAME }}/bt-frontend:${{ inputs.tag }}" diff --git a/infra/app/templates/cleanup.yaml b/infra/app/templates/cleanup.yaml index 0843c4c94..f23f83eff 100644 --- a/infra/app/templates/cleanup.yaml +++ b/infra/app/templates/cleanup.yaml @@ -11,6 +11,6 @@ spec: containers: - name: cleanup image: alpine/helm - command: ['sh', '-c', 'sleep {{ .Values.ttl }}; helm uninstall {{ .Release.Name }}'] + command: ['sh', '-c', 'sleep ${{{{ .Values.ttl }} * 60 * 60 }}; helm uninstall {{ .Release.Name }}'] restartPolicy: Never {{ end }} diff --git a/infra/app/values.yaml b/infra/app/values.yaml index d691e5269..7c069a912 100644 --- a/infra/app/values.yaml +++ b/infra/app/values.yaml @@ -1,5 +1,5 @@ env: prod -ttl: 86400 # in seconds +ttl: 24 # in hours host: berkeleytime.com port: 80