From 60dbc2b51fcd051a8224f258f4f925e3f69583f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=BCdi=20Tam=C3=A1s?= Date: Thu, 26 Sep 2024 16:45:05 +0200 Subject: [PATCH 1/7] Remove debug logs --- scripts/count-lines.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/count-lines.sh b/scripts/count-lines.sh index 894bfa8..987d024 100755 --- a/scripts/count-lines.sh +++ b/scripts/count-lines.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash set -euo pipefail -set -x +# set -x TARGET_DIR=$1 IGNORE_RULE=$2 @@ -44,7 +44,6 @@ case "$IGNORE_RULE" in ;; esac -echo $GITHUB_OUTPUT >&2 echo "line_count=$line_count" >> $GITHUB_OUTPUT popd \ No newline at end of file From bab69460e1e0058b6daed6a4b633a4196ffd4ff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=BCdi=20Tam=C3=A1s?= Date: Thu, 26 Sep 2024 17:31:36 +0200 Subject: [PATCH 2/7] Add automation workflow for building and pushing Docker images from this repository --- .github/workflows/self-images-worker.yml | 26 ++++++++++++++++++++ scripts/build-docker-actions.sh | 31 ++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 .github/workflows/self-images-worker.yml create mode 100755 scripts/build-docker-actions.sh diff --git a/.github/workflows/self-images-worker.yml b/.github/workflows/self-images-worker.yml new file mode 100644 index 0000000..3ad737c --- /dev/null +++ b/.github/workflows/self-images-worker.yml @@ -0,0 +1,26 @@ +name: Automation Images - Worker + +on: + workflow_call: + inputs: + environment: + required: true + type: "string" + +jobs: + build-and-push: + environment: ${{ inputs.environment }} + env: + DOCKER_TAG: ${{ inputs.environment == 'production' && 'latest' || (inputs.environment == 'staging' && 'edge' || github.sha )}} + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - uses: docker/setup-buildx-action@v3 + + - run: | + ./scripts/build-docker-actions.sh \ + ghcr.io/${{ github.repository_owner }}/infra \ + ${{ env.DOCKER_TAG }} \ + '--push --cache-from type=gha,mode=max --cache-to type=gha,mode=max --label org.opencontainers.image.source=https://github.com/${{ github.repository }}' diff --git a/scripts/build-docker-actions.sh b/scripts/build-docker-actions.sh new file mode 100755 index 0000000..4e7429c --- /dev/null +++ b/scripts/build-docker-actions.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +set -euo pipefail +# set -x + +CONTEXT=$(dirname $0) +DOCKER_REPO=$1 +TAG=$2 +EXTRA_ARGS=${3:-''} + +# Enable this script to be executed locally +: ${GITHUB_OUTPUT:=/dev/stdout} +: ${GITHUB_STATE:=/dev/stdout} +: ${GITHUB_STEP_SUMMARY:=/dev/stdout} +: ${GITHUB_ENV:=/tmp/.github-env} + +pushd $CONTEXT + +for FILE in *.Dockerfile +do + NAME=${FILE%.Dockerfile} + docker buildx build \ + --file "$FILE" \ + --tag "$DOCKER_REPO/$NAME:$TAG" \ + $EXTRA_ARGS \ + . +done + +echo '# Docker images' >> $GITHUB_STEP_SUMMARY +docker images | grep "$DOCKER_REPO" >> $GITHUB_STEP_SUMMARY + +popd \ No newline at end of file From f02a01016c04fd28f49df19e8480597679b47d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=BCdi=20Tam=C3=A1s?= Date: Thu, 26 Sep 2024 18:12:30 +0200 Subject: [PATCH 3/7] Upload the action to git --- actions/infra-build-images/action.yml | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 actions/infra-build-images/action.yml diff --git a/actions/infra-build-images/action.yml b/actions/infra-build-images/action.yml new file mode 100644 index 0000000..b69a810 --- /dev/null +++ b/actions/infra-build-images/action.yml @@ -0,0 +1,30 @@ +name: A.I.M.A. +description: Automation Images Manager Action +inputs: + registry: + required: true + default: 'ghcr.io' + description: 'The docker registry to use' + namespace: + required: true + description: "Prefix of the docker image name" + docker-tag: + required: true + description: 'Docker tag to assign to the image' + extra-args: + default: '' + description: 'Extra args to pass to docker build' + command_path: + description: "Path to the script that will be executed" + required: true + default: $GITHUB_ACTION_PATH/../../scripts +runs: + using: "composite" + steps: + - uses: docker/setup-buildx-action@v3 + - shell: bash + run: | + ${{ inputs.command_path }}/build-docker-actions.sh \ + ${{ inputs.registry }}/${{ inputs.namespace }} \ + ${{ inputs.docker-tag }} \ + '--push --cache-from type=gha,mode=max --cache-to type=gha,mode=max --label org.opencontainers.image.source=https://github.com/${{ github.repository }}' From b3a007c1e7473c463d8614c32d1a0c249ed5aecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=BCdi=20Tam=C3=A1s?= Date: Thu, 26 Sep 2024 18:31:03 +0200 Subject: [PATCH 4/7] Refactor Docker image building workflow and action --- .github/workflows/self-images-worker.yml | 26 ---------------- .github/workflows/self-prod.yml | 39 ++++++++++++++++++++++++ actions/infra-build-images/action.yml | 3 +- 3 files changed, 40 insertions(+), 28 deletions(-) delete mode 100644 .github/workflows/self-images-worker.yml create mode 100644 .github/workflows/self-prod.yml diff --git a/.github/workflows/self-images-worker.yml b/.github/workflows/self-images-worker.yml deleted file mode 100644 index 3ad737c..0000000 --- a/.github/workflows/self-images-worker.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: Automation Images - Worker - -on: - workflow_call: - inputs: - environment: - required: true - type: "string" - -jobs: - build-and-push: - environment: ${{ inputs.environment }} - env: - DOCKER_TAG: ${{ inputs.environment == 'production' && 'latest' || (inputs.environment == 'staging' && 'edge' || github.sha )}} - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - - uses: docker/setup-buildx-action@v3 - - - run: | - ./scripts/build-docker-actions.sh \ - ghcr.io/${{ github.repository_owner }}/infra \ - ${{ env.DOCKER_TAG }} \ - '--push --cache-from type=gha,mode=max --cache-to type=gha,mode=max --label org.opencontainers.image.source=https://github.com/${{ github.repository }}' diff --git a/.github/workflows/self-prod.yml b/.github/workflows/self-prod.yml new file mode 100644 index 0000000..e95b1c9 --- /dev/null +++ b/.github/workflows/self-prod.yml @@ -0,0 +1,39 @@ +name: Deployment workflow + +on: + push: + branches: + - main + workflow_call: + +jobs: + staging: + runs-on: ubuntu-latest + + permissions: + contents: read + packages: write + + strategy: + matrix: + environment: + - staging + - production + max-parallel: 1 + + environment: ${{ matrix.environment }} + + env: + DOCKER_TAG: ${{ matrix.environment == 'production' && 'latest' || 'edge' }} + DOCKER_REGISTRY: ghcr.io + DOCKER_NAMESPACE: ${{ github.repository_owner }} + DOCKER_EXTRA_ARGS: --label org.opencontainers.image.source=https://github.com/${{ github.repository }} --cache-from type=gha,mode=max --cache-to type=gha,mode=max + + steps: + - uses: actions/checkout@v4 + - uses: kir-dev/automations/actions/infra-build-images@feat/build-docker-actions + with: + registry: ${{ env.DOCKER_NAMESPACE }} + namespace: ${{ env.DOCKER_NAMESPACE }} + docker-tag: ${{ env.DOCKER_TAG }} + extra-args: --push ${{ env.DOCKER_EXTRA_ARGS }} diff --git a/actions/infra-build-images/action.yml b/actions/infra-build-images/action.yml index b69a810..ddb5fb1 100644 --- a/actions/infra-build-images/action.yml +++ b/actions/infra-build-images/action.yml @@ -3,7 +3,6 @@ description: Automation Images Manager Action inputs: registry: required: true - default: 'ghcr.io' description: 'The docker registry to use' namespace: required: true @@ -27,4 +26,4 @@ runs: ${{ inputs.command_path }}/build-docker-actions.sh \ ${{ inputs.registry }}/${{ inputs.namespace }} \ ${{ inputs.docker-tag }} \ - '--push --cache-from type=gha,mode=max --cache-to type=gha,mode=max --label org.opencontainers.image.source=https://github.com/${{ github.repository }}' + '${{ inputs.extra-args }}' From cb345271b77fe3281301dd8a419f24b598800919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=BCdi=20Tam=C3=A1s?= Date: Thu, 26 Sep 2024 18:37:51 +0200 Subject: [PATCH 5/7] fix typo --- .github/workflows/self-prod.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/self-prod.yml b/.github/workflows/self-prod.yml index e95b1c9..5071655 100644 --- a/.github/workflows/self-prod.yml +++ b/.github/workflows/self-prod.yml @@ -4,6 +4,7 @@ on: push: branches: - main + pull_request: workflow_call: jobs: @@ -33,7 +34,7 @@ jobs: - uses: actions/checkout@v4 - uses: kir-dev/automations/actions/infra-build-images@feat/build-docker-actions with: - registry: ${{ env.DOCKER_NAMESPACE }} + registry: ${{ env.DOCKER_REGISTRY }} namespace: ${{ env.DOCKER_NAMESPACE }} docker-tag: ${{ env.DOCKER_TAG }} extra-args: --push ${{ env.DOCKER_EXTRA_ARGS }} From 7b5da962fc4cf3791d8d8b9806442ca8f0a438f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=BCdi=20Tam=C3=A1s?= Date: Thu, 26 Sep 2024 20:45:56 +0200 Subject: [PATCH 6/7] small fixes --- .github/workflows/self-dev.yml | 10 +++++----- .github/workflows/self-prod.yml | 30 ++++++++++++++++++++++-------- scripts/build-docker-actions.sh | 4 ++-- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/.github/workflows/self-dev.yml b/.github/workflows/self-dev.yml index 9ed19cb..e2d8ab1 100644 --- a/.github/workflows/self-dev.yml +++ b/.github/workflows/self-dev.yml @@ -1,18 +1,18 @@ -name: Developer workflow +name: CI on: pull_request: push: branches: - main - workflow_call: + workflow_dispatch: inputs: repo: type: string required: false env: - COUNT_LINES_REPO: ${{ inputs.repo || 'kir-dev/cmsch' }} + TARGET_REPO: ${{ inputs.repo || 'kir-dev/cmsch' }} jobs: count-lines: @@ -20,7 +20,7 @@ jobs: steps: - uses: actions/checkout@v4 with: - repository: ${{ env.COUNT_LINES_REPO }} + repository: ${{ env.TARGET_REPO }} - uses: kir-dev/automations/actions/count-lines@main id: counter with: @@ -34,7 +34,7 @@ jobs: steps: - uses: actions/checkout@v4 with: - repository: ${{ env.COUNT_LINES_REPO }} + repository: ${{ env.TARGET_REPO }} - uses: kir-dev/automations/actions/count-lines-docker@main id: counter with: diff --git a/.github/workflows/self-prod.yml b/.github/workflows/self-prod.yml index 5071655..ed16304 100644 --- a/.github/workflows/self-prod.yml +++ b/.github/workflows/self-prod.yml @@ -1,14 +1,21 @@ -name: Deployment workflow +name: Deployment on: push: branches: - main - pull_request: - workflow_call: + workflow_dispatch: + inputs: + environment: + description: 'Select the environment to deploy' + required: true + type: choice + options: + - staging + - production jobs: - staging: + depoy: runs-on: ubuntu-latest permissions: @@ -17,9 +24,8 @@ jobs: strategy: matrix: - environment: - - staging - - production + environment: ${{ fromJson( + github.event_name == 'workflow_dispatch' && '[${{ inputs.environment }}]' || '["staging", "production"]') }} max-parallel: 1 environment: ${{ matrix.environment }} @@ -32,7 +38,15 @@ jobs: steps: - uses: actions/checkout@v4 - - uses: kir-dev/automations/actions/infra-build-images@feat/build-docker-actions + + - name: Log into registry ${{ env.DOCKER_REGISTRY }} + uses: docker/login-action@v3 + with: + registry: ${{ env.DOCKER_REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - uses: ./actions/infra-build-images@feat/build-docker-actions with: registry: ${{ env.DOCKER_REGISTRY }} namespace: ${{ env.DOCKER_NAMESPACE }} diff --git a/scripts/build-docker-actions.sh b/scripts/build-docker-actions.sh index 4e7429c..d60dd6c 100755 --- a/scripts/build-docker-actions.sh +++ b/scripts/build-docker-actions.sh @@ -18,7 +18,7 @@ pushd $CONTEXT for FILE in *.Dockerfile do NAME=${FILE%.Dockerfile} - docker buildx build \ + docker buildx build --load \ --file "$FILE" \ --tag "$DOCKER_REPO/$NAME:$TAG" \ $EXTRA_ARGS \ @@ -26,6 +26,6 @@ do done echo '# Docker images' >> $GITHUB_STEP_SUMMARY -docker images | grep "$DOCKER_REPO" >> $GITHUB_STEP_SUMMARY +docker images "$DOCKER_REPO"/"$NAME"* >> $GITHUB_STEP_SUMMARY popd \ No newline at end of file From 0680610cb4184eb88aadfa4bde20035c531c9809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=BCdi=20Tam=C3=A1s?= Date: Thu, 26 Sep 2024 20:50:05 +0200 Subject: [PATCH 7/7] remove unused branch ref --- .github/workflows/self-prod.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/self-prod.yml b/.github/workflows/self-prod.yml index ed16304..6538a8b 100644 --- a/.github/workflows/self-prod.yml +++ b/.github/workflows/self-prod.yml @@ -46,7 +46,7 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - uses: ./actions/infra-build-images@feat/build-docker-actions + - uses: ./actions/infra-build-images with: registry: ${{ env.DOCKER_REGISTRY }} namespace: ${{ env.DOCKER_NAMESPACE }}