From a3e3ad5c86fc10c5a50c463fbb230650c10b0ac4 Mon Sep 17 00:00:00 2001 From: Suzanne Vogt Date: Wed, 13 Dec 2023 13:39:34 -0500 Subject: [PATCH] Workflows. --- .github/workflows/_update_terraform.yml | 27 ++++++----------------- .github/workflows/branch_to_staging.yml | 3 --- .github/workflows/build.yml | 29 +++++++++++-------------- .github/workflows/deploy.yml | 4 ++++ .github/workflows/pull_request.yml | 6 ++--- .github/workflows/release.yml | 4 ++++ .github/workflows/reset_staging.yml | 10 +++++++++ .github/workflows/test-branch.yml | 17 --------------- .github/workflows/test_branch.yml | 8 +++++++ 9 files changed, 49 insertions(+), 59 deletions(-) create mode 100644 .github/workflows/reset_staging.yml delete mode 100644 .github/workflows/test-branch.yml create mode 100644 .github/workflows/test_branch.yml diff --git a/.github/workflows/_update_terraform.yml b/.github/workflows/_update_terraform.yml index f6f18d0..572ea29 100644 --- a/.github/workflows/_update_terraform.yml +++ b/.github/workflows/_update_terraform.yml @@ -5,6 +5,10 @@ on: PERSONAL_ACCESS_TOKEN: required: true inputs: + image_tag: + description: Tag for the image for docker/ghcr registries + required: true + type: string deployment_environment: description: The terraform target environment required: true @@ -13,26 +17,16 @@ on: jobs: deploy: runs-on: ubuntu-latest + env: + GIT_SHA: ${{ github.sha }} + GIT_TAG: ${{ inputs.image_tag }} steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Extract variables - shell: bash - run: | - echo "BRANCH=$(echo ${GITHUB_REF#refs/heads/} | sed 's/\//_/g')" >> $GITHUB_OUTPUT - echo "TAG=$(git tag --points-at HEAD)" >> $GITHUB_OUTPUT - echo "GIT_SHA=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT - echo "GIT_SHA_SHORT=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT - id: extract_variables - - name: Checkout terraform config repo uses: actions/checkout@v3 with: # public repo with terraform configuration repository: 'datacite/mastino' persist-credentials: false - - name: Setup dockerize and template parameters # use go template in terraform config repository to update git sha and tag # commit and push changes to trigger terraform workflow @@ -42,27 +36,20 @@ jobs: wget https://github.com/jwilder/dockerize/releases/download/v0.6.0/dockerize-linux-amd64-v0.6.0.tar.gz tar -xzvf dockerize-linux-amd64-v0.6.0.tar.gz rm dockerize-linux-amd64-v0.6.0.tar.gz - - name: Conditionally update staging environment if: ${{ (inputs.deployment_environment == 'staging') }} run: | - export GIT_SHA=${{ steps.extract_variables.outputs.GIT_SHA_SHORT }} - export GIT_TAG=${{ steps.extract_variables.outputs.GIT_TAG }} ./dockerize -template stage/services/mds/_poodle.auto.tfvars.tmpl:stage/services/mds/_poodle.auto.tfvars git add stage/services/mds/_poodle.auto.tfvars git commit -m "Adding poodle git variables for commit ${{ steps.extract_variables.outputs.GIT_SHA }}" - - name: Conditionally update production/test environments if: ${{ (inputs.deployment_environment == 'production') }} run: | - export GIT_SHA=${{ steps.extract_variables.outputs.GIT_SHA_SHORT }} - export GIT_TAG=${{ steps.extract_variables.outputs.GIT_TAG }} ./dockerize -template prod-eu-west/services/mds/_poodle.auto.tfvars.tmpl:prod-eu-west/services/mds/_poodle.auto.tfvars ./dockerize -template test/services/mds/_poodle.auto.tfvars.tmpl:test/services/mds/_poodle.auto.tfvars git add prod-eu-west/services/mds/_poodle.auto.tfvars git add test/services/mds/_poodle.auto.tfvars git commit -m "Adding poodle git variables for tag ${{ steps.extract_variables.outputs.GIT_TAG }}" - - name: Push changes uses: ad-m/github-push-action@v0.7.0 with: diff --git a/.github/workflows/branch_to_staging.yml b/.github/workflows/branch_to_staging.yml index 723f61f..96bc22a 100644 --- a/.github/workflows/branch_to_staging.yml +++ b/.github/workflows/branch_to_staging.yml @@ -8,9 +8,6 @@ jobs: call_build_and_push: needs: test uses: ./.github/workflows/build.yml - with: - image_name: ${{ github.repository }} - image_tag: ${{ github.ref_name }} secrets: inherit deploy: needs: [test, call_build_and_push] diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e9647cc..0aaa418 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,6 +6,15 @@ on: required: true DOCKERHUB_TOKEN: required: true + inputs: + image_name: + description: The name of the image for docker/ghcr registries + required: true + type: string + image_tag: + description: Tag for the image for docker/ghcr registries + required: true + type: string jobs: build: runs-on: ubuntu-latest @@ -32,27 +41,15 @@ jobs: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Get git tag - run: | - echo "GIT_TAG=$(git describe --tags $(git rev-list --tags --max-count=1))" >> $GITHUB_OUTPUT - id: set_git_vars - - name: Push to Docker Hub + - name: Build and Push uses: docker/build-push-action@v4 with: context: . file: ./Dockerfile push: true - tags: ${{ github.repository }}:latest + tags: | + ${{ inputs.image_name }}:${{ inputs.image_tag }} + ghcr.io/${{ inputs.image_name }}:${{ inputs.image_tag }} cache-from: type=local,src=/tmp/.buildx-cache cache-to: type=local,dest=/tmp/.buildx-cache # no-cache: true - - name: Push to GitHub Packages - uses: docker/build-push-action@v4 - with: - context: . - file: ./Dockerfile - push: true - tags: ghcr.io/${{ github.repository }}:latest - cache-from: type=local,src=/tmp/.buildx-cache - cache-to: type=local,dest=/tmp/.buildx-cache - # no-cache: true \ No newline at end of file diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 43fe601..0b96571 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -10,10 +10,14 @@ jobs: call_build_and_push: needs: test uses: ./.github/workflows/build.yml + with: + image_name: ${{ github.repository }} + image_tag: main secrets: inherit deploy: needs: [test, call_build_and_push] uses: ./.github/workflows/_update_terraform.yml with: + image_tag: main deployment_environment: staging secrets: inherit diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index a5522bb..5cd06dc 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -1,8 +1,8 @@ name: Test Pull Request on: - # pull_request: - # branches: - # - master + pull_request: + branches: + - master workflow_dispatch: jobs: test: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 00759f8..59700ae 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -9,10 +9,14 @@ jobs: call_build_and_push: needs: test uses: ./.github/workflows/build.yml + with: + image_name: ${{ github.repository }} + image_tag: ${{ github.ref_name }} secrets: inherit deploy: needs: [test, call_build_and_push] uses: ./.github/workflows/_update_terraform.yml with: + image_tag: ${{ github.ref_name }} deployment_environment: production secrets: inherit diff --git a/.github/workflows/reset_staging.yml b/.github/workflows/reset_staging.yml new file mode 100644 index 0000000..aa9e039 --- /dev/null +++ b/.github/workflows/reset_staging.yml @@ -0,0 +1,10 @@ +name: Reset Staging to Main Branch +on: + workflow_dispatch: +jobs: + deploy: + uses: ./.github/workflows/_update_terraform.yml + with: + image_tag: main + deployment_environment: staging + secrets: inherit diff --git a/.github/workflows/test-branch.yml b/.github/workflows/test-branch.yml deleted file mode 100644 index 96bc22a..0000000 --- a/.github/workflows/test-branch.yml +++ /dev/null @@ -1,17 +0,0 @@ -name: Build/Deploy Branch to Staging -on: - workflow_dispatch: -jobs: - test: - uses: ./.github/workflows/ci.yml - secrets: inherit - call_build_and_push: - needs: test - uses: ./.github/workflows/build.yml - secrets: inherit - deploy: - needs: [test, call_build_and_push] - uses: ./.github/workflows/_update_terraform.yml - with: - deployment_environment: staging - secrets: inherit diff --git a/.github/workflows/test_branch.yml b/.github/workflows/test_branch.yml new file mode 100644 index 0000000..f5bb562 --- /dev/null +++ b/.github/workflows/test_branch.yml @@ -0,0 +1,8 @@ +name: Test Branch +on: + workflow_dispatch: +jobs: + test: + uses: ./.github/workflows/ci.yml + secrets: inherit +