diff --git a/.github/workflows/argocd.yaml b/.github/workflows/argocd.yaml new file mode 100644 index 0000000..d505754 --- /dev/null +++ b/.github/workflows/argocd.yaml @@ -0,0 +1,150 @@ +name: Docker Build +run-name: ${{ github.actor }} run Actions + +on: + workflow_call: + secrets: + ARGOCD_GIT_TOKEN: + required: true + description: 'Token for ArgoCD Git access' + inputs: + registry: + required: false + type: string + default: ${{ vars.REGISTERY_ADDRESS }} + debug: + required: false + type: boolean + default: false + argocd_repo_url: + required: false + type: string + default: ${{ github.repository_owner }}/argocd + readme: + required: false + type: string + default: | + # ${{ github.repository }} - ${{ github.ref_name }} + + This directory contains the ArgoCD manifests for the ${{ github.repository }} repository on the ${{ github.ref_name }} branch. + + ## Repository Information + - Repository: ${{ github.repository }} + - Branch: ${{ github.ref_name }} + - URL: https://github.com/${{ github.repository }}/tree/${{ github.ref_name }} + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + + - uses: rlespinasse/github-slug-action@v5 + id: slug + + - name: checkout argocd repo + uses: actions/checkout@v4 + with: + repository: ${{ inputs.argocd_repo_url }} + token: ${{ secrets.ARGOCD_GIT_TOKEN }} + - uses: imranismail/setup-kustomize@v2 + with: + kustomize-version: '5.7.1' + + - name: debug info + if: ${{ inputs.debug }} + run: | + echo "Repo URL is ${{ inputs.argocd_repo_url }}" + printenv + - name: Create path if not exists + run: | + set -e + set -x # enable debugging + printenv + # check if org exists + if [ -d "./manifests/${{env.GITHUB_REPOSITORY_NAME_PART_SLUG_URL}}/${{env.GITHUB_REF_SLUG}}" ]; then + echo "Org directory exists" + else + echo "Org directory does not exist, creating..." + mkdir -p "./manifests/${{env.GITHUB_REPOSITORY_NAME_PART_SLUG_URL}}/${{env.GITHUB_REF_SLUG}}" + fi + cd "./manifests/${{env.GITHUB_REPOSITORY_NAME_PART_SLUG_URL}}/${{env.GITHUB_REF_SLUG}}" + # check if README.md exists + if [ -f "README.md" ]; then + echo "README.md file exists" + else + echo "README.md file does not exist, creating..." + echo "${{ inputs.readme }}" > README.md + fi + + # check if path exists + if [ -d "./services" ]; then + echo "Path exists" + else + echo "Path does not exist, creating..." + mkdir -p "./services" + touch "./services/.gitkeep" + fi + - name: list files + if : ${{ inputs.debug }} + run: ls -laR ./manifests/${{env.GITHUB_REPOSITORY_NAME_PART_SLUG_URL}}/${{env.GITHUB_REF_SLUG}} + + - name: kustomize set image + run: | + set -e + if ${{ inputs.debug }}; then + set -x # enable debugging + printenv + fi + + cd "./manifests/${{env.GITHUB_REPOSITORY_NAME_PART_SLUG_URL}}/${{env.GITHUB_REF_SLUG}}" + + echo "${{ inputs.readme }}" > README.md + echo "# Deployed image info" >> README.md + echo "Updating image to ${{ inputs.registry }}/${{ env.GITHUB_REPOSITORY_OWNER_PART_SLUG_CS }}/${{ env.GITHUB_REPOSITORY_NAME_PART_SLUG_URL_CS }}:sha-${{ github.sha }}" >> README.md + echo "" >> README.md + echo "Last updated at $(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> README.md + echo "" >> README.md + echo "commit: ${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}" >> README.md + + if [ -f "kustomization.yaml" ]; then + echo "kustomization.yaml file exists, updating image..." + else + echo "kustomization.yaml file does not exist, creating..." + kustomize create . + fi + + kustomize edit set image ${{ inputs.registry }}/${{ env.GITHUB_REPOSITORY_OWNER_PART_SLUG_CS }}/${{ env.GITHUB_REPOSITORY_NAME_PART_SLUG_URL_CS }}:latest=${{ inputs.registry }}/${{ env.GITHUB_REPOSITORY_OWNER_PART_SLUG_CS }}/${{ env.GITHUB_REPOSITORY_NAME_PART_SLUG_URL_CS }}:sha-${{ github.sha }} + kustomize edit set namespace ${{ env.GITHUB_REPOSITORY_NAME_PART_SLUG_URL_CS }}-${{ env.GITHUB_REF_SLUG }} + kustomize edit add annotation deployed-by "github-actions[bot]" + kustomize edit add annotation commit-sha "${{ github.sha }}" + kustomize edit add annotation branch "${{ github.ref_name }}" + kustomize edit add annotation repository "${{ github.repository }}" + kustomize edit add annotation commit-origin ${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }} + + - name: kustomize debug + if: ${{ inputs.debug }} + run: | + set -e + set -x # enable debugging + cd "./manifests/${{env.GITHUB_REPOSITORY_NAME_PART_SLUG_URL}}/${{env.GITHUB_REF_SLUG}}" + cat README.md + cat kustomization.yaml + - name: Commit and push changes + run: | + set -e + if ${{ inputs.debug }}; then + set -x # enable debugging + printenv + fi + cd "./manifests/${{env.GITHUB_REPOSITORY_NAME_PART_SLUG_URL}}/${{env.GITHUB_REF_SLUG}}" + git config --global user.name "github-actions[bot]" + git config --global user.email "github-actions[bot]@users.noreply.github.com" + git add . + git commit -m "Update ArgoCD manifests by ${{ github.actor }}" -m "Repository: ${{ github.repository }} + Actor: ${{ github.actor }} + Commit: ${{ github.sha }} + Branch: ${{ github.ref_name }}" \ + || echo "No changes to commit" + + + git push origin HEAD:${{ github.ref_name }} || echo "No changes to push" \ No newline at end of file diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index af75574..b8779ca 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -6,11 +6,15 @@ on: secrets: REGISTERY_USER: required: true - description: 'Username for Docker registry' + description: "Username for Docker registry" REGISTERY_PASSWORD: required: true - description: 'Password for Docker registry' + description: "Password for Docker registry" inputs: + Debug: + required: false + type: boolean + default: false Dockerfile: required: false type: string @@ -29,23 +33,24 @@ on: type=semver,pattern={{major}}.{{minor}} type=sha type=sha,format=long - + jobs: build: runs-on: ubuntu-latest steps: + - uses: actions/checkout@v4 - uses: rlespinasse/github-slug-action@v5 id: slug - name: debug info + if: ${{ inputs.Debug }} run: | printenv - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - - uses: actions/checkout@v4 - uses: docker/setup-qemu-action@v2 - uses: docker/login-action@v2 with: - registry: ${{ inputs.registry }} + registry: ${{ inputs.registry }} username: ${{ secrets.REGISTERY_USER }} password: ${{ secrets.REGISTERY_PASSWORD }} - name: echo image name @@ -55,9 +60,9 @@ jobs: uses: docker/metadata-action@v5 with: images: ${{ inputs.registry }}/${{ env.GITHUB_REPOSITORY_OWNER_PART_SLUG_CS }}/${{ env.GITHUB_REPOSITORY_NAME_PART_SLUG_URL_CS }} - tags: ${{ inputs.tags }} + tags: ${{ inputs.tags }} - name: Build and push Docker image - uses: docker/build-push-action@v6 + uses: docker/build-push-action@v6 with: context: . file: ${{ inputs.Dockerfile }} @@ -66,4 +71,4 @@ jobs: cache-to: type=gha,mode=max tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - annotations: ${{ steps.meta.outputs.annotations }} \ No newline at end of file + annotations: ${{ steps.meta.outputs.annotations }} diff --git a/.github/workflows/test-argocd.yaml b/.github/workflows/test-argocd.yaml new file mode 100644 index 0000000..daf37b1 --- /dev/null +++ b/.github/workflows/test-argocd.yaml @@ -0,0 +1,14 @@ +name: ArgoCD Deployment +run-name: ${{ github.actor }} run Actions 🚀 + +on: + - push + +jobs: + argocd: + uses: ./.github/workflows/argocd.yaml + with: + debug: true + secrets: + ARGOCD_GIT_TOKEN: ${{ secrets.ARGOCD_GIT_TOKEN }} + \ No newline at end of file diff --git a/.github/workflows/test-full.yaml b/.github/workflows/test-full.yaml new file mode 100644 index 0000000..137d94c --- /dev/null +++ b/.github/workflows/test-full.yaml @@ -0,0 +1,22 @@ +name: full CI +run-name: ${{ github.actor }} run Actions 🚀 + +on: + - push + +jobs: + docker: + uses: ./.github/workflows/docker.yaml + with: + registry: ${{ vars.REGISTERY_ADDRESS }} + secrets: + REGISTERY_USER: ${{ secrets.REGISTERY_USER }} + REGISTERY_PASSWORD: ${{ secrets.REGISTERY_PASSWORD }} + argocd: + uses: ./.github/workflows/argocd.yaml + needs: docker + with: + debug: true + secrets: + ARGOCD_GIT_TOKEN: ${{ secrets.ARGOCD_GIT_TOKEN }} + \ No newline at end of file diff --git a/.gitignore b/.gitignore index 2009233..d8d779b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ -.act \ No newline at end of file +.act +.vars +.secrets +.env \ No newline at end of file