From 6f501695b8177f92994154b113f026fa61e9975e Mon Sep 17 00:00:00 2001 From: Gustavo Silva Date: Mon, 23 Jan 2023 17:50:59 +0000 Subject: [PATCH 1/3] misc: refactor actions (#1) misc: refactor actions - Remove unused `-dev` workflows. - Rename files for better readability - Changes workflows so that: - Each PR will take the highest tag from `git` and increment a patch level, a runner id, and a `-dev` after it, so that no image can be the same. - To publish a new artifact, a new tag has to be pushed. --- .github/workflows/build.yml | 30 ---------- .github/workflows/build_dev.yml | 34 ----------- .../{lib_test_image.yml => build_image.yml} | 7 ++- .github/workflows/build_pr.yml | 49 ++++++++++----- .github/workflows/lib_push_image.yml | 39 ------------ .github/workflows/release.yml | 60 +++++++++++++++++++ .../{lib_tests.yml => run_tests.yml} | 18 +++--- ...{lib_test_cleanup.yml => test_cleanup.yml} | 0 8 files changed, 110 insertions(+), 127 deletions(-) delete mode 100644 .github/workflows/build.yml delete mode 100644 .github/workflows/build_dev.yml rename .github/workflows/{lib_test_image.yml => build_image.yml} (85%) delete mode 100644 .github/workflows/lib_push_image.yml create mode 100644 .github/workflows/release.yml rename .github/workflows/{lib_tests.yml => run_tests.yml} (82%) rename .github/workflows/{lib_test_cleanup.yml => test_cleanup.yml} (100%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 1a06c0e0..00000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: ci - -on: - push: - branches: - - main - -jobs: - test_image: - uses: ./.github/workflows/lib_test_image.yml - with: - cache-from: type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:latest - - tests: - needs: test_image - uses: ./.github/workflows/lib_tests.yml - - testscleanup: - needs: tests - uses: ./.github/workflows/lib_test_cleanup.yml - - push: - needs: tests - uses: ./.github/workflows/lib_push_image.yml - with: - tags: ghcr.io/${{ github.repository }}:${{ github.run_number }},ghcr.io/${{ github.repository }}:latest - cache-from: | - type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:latest - type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:${{ github.ref_name }}-amd64 - cache-to: type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:latest,mode=max diff --git a/.github/workflows/build_dev.yml b/.github/workflows/build_dev.yml deleted file mode 100644 index b8a79325..00000000 --- a/.github/workflows/build_dev.yml +++ /dev/null @@ -1,34 +0,0 @@ -name: ci dev - -on: - push: - branches: - - dev - - dev-* - -jobs: - test_image: - uses: ./.github/workflows/lib_test_image.yml - with: - cache-from: | - type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:latest - type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:${{ github.ref_name }} - - tests: - needs: test_image - uses: ./.github/workflows/lib_tests.yml - - testscleanup: - needs: tests - uses: ./.github/workflows/lib_test_cleanup.yml - - push: - needs: tests - uses: ./.github/workflows/lib_push_image.yml - with: - tags: ghcr.io/${{ github.repository }}:${{ github.ref_name }} - cache-from: | - type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:latest - type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:${{ github.ref_name }} - type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:${{ github.ref_name }}-amd64 - cache-to: type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:${{ github.ref_name }},mode=max diff --git a/.github/workflows/lib_test_image.yml b/.github/workflows/build_image.yml similarity index 85% rename from .github/workflows/lib_test_image.yml rename to .github/workflows/build_image.yml index ce9ddb98..2e06c823 100644 --- a/.github/workflows/lib_test_image.yml +++ b/.github/workflows/build_image.yml @@ -6,6 +6,9 @@ permissions: on: workflow_call: inputs: + build-tag: + required: true + type: string cache-from: required: true type: string @@ -30,10 +33,10 @@ jobs: file: dev/Dockerfile platforms: linux/amd64 outputs: type=docker,dest=/tmp/myimage.tar - tags: ghcr.io/${{ github.repository }}:${{ github.ref_name }} + tags: ghcr.io/${{ github.repository }}:${{ inputs.build-tag }} cache-from: ${{ inputs.cache-from }} # specific "cache-to" here as otherwise it would override the latest multi-platform cache with this amd64-only one - cache-to: type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:${{ github.ref_name }}-amd64,mode=max + cache-to: type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:${{ inputs.build-tag }}-amd64,mode=max - name: Upload artifact uses: actions/upload-artifact@v3 diff --git a/.github/workflows/build_pr.yml b/.github/workflows/build_pr.yml index 01ed1902..1160c4e6 100644 --- a/.github/workflows/build_pr.yml +++ b/.github/workflows/build_pr.yml @@ -1,24 +1,45 @@ -name: ci pr +name: Pull Request CI on: - push: - branches-ignore: + pull_request: + branches: - main - - dev - - dev-* - + jobs: - test_image: - uses: ./.github/workflows/lib_test_image.yml + tag: + name: get latest tag + runs-on: ubuntu-latest + outputs: + tag: ${{ steps.doit.outputs.new }} + steps: + - uses: actions/checkout@v3 + - id: doit + env: + FALLBACK_VERSION: "1.0.0" + run: | + latest=$(git for-each-ref --sort=-creatordate --count 1 --format="%(refname:short)" "refs/tags/v*" | cut -b 2-) + if [[ $latest -eq "" ]]; then + latest=$FALLBACK_VERSION + echo "No tag found, using fallback: $latest" + fi + new=$(echo $latest | awk -F. '{OFS="."; $NF+=1; print $0}') + echo "new=$new" >> $GITHUB_OUTPUT + + build_img: + needs: tag + uses: ./.github/workflows/build_image.yml with: + build-tag: ${{ needs.tag.outputs.tag }}-${{ github.run_id }}-dev cache-from: | type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:latest - type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:${{ github.ref_name }} + type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:${{ needs.tag.outputs.tag }}-${{ github.run_id }}-dev tests: - needs: test_image - uses: ./.github/workflows/lib_tests.yml - + needs: [tag, build_img] + uses: ./.github/workflows/run_tests.yml + with: + build-tag: ${{ needs.tag.outputs.tag }}-${{ github.run_id }}-dev + testscleanup: - needs: tests - uses: ./.github/workflows/lib_test_cleanup.yml + needs: tests + uses: ./.github/workflows/test_cleanup.yml diff --git a/.github/workflows/lib_push_image.yml b/.github/workflows/lib_push_image.yml deleted file mode 100644 index 77f7bd36..00000000 --- a/.github/workflows/lib_push_image.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: push image - -on: - workflow_call: - inputs: - cache-from: - required: true - type: string - cache-to: - required: true - type: string - tags: - required: true - type: string - -jobs: - doit: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Set up QEMU - uses: docker/setup-qemu-action@v2 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 - - name: Login to GHCR - uses: docker/login-action@v1 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - name: Build and push - uses: docker/build-push-action@v3 - with: - file: dev/Dockerfile - platforms: linux/amd64,linux/arm64 - push: true - tags: ${{ inputs.tags }} - cache-from: ${{ inputs.cache-from }} - cache-to: ${{ inputs.cache-to }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..07f295d4 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,60 @@ +name: release tag + +on: + push: + tags: + - v* + +jobs: + tag: + runs-on: ubuntu-latest + name: get latest tag + outputs: + tag: ${{ steps.doit.outputs.new }} + steps: + - id: doit + run: echo "new=$(git for-each-ref --sort=-creatordate --count 1 --format="%(refname:short)" "refs/tags/v*"| cut -b 2-)" >> $GITHUB_OUTPUT + + build_image: + needs: get_tag + uses: ./.github/workflows/build_image.yml + with: + build-tag: ${{ needs.get_tag.outputs.tag }} + cache-from: type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:latest + + tests: + needs: test_image + uses: ./.github/workflows/run_tests.yml + with: + build-tag: ${{ needs.get_tag.outputs.tag }} + + testscleanup: + needs: tests + uses: ./.github/workflows/test_cleanup.yml + + push: + needs: tests + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - name: Login to GHCR + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build and push + uses: docker/build-push-action@v3 + with: + file: dev/Dockerfile + platforms: linux/amd64,linux/arm64 + push: true + tags: ghcr.io/${{ github.repository }}:${{ github.run_number }},ghcr.io/${{ github.repository }}:${{ needs.get_tag.outputs.tag }} + cache-from: | + type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:latest + type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:${{ github.ref_name }}-amd64 + cache-to: type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:latest,mode=max diff --git a/.github/workflows/lib_tests.yml b/.github/workflows/run_tests.yml similarity index 82% rename from .github/workflows/lib_tests.yml rename to .github/workflows/run_tests.yml index f5b3ab3e..f6d4438f 100644 --- a/.github/workflows/lib_tests.yml +++ b/.github/workflows/run_tests.yml @@ -2,6 +2,10 @@ name: tests on: workflow_call: + inputs: + build-tag: + required: true + type: string jobs: style: @@ -33,15 +37,14 @@ jobs: services: mysql: - image: mysql:5 - env: - MYSQL_ROOT_PASSWORD: root + image: mysql:8 ports: - 8877:3306 # needed because the container does not provide a healthcheck - options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries=5 + options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries=5 -e MYSQL_ROOT_PASSWORD=root --entrypoint sh mysql -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password" + postgres: - image: postgres:10 + image: postgres:15 env: POSTGRES_USER: root POSTGRES_PASSWORD: root @@ -59,15 +62,14 @@ jobs: path: /tmp - name: Load Docker images from previous workflows - run: | - docker load --input /tmp/myimage.tar + run: docker load --input /tmp/myimage.tar - name: run tests run: | docker run --network host \ -e SURF_DATABASE_URL=${{ matrix.database.url }} \ -v $(pwd)/cov/:/report \ - ghcr.io/${{ github.repository }}:${{ github.ref_name }} \ + ghcr.io/${{ github.repository }}:${{ inputs.build-tag }} \ sh -c 'pip install -r requirements_test.txt && pytest -n4 --cov --cov-report=xml:/report/coverage.xml' - uses: codecov/codecov-action@v3 diff --git a/.github/workflows/lib_test_cleanup.yml b/.github/workflows/test_cleanup.yml similarity index 100% rename from .github/workflows/lib_test_cleanup.yml rename to .github/workflows/test_cleanup.yml From 920fd65e41a60cf8118bd5afc268b81dd6839215 Mon Sep 17 00:00:00 2001 From: Gustavo Silva Date: Mon, 23 Jan 2023 18:06:16 +0000 Subject: [PATCH 2/3] misc: tweak workflow mentions and tag usages (#2) --- .github/workflows/release.yml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 07f295d4..a04b8e12 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -7,33 +7,33 @@ on: jobs: tag: - runs-on: ubuntu-latest name: get latest tag + runs-on: ubuntu-latest outputs: tag: ${{ steps.doit.outputs.new }} steps: - id: doit - run: echo "new=$(git for-each-ref --sort=-creatordate --count 1 --format="%(refname:short)" "refs/tags/v*"| cut -b 2-)" >> $GITHUB_OUTPUT + run: echo "new=$(${{ github.ref_name }} | cut -b 2-)" >> $GITHUB_OUTPUT - build_image: - needs: get_tag + build_img: + needs: tag uses: ./.github/workflows/build_image.yml with: - build-tag: ${{ needs.get_tag.outputs.tag }} + build-tag: ${{ needs.tag.outputs.tag }} cache-from: type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:latest tests: - needs: test_image + needs: [tag, build_img] uses: ./.github/workflows/run_tests.yml with: - build-tag: ${{ needs.get_tag.outputs.tag }} + build-tag: ${{ needs.tag.outputs.tag }} testscleanup: needs: tests uses: ./.github/workflows/test_cleanup.yml push: - needs: tests + needs: [tag, tests] runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 @@ -53,8 +53,8 @@ jobs: file: dev/Dockerfile platforms: linux/amd64,linux/arm64 push: true - tags: ghcr.io/${{ github.repository }}:${{ github.run_number }},ghcr.io/${{ github.repository }}:${{ needs.get_tag.outputs.tag }} + tags: ghcr.io/${{ github.repository }}:${{ github.run_number }},ghcr.io/${{ github.repository }}:${{ needs.tag.outputs.tag }} cache-from: | type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:latest - type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:${{ github.ref_name }}-amd64 + type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:${{ needs.tag.outputs.tag }}-amd64 cache-to: type=registry,ref=ghcr.io/${{ github.repository }}-builder-cache:latest,mode=max From 10feb89db09ff2ef47305d91acdd7bf0ee550daa Mon Sep 17 00:00:00 2001 From: Gustavo Silva Date: Mon, 23 Jan 2023 18:31:36 +0000 Subject: [PATCH 3/3] fix: echo tag to cut --- .github/workflows/build_image.yml | 1 + .github/workflows/build_pr.yml | 2 +- .github/workflows/release.yml | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_image.yml b/.github/workflows/build_image.yml index 2e06c823..eef91138 100644 --- a/.github/workflows/build_image.yml +++ b/.github/workflows/build_image.yml @@ -2,6 +2,7 @@ name: test image permissions: packages: write + pull-requests: write on: workflow_call: diff --git a/.github/workflows/build_pr.yml b/.github/workflows/build_pr.yml index 1160c4e6..ae11cee8 100644 --- a/.github/workflows/build_pr.yml +++ b/.github/workflows/build_pr.yml @@ -1,4 +1,4 @@ -name: Pull Request CI +name: pull request ci on: pull_request: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a04b8e12..0b4170a8 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -13,7 +13,8 @@ jobs: tag: ${{ steps.doit.outputs.new }} steps: - id: doit - run: echo "new=$(${{ github.ref_name }} | cut -b 2-)" >> $GITHUB_OUTPUT + run: | + echo "new=$(echo ${{ github.ref_name }} | cut -b 2-)" >> $GITHUB_OUTPUT build_img: needs: tag