diff --git a/.github/create-pr.sh b/.github/create-pr.sh new file mode 100755 index 0000000..a9ac89c --- /dev/null +++ b/.github/create-pr.sh @@ -0,0 +1,55 @@ +#!/bin/sh + +set -e +set -x + +if [ -z "$INPUT_SOURCE_FOLDER" ] +then + echo "Source folder must be defined" + return -1 +fi + +if [ $INPUT_DESTINATION_HEAD_BRANCH == "main" ] || [ $INPUT_DESTINATION_HEAD_BRANCH == "master"] +then + echo "Destination head branch cannot be 'main' nor 'master'" + return -1 +fi + +if [ -z "$INPUT_PULL_REQUEST_REVIEWERS" ] +then + PULL_REQUEST_REVIEWERS=$INPUT_PULL_REQUEST_REVIEWERS +else + PULL_REQUEST_REVIEWERS='-r '$INPUT_PULL_REQUEST_REVIEWERS +fi + +CLONE_DIR=$(mktemp -d) + +echo "Setting git variables" +export GITHUB_TOKEN=$API_TOKEN_GITHUB +git config --global user.email "$INPUT_USER_EMAIL" +git config --global user.name "$INPUT_USER_NAME" + +echo "Cloning destination git repository" +git clone "https://$API_TOKEN_GITHUB@github.com/$INPUT_DESTINATION_REPO.git" "$CLONE_DIR" + +echo "Copying contents to git repo"-r $INPUT_USER_NAME +cp -R $INPUT_SOURCE_FOLDER "$CLONE_DIR/$INPUT_DESTINATION_FOLDER" +cd "$CLONE_DIR" +git checkout -b "$INPUT_DESTINATION_HEAD_BRANCH" + +echo "Adding git commit" +git add . +if git status | grep -q "Changes to be committed" +then + git commit --message "Update from https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA" + echo "Pushing git commit" + git push -u origin HEAD:$INPUT_DESTINATION_HEAD_BRANCH + echo "Creating a pull request" + gh pr create -t $INPUT_DESTINATION_HEAD_BRANCH \ + -b $INPUT_DESTINATION_HEAD_BRANCH \ + -B $INPUT_DESTINATION_BASE_BRANCH \ + -H $INPUT_DESTINATION_HEAD_BRANCH \ + $PULL_REQUEST_REVIEWERS +else + echo "No changes detected" +fi diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml index 7df8c33..9a28054 100644 --- a/.github/workflows/cd.yaml +++ b/.github/workflows/cd.yaml @@ -21,7 +21,7 @@ env: jobs: deployment: - name: Deployment + name: Create deployment PR runs-on: ubuntu-latest steps: - name: Checkout @@ -50,14 +50,13 @@ jobs: cat ${{ env.DEPLOYMENT_SCRIPTS_REPO }}/${{ env.SERVICE_RESOURCES_FILE }} |\ ./hcledit attribute set locals.svc_${{ steps.service-name.outputs.service_name }}_image_version '"${{ inputs.service_image_tag }}"' \ > ${{ env.SERVICE_RESOURCES_FILE }} - - name: Create pull request - uses: paygoc6/action-pull-request-another-repo@v1.0.1 + - name: Create deployment pull request env: API_TOKEN_GITHUB: ${{ secrets.api_github_token }} - with: - source_folder: "terraform-plans" - destination_repo: ${{ env.DEPLOYMENT_SCRIPTS_REPO }} - destination_base_branch: 'main' - destination_head_branch: cd/${{ inputs.environment }}/${{ github.event.repository.name }}/${{ inputs.service_image_tag }} - user_email: ${{ github.event.pusher.email }} - user_name: ${{ github.actor }} + INPUT_SOURCE_FOLDER: "terraform-plans" + INPUT_DESTINATION_REPO: ${{ env.DEPLOYMENT_SCRIPTS_REPO }} + INPUT_DESTINATION_BASE_BRANCH: "main" + INPUT_DESTINATION_HEAD_BRANCH: cd/${{ inputs.environment }}/${{ github.event.repository.name }}/${{ inputs.service_image_tag }} + INPUT_USER_EMAIL: ${{ github.event.pusher.email }} + INPUT_USER_NAME: ${{ github.actor }} + run: "${GITHUB_WORKSPACE}/.github/create-pr.sh" \ No newline at end of file diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index fd47837..0f76806 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -6,9 +6,9 @@ on: - '**' tags: - '*' - env: AWS_REGION: us-west-2 + NPM_GITHUB_TOKEN: ${{ secrets.API_TOKEN_GITHUB_PRIVATE_PACKAGES_DOWNLOAD }} jobs: integration: @@ -17,11 +17,16 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + - name: Use Node.js + uses: actions/setup-node@v3 + with: + node-version: '16' - name: Install deps run: yarn + - name: Lint + run: yarn lint - name: Build - run: yarn build - + run: yarn prisma generate && yarn build docker-image: if: ${{ !contains(github.ref, 'heads/dependabot') }} needs: @@ -56,14 +61,14 @@ jobs: ECR_REPOSITORY: ${{ github.event.repository.name }} IMAGE_TAG: ${{ steps.get-image-tag.outputs.image_tag }} run: | - docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest . + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest --build-arg NPM_GITHUB_TOKEN=${{ secrets.API_TOKEN_GITHUB_PRIVATE_PACKAGES_DOWNLOAD }} . docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest outputs: image_tag: ${{ steps.get-image-tag.outputs.image_tag }} cd_production: -# if: ${{ contains(github.ref, 'heads/master') }} + if: ${{ contains(github.ref, 'heads/main') || contains(github.ref, 'fix')}} needs: - docker-image uses: ./.github/workflows/cd.yaml