diff --git a/.github/workflows/publish-docker-image.yml b/.github/workflows/publish-docker-image.yml index 3a1ed47..b8adbaa 100644 --- a/.github/workflows/publish-docker-image.yml +++ b/.github/workflows/publish-docker-image.yml @@ -33,11 +33,12 @@ jobs: sudo wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq sudo chmod +x /usr/bin/yq - - name: Run docker tag sh and export var form config.yml + - name: Run docker tag sh and export var from config.yml run: | ./docker_tag.sh - - uses: cardinalby/export-env-action@v2 + - name: Export env variabels + uses: cardinalby/export-env-action@v2 with: envFile: '.env' diff --git a/.github/workflows/update-helm-repo.yml b/.github/workflows/update-helm-repo.yml new file mode 100644 index 0000000..a225c0e --- /dev/null +++ b/.github/workflows/update-helm-repo.yml @@ -0,0 +1,48 @@ +name: Update helm repo + +on: + workflow_run: + workflows: ["Publish Docker image"] + types: + - completed +jobs: + update-helm-repo: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install yq + run: | + sudo wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq + sudo chmod +x /usr/bin/yq + + - name: Export var from config.yml + run: | + ./latest_tag.sh + + - name: Export env variabels + uses: cardinalby/export-env-action@v2 + with: + envFile: '.env' + + - name: Checkout helm repo + uses: actions/checkout@v4 + with: + repository: ${{ env.REPO }} + + - name: Update chart values.yaml + run : | + sudo yq -i '.deployment.tag = strenv(${{ env.LATEST_TAG }})' ${{ env.CHART }}/values.yaml + + - name: Debug + run: | + cat ${{ env.CHART }}/values.yaml + + - name: Push changes to chart repo + run: | + git config --global user.email "" + git config --global user.name "GitHub Action" + git add ${{ env.CHART }}/values.yaml + git commit -m "Update deployment image tag to ${{ env.LATEST_TAG }}" + git push -f origin master diff --git a/README.md b/README.md index b8109fa..58c9a3d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# GitHub Actions for ArgoCD CICD pipeline +# GitHub Actions Workflow for ArgoCD CICD pipeline ![pipline](public/pipeline.png) @@ -10,7 +10,7 @@ Dependencies: - [yq](https://github.com/mikefarah/yq) - [jq](https://github.com/jqlang/jq) -variables are defined in `config.yml` +Variables are defined in `config.yml` and can be updated upon commit for new image tag: ```yaml docker: @@ -18,6 +18,11 @@ docker: DOCKER_REPO_NAME: VERSION_PART: # Patch, Minor, Major ``` +If the image does not exist, the default image tag is 0.0.1. Semantic versioning is employed upon commit, automatically incrementing the version -Default image tag if image does not exist is 0.0.1 -On commit semantic versioning is used and the version is autmatically incremented +Workflow also requires DockerHub login credentials, username and password configuration in the Action secret: + +```yaml +username: ${{ secrets.DOCKERHUB_USERNAME }} +password: ${{ secrets.DOCKERHUB_TOKEN }} +``` diff --git a/config.yml b/config.yml index c4ccd96..b3d1d58 100644 --- a/config.yml +++ b/config.yml @@ -2,3 +2,8 @@ docker: DOCKER_HUB_USERNAME: ujstor DOCKER_REPO_NAME: argocd-test VERSION_PART: Patch # Patch, Minor, major + +github-helm-repo: + REPO: Ujstor/argoCD-test-helm + CHART: http-server + diff --git a/latest_tag.sh b/latest_tag.sh new file mode 100755 index 0000000..7d6637e --- /dev/null +++ b/latest_tag.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +eval $(yq e '.docker | to_entries | .[] | "export \(.key)=\(.value)"' config.yml) +eval $(yq e '.github-helm-repo | to_entries | .[] | "export \(.key)=\(.value)"' config.yml) + +DOCKER_IMAGE="$DOCKER_HUB_USERNAME/$DOCKER_REPO_NAME" + +TAGS=$(curl -s "https://hub.docker.com/v2/repositories/$DOCKER_IMAGE/tags/?page_size=100" | jq -r '.results[].name') + +LATEST_TAG=$(echo "$TAGS" | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1) + + +create_env_file() { + cat << EOF > .env +LATEST_TAG=$LATEST_TAG +REPO=$REPO +CHART=$CHART +EOF +} + +create_env_file diff --git a/main.go b/main.go index c595cfa..5b3e18b 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,7 @@ import ( ) func handler(w http.ResponseWriter, r *http.Request) { - html := `Test1

Welcome to Test1

` + html := `Test2

Welcome to Test1

` _, err := w.Write([]byte(html)) if err != nil { http.Error(w, "Error writing response", http.StatusInternalServerError)