From 3a8e2a81f7f6fa1a708b22aba3b2cdf925dbd536 Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Fri, 21 Jun 2024 14:09:02 +0100 Subject: [PATCH] Remove old actions and use new build scripts (#1658) --- .github/actions/docker-build/action.yml | 69 ---------------------- .github/actions/get-version-tag/action.yml | 21 ------- .github/actions/install-aws/action.yml | 27 --------- .github/workflows/branch-cleanup.yml | 2 +- .github/workflows/cd-feature.yml | 32 ---------- .github/workflows/cd-main.yml | 34 ----------- .github/workflows/cd.yml | 47 +++++++++++++++ .github/workflows/{ci.yml => pr.yml} | 5 +- .github/workflows/release.yml | 32 ---------- .github/workflows/remove-untagged.yml | 29 --------- 10 files changed, 52 insertions(+), 246 deletions(-) delete mode 100644 .github/actions/docker-build/action.yml delete mode 100644 .github/actions/get-version-tag/action.yml delete mode 100644 .github/actions/install-aws/action.yml delete mode 100644 .github/workflows/cd-feature.yml delete mode 100644 .github/workflows/cd-main.yml create mode 100644 .github/workflows/cd.yml rename .github/workflows/{ci.yml => pr.yml} (75%) delete mode 100644 .github/workflows/release.yml delete mode 100644 .github/workflows/remove-untagged.yml diff --git a/.github/actions/docker-build/action.yml b/.github/actions/docker-build/action.yml deleted file mode 100644 index 1b59643d4..000000000 --- a/.github/actions/docker-build/action.yml +++ /dev/null @@ -1,69 +0,0 @@ -name: Build Docker image - -inputs: - version: - required: true - type: string - latest: - required: false - type: boolean - default: false - github-token: - required: true - type: string - docker-image-name: - required: true - type: string - -runs: - using: "composite" - steps: - - uses: actions/checkout@v4 - - name: Log in to registry - run: echo "${{ inputs.github-token }}" | docker login ghcr.io -u $ --password-stdin - shell: bash - - uses: hadolint/hadolint-action@v3.1.0 - with: - dockerfile: Dockerfile - ignore: DL3045,DL3007 - - name: Build and push image - run: | - IMAGE_ID=$(echo "ghcr.io/${{ github.repository_owner }}/${{ inputs.docker-image-name }}" | tr '[A-Z]' '[a-z]') - echo "IMAGE_ID=$IMAGE_ID" >> "$GITHUB_OUTPUT" - echo $IMAGE_ID - docker build --tag $IMAGE_ID:${{ inputs.version }} --platform=linux/amd64 --no-cache . - docker push $IMAGE_ID:${{ inputs.version }} - shell: bash - - name: Tag latest - if: inputs.latest - run: | - IMAGE_ID=$(echo "ghcr.io/${{ github.repository_owner }}/${{ inputs.docker-image-name }}" | tr '[A-Z]' '[a-z]') - echo $IMAGE_ID - docker tag $IMAGE_ID:${{ inputs.version }} $IMAGE_ID:latest - docker push $IMAGE_ID:latest - shell: bash - - name: List all images - run: docker image ls - shell: bash - - name: List dangling images - run: docker images -f "dangling=true" - shell: bash - # - name: Remove untagged images - # run: docker rmi $(docker images -f "dangling=true" -q) - # shell: bash - # - name: Delete untagged images - # uses: actions/github-script@v6.3.3 - # with: - # github-token: ${{ inputs.github-token }} - # script: | - # const response = await github.request("GET /orgs/${{ github.repository_owner }}/packages/container/${{ IMAGE_ID }}/versions", - # { per_page: 100 - # }); - # for(version of response.data) { - # if (version.metadata.container.tags.length == 0 && version.name !== "latest" && version.name !== "preview") { - # console.log("Delete " + version.id) - - # const deleteResponse = await github.request("DELETE /orgs/${{ github.repository_owner }}/packages/container/${{ IMAGE_ID }}/versions/" + version.id, { }); - # console.log("status " + deleteResponse.status) - # } - # } diff --git a/.github/actions/get-version-tag/action.yml b/.github/actions/get-version-tag/action.yml deleted file mode 100644 index 4f3f29c5b..000000000 --- a/.github/actions/get-version-tag/action.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Get version tag - -outputs: - version-tag: - description: "Version tag" - value: ${{ steps.version-tag.outputs.VERSION }} - -runs: - using: "composite" - steps: - - id: version-tag - name: Get tag from branch, tag or release - run: | - VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') - # Strip "v" prefix from tag name - [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') - [ "$VERSION" == "main" ] && VERSION=develop - [ "$VERSION" == "develop" ] && VERSION=develop - echo "Version: $VERSION" - echo "VERSION=$VERSION" >> "$GITHUB_OUTPUT" - shell: bash diff --git a/.github/actions/install-aws/action.yml b/.github/actions/install-aws/action.yml deleted file mode 100644 index c08820edc..000000000 --- a/.github/actions/install-aws/action.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Set up AWS CLI - -inputs: - aws-access-key-id: - required: true - type: string - aws-secret-access-key: - required: true - type: string - -runs: - using: "composite" - steps: - - uses: actions/checkout@v4 - # - name: Install AWS CLI - # run: | - # curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" - # unzip awscliv2.zip - # ./aws/install --update - # aws --version - # shell: bash - - name: Configure AWS CLI - run: | - export AWS_ACCESS_KEY_ID=${{ inputs.aws-access-key-id }} - export AWS_SECRET_ACCESS_KEY=${{ inputs.aws-secret-access-key }} - aws configure --region eu-west-2 --output json - shell: bash diff --git a/.github/workflows/branch-cleanup.yml b/.github/workflows/branch-cleanup.yml index 51afb3292..17b405a6f 100644 --- a/.github/workflows/branch-cleanup.yml +++ b/.github/workflows/branch-cleanup.yml @@ -11,7 +11,7 @@ jobs: - uses: actions/checkout@v4 - name: Get tag id: version-tag - uses: ./.github/actions/get-version-tag + run: echo "VERSION=$(echo "${{ github.event.ref }}" | sed -e 's,/,-,g')" >> "$GITHUB_OUTPUT" - name: Debug run: echo "Clean up Docker image ${{ vars.DOCKER_IMAGE_NAME }}:${{ steps.version-tag.outputs.VERSION }}" - name: Delete image diff --git a/.github/workflows/cd-feature.yml b/.github/workflows/cd-feature.yml deleted file mode 100644 index ed4cae69a..000000000 --- a/.github/workflows/cd-feature.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: Deploy feature branch - -on: - workflow_dispatch: - # push: - # branches: - # - feature/* - -jobs: - deploy: - name: Deploy to AWS - runs-on: ubuntu-latest - permissions: - packages: write - contents: read - steps: - - uses: actions/checkout@v4 - - name: Get tag - id: version-tag - uses: ./.github/actions/get-version-tag - - name: Build Docker image - uses: ./.github/actions/docker-build - with: - version: ${{ steps.version-tag.outputs.version-tag }} # (feature/abc) - github-token: ${{ secrets.GITHUB_TOKEN }} - docker-image-name: ${{ vars.DOCKER_IMAGE_NAME }} - # - name: Install AWS CLI - # uses: ./.github/actions/install-aws - # with: - # aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} - # aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - # TODO: Deploy new feature environment diff --git a/.github/workflows/cd-main.yml b/.github/workflows/cd-main.yml deleted file mode 100644 index 4c9843752..000000000 --- a/.github/workflows/cd-main.yml +++ /dev/null @@ -1,34 +0,0 @@ -name: Continuous deployment to preview - -on: - # workflow_dispatch: - push: - branches: - - develop # TODO: Change to "main" if our working branch changes - -jobs: - deploy: - name: Deploy to AWS - runs-on: ubuntu-latest - permissions: - packages: write - contents: read - steps: - - uses: actions/checkout@v4 - - name: Get tag - id: version-tag - uses: ./.github/actions/get-version-tag - - name: Build Docker image - uses: ./.github/actions/docker-build - with: - version: ${{ steps.version-tag.outputs.version-tag }} # (develop) - latest: true - github-token: ${{ secrets.GITHUB_TOKEN }} - docker-image-name: ${{ vars.DOCKER_IMAGE_NAME }} - # - name: Install AWS CLI - # uses: ./.github/actions/install-aws - # with: - # aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} - # aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - # - name: Deploy to AWS - # run: aws ssm put-parameter --profile preview --name "/devops/private-beta/version" --type "String" --value "${{ steps.version-tag.outputs.VERSION }}" --overwrite diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 000000000..4df05ea72 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,47 @@ +name: Build and deploy + +on: + workflow_dispatch: + push: + release: + types: + - published + +concurrency: + group: cd-${{ github.ref }} + +jobs: + test: + name: Tests + uses: ./.github/workflows/_tests.yml + with: + python-version: ${{ vars.CI_PYTHON_VERSION }} + poetry-version: ${{ vars.CI_POETRY_VERSION }} + + version: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Get tag + id: version-tag + uses: nationalarchives/ds-docker-actions/.github/actions/get-version-tag@main + outputs: + version: ${{ steps.version-tag.outputs.version-tag }} + + build: + runs-on: ubuntu-latest + needs: + - test + - version + permissions: + packages: write + contents: read + steps: + - uses: actions/checkout@v4 + - name: Build Docker image + uses: nationalarchives/ds-docker-actions/.github/actions/docker-build@main + with: + version: ${{ needs.version.outputs.version }} + latest: ${{ github.ref == 'refs/heads/main' }} + github-token: ${{ secrets.GITHUB_TOKEN }} + docker-image-name: ${{ vars.DOCKER_IMAGE_NAME }} diff --git a/.github/workflows/ci.yml b/.github/workflows/pr.yml similarity index 75% rename from .github/workflows/ci.yml rename to .github/workflows/pr.yml index 1d18ad1e2..f16a6e301 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/pr.yml @@ -1,7 +1,10 @@ -name: CI +name: Pull request on: pull_request: + types: + - opened + - synchronize jobs: ci: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index d8e05776c..000000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: Release to staging - -on: - release: - types: - - published - -jobs: - build: - name: Build and deploy to AWS - runs-on: ubuntu-latest - permissions: - packages: write - contents: read - steps: - - uses: actions/checkout@v4 - - name: Get tag - id: version-tag - uses: ./.github/actions/get-version-tag - - name: Build - uses: ./.github/actions/docker-build - with: - version: ${{ steps.version-tag.outputs.version-tag }} # (e.g. 23.09.24.1) - github-token: ${{ secrets.GITHUB_TOKEN }} - docker-image-name: ${{ vars.DOCKER_IMAGE_NAME }} - # - name: Install AWS CLI - # uses: ./.github/actions/install-aws - # with: - # aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} - # aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - # - name: Deploy to AWS - # run: aws ssm put-parameter --profile staging --name "/devops/private-beta/version" --type "String" --value "${{ steps.version-tag.outputs.VERSION }}" --overwrite diff --git a/.github/workflows/remove-untagged.yml b/.github/workflows/remove-untagged.yml deleted file mode 100644 index afcd500ba..000000000 --- a/.github/workflows/remove-untagged.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Remove untagged container images - -on: - workflow_dispatch: - # schedule: - # - cron: "0 3 * * 1" - -jobs: - remove-untagged: - runs-on: ubuntu-latest - env: - PER_PAGE: 100 - steps: - - name: Delete untagged images - uses: actions/github-script@v7.0.1 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const response = await github.request("GET /orgs/${{ github.repository_owner }}/packages/container/${{ vars.DOCKER_IMAGE_NAME }}/versions", - { per_page: ${{ env.PER_PAGE }} - }); - for(version of response.data) { - if (version.metadata.container.tags.length == 0 && version.name !== "latest" && version.name !== "preview") { - console.log("Delete " + version.id) - - // const deleteResponse = await github.request("DELETE /orgs/${{ github.repository_owner }}/packages/container/${{ vars.DOCKER_IMAGE_NAME }}/versions/" + version.id, { }); - // console.log("status " + deleteResponse.status) - } - } \ No newline at end of file