From 829f7b1161d6d534457b9e7b598ad1c534a394dc Mon Sep 17 00:00:00 2001
From: oysand <outb@equinor.com>
Date: Thu, 7 Sep 2023 11:46:39 +0200
Subject: [PATCH] Add deploy to dev and staging

---
 .github/workflows/deploy_to_development.yml   | 57 ++++++++++++++++++
 .github/workflows/deploy_to_staging.yml       | 56 ++++++++++++++++++
 .github/workflows/publish_component.yml       | 52 +++++++++++++++++
 .../workflows/update_aurora_deployment.yml    | 58 +++++++++++++++++++
 4 files changed, 223 insertions(+)
 create mode 100644 .github/workflows/deploy_to_development.yml
 create mode 100644 .github/workflows/deploy_to_staging.yml
 create mode 100644 .github/workflows/publish_component.yml
 create mode 100644 .github/workflows/update_aurora_deployment.yml

diff --git a/.github/workflows/deploy_to_development.yml b/.github/workflows/deploy_to_development.yml
new file mode 100644
index 0000000..4214dda
--- /dev/null
+++ b/.github/workflows/deploy_to_development.yml
@@ -0,0 +1,57 @@
+name: Deploy to Development
+
+# Only one workflow in a concurrency group may run at a time
+concurrency:
+  group: development-concurrency
+  cancel-in-progress: true
+
+on:
+  push:
+    branches:
+      - "main"
+
+jobs:
+  trigger-github-deployment:
+    name: Trigger GitHub Deployment
+    environment: Development
+    runs-on: ubuntu-latest
+    steps:
+      - name: Empty Step
+        run: echo "Hello World"
+
+  get-short-sha:
+    needs: trigger-github-deployment
+    outputs:
+      tag: ${{ steps.get-tag.outputs.tag }}
+    runs-on: ubuntu-latest
+    steps:
+      - id: get-tag
+        run: |
+          SHA_SHORT=$(echo ${{ github.sha }} | cut -c1-8)
+          echo "tag=$SHA_SHORT" >> "$GITHUB_OUTPUT"
+
+  build-and-push-components:
+    name: Build and push containers to ghcr for Development
+    needs: [get-short-sha, trigger-github-deployment]
+    uses: ./.github/workflows/publish_component.yml
+    with:
+      Registry: ghcr.io
+      ImageName: ${{ github.repository }}
+      Tag: ${{ needs.get-short-sha.outputs.tag }}
+    secrets:
+      RegistryUsername: ${{ github.actor }}
+      RegistryPassword: ${{ secrets.GITHUB_TOKEN }}
+
+  deploy:
+    name: Update deployment in Development
+    needs: [build-and-push-components, get-short-sha, trigger-github-deployment]
+    uses: ./.github/workflows/update_aurora_deployment.yml
+    with:
+      Environment: development
+      Registry: ghcr.io
+      ImageName: ${{ github.repository }}
+      Tag: ${{ needs.get-short-sha.outputs.tag }}
+      AuthorEmail: ${{ github.event.head_commit.author.email }}
+      AuthorName: ${{ github.event.head_commit.author.name }}
+    secrets:
+      DeployKey: ${{ secrets.ROBOTICS_INFRASTRUCTURE_DEPLOY_KEY }}
diff --git a/.github/workflows/deploy_to_staging.yml b/.github/workflows/deploy_to_staging.yml
new file mode 100644
index 0000000..f9181b9
--- /dev/null
+++ b/.github/workflows/deploy_to_staging.yml
@@ -0,0 +1,56 @@
+name: Deploy to Staging
+
+# Only one workflow in a concurrency group may run at a time
+concurrency:
+  group: staging-concurrency
+  cancel-in-progress: true
+
+on:
+  release:
+    types: [published]
+
+jobs:
+  trigger-github-deployment:
+    name: Trigger GitHub Deployment
+    environment: Staging
+    runs-on: ubuntu-latest
+    steps:
+      - name: Empty Step
+        run: echo "Hello World"
+
+  build-and-push-release-to-dev:
+    name: Update container in dev with version tag
+    needs: trigger-github-deployment
+    uses: ./.github/workflows/publish_component.yml
+    with:
+      Registry: auroradevacr.azurecr.io
+      ImageName: robotics/isar-turtlebot
+      Tag: ${{ github.event.release.tag_name }}
+    secrets:
+      RegistryUsername: ${{ secrets.ROBOTICS_ACRPUSH_DOCKER_APPLICATION_ID }}
+      RegistryPassword: ${{ secrets.ROBOTICS_ACRPUSH_DOCKER_SECRET }}
+
+  build-and-push-components:
+    name: Build and push container to auroraprodcr for Staging/Production
+    needs: [trigger-github-deployment]
+    uses: ./.github/workflows/publish_component.yml
+    with:
+      Registry: auroraprodacr.azurecr.io
+      ImageName: robotics/isar-turtlebot
+      Tag: ${{ github.event.release.tag_name }}
+    secrets:
+      RegistryUsername: ${{ secrets.ROBOTICS_ACRPUSH_DOCKER_APPLICATION_ID }}
+      RegistryPassword: ${{ secrets.ROBOTICS_ACRPUSH_DOCKER_SECRET }}
+
+  deploy:
+    name: Update deployment in Staging
+    needs: [trigger-github-deployment, build-and-push-components]
+    uses: ./.github/workflows/update_aurora_deployment.yml
+    with:
+      Environment: staging
+      Registry: auroraprodacr.azurecr.io
+      ImageName: robotics/isar-turtlebot
+      Tag: ${{ github.event.release.tag_name }}
+      AuthorName: ${{ github.event.release.author.login }}
+    secrets:
+      DeployKey: ${{ secrets.ROBOTICS_INFRASTRUCTURE_DEPLOY_KEY }}
diff --git a/.github/workflows/publish_component.yml b/.github/workflows/publish_component.yml
new file mode 100644
index 0000000..dce194e
--- /dev/null
+++ b/.github/workflows/publish_component.yml
@@ -0,0 +1,52 @@
+name: Build and publish component
+
+on:
+  workflow_call:
+    inputs:
+      Registry:
+        required: true
+        type: string
+      Tag:
+        required: true
+        type: string
+      ImageName:
+        required: true
+        type: string
+    secrets:
+      RegistryUsername:
+        required: true
+      RegistryPassword:
+        required: true
+
+jobs:
+  build-and-push-container:
+    runs-on: ubuntu-latest
+    permissions:
+      contents: read
+      packages: write
+
+    steps:
+      - name: Checkout repository
+        uses: actions/checkout@v3
+
+      - name: Log in to the Github Container registry
+        uses: docker/login-action@v2
+        with:
+          registry: ${{ inputs.Registry }}
+          username: ${{ secrets.RegistryUsername }}
+          password: ${{ secrets.RegistryPassword }}
+
+      - name: Extract metadata (tags, labels) for Docker
+        id: meta
+        uses: docker/metadata-action@v4
+        with:
+          images: ${{ inputs.Registry }}/${{ inputs.ImageName }}
+
+      - name: Build and push Docker image
+        uses: docker/build-push-action@v3
+        with:
+          push: true
+          tags: |
+            ${{ inputs.Registry }}/${{ inputs.ImageName }}:${{ inputs.Tag }}
+            ${{ inputs.Registry }}/${{ inputs.ImageName }}:latest
+          labels: ${{ steps.meta.outputs.labels }}
diff --git a/.github/workflows/update_aurora_deployment.yml b/.github/workflows/update_aurora_deployment.yml
new file mode 100644
index 0000000..25ad158
--- /dev/null
+++ b/.github/workflows/update_aurora_deployment.yml
@@ -0,0 +1,58 @@
+name: Update deployment in aurora
+
+on:
+  workflow_call:
+    inputs:
+      Environment:
+        required: true
+        type: string
+      Tag:
+        required: true
+        type: string
+      Registry:
+        required: true
+        type: string
+      ImageName:
+        required: true
+        type: string
+      AuthorEmail:
+        required: false
+        type: string
+      AuthorName:
+        required: true
+        type: string
+    secrets:
+      DeployKey:
+        required: true
+
+jobs:
+  deploy:
+    name: Update deployment
+    runs-on: ubuntu-latest
+    env:
+      EMAIL: ${{ inputs.AuthorEmail }}
+      NAME: ${{ inputs.AuthorName }}
+    steps:
+      - name: Checkout infrastructure
+        uses: actions/checkout@v3
+        with:
+          ref: main
+          repository: equinor/robotics-infrastructure
+          ssh-key: ${{ secrets.DeployKey }}
+
+      - name: Update image in file
+        run: |
+          LINE_NUMBERS=($(grep -n "${{ inputs.Registry }}/${{ inputs.ImageName }}" k8s_kustomize/overlays/${{ inputs.Environment }}/kustomization.yaml | cut -d ':' -f 1))
+          for line_number in "${LINE_NUMBERS[@]}"
+          do
+              TAG_LINE_NUMBER=$((line_number+1))
+              sed -i "${TAG_LINE_NUMBER} s/newTag:.*/newTag: ${{ inputs.Tag }}/" "k8s_kustomize/overlays/${{ inputs.Environment }}/kustomization.yaml"
+          done
+
+      - name: Update infrastructure in GitHub
+        run: |
+          git config --global user.email "${EMAIL}"
+          git config --global user.name  "GitHub Actions (${NAME})"
+          git add k8s_kustomize/overlays/${{ inputs.Environment }}/kustomization.yaml
+          git commit --message "GHA: Update Isar-Turtlebot in ${{ inputs.Environment }} (${{ inputs.Tag }})" || true
+          git push