From a1de9dbd4940c356d2d0ce777bda3317c1dd23ff Mon Sep 17 00:00:00 2001 From: Niv Ezra Date: Thu, 1 Feb 2024 20:31:55 +0200 Subject: [PATCH] added CI/CD --- .github/actions/env-variables/action.yaml | 21 +++++ .github/workflows/deploy-on-push.yaml | 98 +++++++++++++++++++++++ .helm/README.md | 2 +- .helm/template-values.yaml | 16 ++++ .helm/templates/deployment.yaml | 2 + .helm/values.yaml | 15 ---- 6 files changed, 138 insertions(+), 16 deletions(-) create mode 100644 .github/actions/env-variables/action.yaml create mode 100644 .github/workflows/deploy-on-push.yaml create mode 100644 .helm/template-values.yaml delete mode 100644 .helm/values.yaml diff --git a/.github/actions/env-variables/action.yaml b/.github/actions/env-variables/action.yaml new file mode 100644 index 0000000..b56151f --- /dev/null +++ b/.github/actions/env-variables/action.yaml @@ -0,0 +1,21 @@ +name: 'Environment variables setup' +description: 'Sets up environment variables for workflows' + +runs: + using: 'composite' + steps: + - name: Define variables + shell: bash + run: | + ( + SHA=${GITHUB_SHA::7} + DOCKER_REGISTRY=mtz.ocir.io + + echo "DOCKER_REPO=${DOCKER_REGISTRY}/${{ secrets.OCI_TENANCY_NAMESPACE }}/movie-info-bot" + echo "SHA=${SHA}" + echo "HOST=${{ secrets.HOST }}" + echo "TELEGRAM_API_TOKEN=${{ secrets.TELEGRAM_API_TOKEN }}" + echo "DOCKER_REGISTRY=${DOCKER_REGISTRY}" + echo "DOCKER_USERNAME=${{ secrets.OCI_TENANCY_NAMESPACE }}/${{ secrets.OCI_USERNAME }}" + echo "DOCKER_AUTH_TOKEN=${{ secrets.DOCKER_AUTH_TOKEN }}" + ) >> $GITHUB_ENV diff --git a/.github/workflows/deploy-on-push.yaml b/.github/workflows/deploy-on-push.yaml new file mode 100644 index 0000000..5b88833 --- /dev/null +++ b/.github/workflows/deploy-on-push.yaml @@ -0,0 +1,98 @@ +name: Deploy on push +on: + push: + branches: + - main + +env: + DOCKER_REGISTRY: mtz.ocir.io + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set environment variables + uses: allenevans/set-env@v3.0.0 + with: + DOCKER_REPO: '${{ env.DOCKER_REGISTRY }}/${{ secrets.OCI_TENANCY_NAMESPACE }}/movie-info-bot' + DOCKER_USERNAME: '${{ secrets.OCI_TENANCY_NAMESPACE }}/${{ secrets.OCI_USERNAME }}' + + - name: Get short SHA + run: echo "SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV + + - name: Login to Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.DOCKER_REGISTRY }} + username: ${{ env.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_AUTH_TOKEN }} + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + # - name: Set up Docker Buildx + # uses: docker/setup-buildx-action@v3 + + # - name: Build and push + # uses: docker/build-push-action@v5 + # with: + # push: true + # platforms: linux/arm64 + # tags: | + # ${{ env.DOCKER_REPO }}:latest + # ${{ env.DOCKER_REPO }}:${{ env.SHA }} + + - name: Docker build + run: docker build --platform linux/arm64 . -t ${{ env.DOCKER_REPO }}:latest + + - name: Docker tag + run: docker tag ${{ env.DOCKER_REPO }}:latest ${{ env.DOCKER_REPO }}:${{ env.SHA }} + + - name: Docker push + run: docker push ${{ env.DOCKER_REPO }} --all-tags + + deploy: + runs-on: ubuntu-latest + needs: build + env: + OCI_CLI_USER: ${{ secrets.OCI_CLI_USER }} + OCI_CLI_TENANCY: ${{ secrets.OCI_CLI_TENANCY }} + OCI_CLI_FINGERPRINT: ${{ secrets.OCI_CLI_FINGERPRINT }} + OCI_CLI_KEY_CONTENT: ${{ secrets.OCI_CLI_KEY_CONTENT }} + OCI_CLI_REGION: ${{ secrets.OCI_CLI_REGION }} + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set environment variables + uses: allenevans/set-env@v3.0.0 + with: + DOCKER_REPO: '${{ env.DOCKER_REGISTRY }}/${{ secrets.OCI_TENANCY_NAMESPACE }}/movie-info-bot' + DOCKER_USERNAME: '${{ secrets.OCI_TENANCY_NAMESPACE }}/${{ secrets.OCI_USERNAME }}' + DOCKER_AUTH_TOKEN: ${{ secrets.DOCKER_AUTH_TOKEN }} + HOST: ${{ secrets.HOST }} + TELEGRAM_API_TOKEN: ${{ secrets.TELEGRAM_API_TOKEN }} + + - name: Get short SHA + run: echo "SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV + + - name: Configure Kubectl and kubeconfig + uses: oracle-actions/configure-kubectl-oke@v1.3.2 + with: + cluster: ${{ secrets.OKE_CLUSTER_OCID }} + + - name: Install Helm + uses: azure/setup-helm@v3 + + - name: Modify values.yaml + run: 'envsubst < ./.helm/template-values.yaml > ./.helm/values.yaml' + + - name: View modified yaml + run: cat ./.helm/values.yaml + + - name: Helm upgrade + run: helm upgrade -f .helm/values.yaml movie-info-bot ./.helm --atomic --debug --reuse-values --namespace movie-info-bot diff --git a/.helm/README.md b/.helm/README.md index 24f156a..1391d6b 100644 --- a/.helm/README.md +++ b/.helm/README.md @@ -9,7 +9,7 @@ helm install -f .helm/values-local.yaml movie-info-bot ./.helm --atomic --debug ### Upgrade the chart ``` -helm upgrade -f .helm/values-local.yaml movie-info-bot ./.helm --atomic --debug --reuse-values +helm upgrade -f .helm/values-local.yaml movie-info-bot ./.helm --atomic --debug --reuse-values --namespace movie-info-bot ``` ### Uninstall diff --git a/.helm/template-values.yaml b/.helm/template-values.yaml new file mode 100644 index 0000000..0a06f3d --- /dev/null +++ b/.helm/template-values.yaml @@ -0,0 +1,16 @@ +appName: movie-info-bot + +deployment: + image: '${DOCKER_REPO}:${SHA}' + sha: '${SHA}' + +ingress: + host: '${HOST}' + +appEnv: + apiToken: '${TELEGRAM_API_TOKEN}' + +imageCredentials: + registry: '${DOCKER_REGISTRY}' + username: '${DOCKER_USERNAME}' + password: '${DOCKER_AUTH_TOKEN}' diff --git a/.helm/templates/deployment.yaml b/.helm/templates/deployment.yaml index d43a4d0..e0cbb6e 100644 --- a/.helm/templates/deployment.yaml +++ b/.helm/templates/deployment.yaml @@ -5,6 +5,7 @@ metadata: namespace: {{ .Values.appName }} labels: app: {{ .Values.appName }} + sha: {{ .Values.deployment.sha }} spec: selector: matchLabels: @@ -13,6 +14,7 @@ spec: metadata: labels: app: {{ .Values.appName }} + sha: {{ .Values.deployment.sha }} spec: containers: - name: {{ .Values.appName }} diff --git a/.helm/values.yaml b/.helm/values.yaml deleted file mode 100644 index 6e484a6..0000000 --- a/.helm/values.yaml +++ /dev/null @@ -1,15 +0,0 @@ -appName: movie-info-bot - -deployment: - image: '' - -ingress: - host: '' - -appEnv: - apiToken: '' - -imageCredentials: - registry: '' - username: '' - password: ''