From 2e8efd25b5a2edda447d4109f141b4ab06c67c02 Mon Sep 17 00:00:00 2001 From: "Dat. Ba Dao" Date: Tue, 28 May 2024 09:29:38 +0700 Subject: [PATCH] src/commands: run migrate-push by atlasaction binary (#16) --- .circleci/test-deploy.yml | 72 ++++++++++++++++++++++++----------- src/commands/migrate_push.yml | 62 ++++++++++++++++++++++-------- src/scripts/migrate-push.sh | 36 ------------------ 3 files changed, 96 insertions(+), 74 deletions(-) delete mode 100755 src/scripts/migrate-push.sh diff --git a/.circleci/test-deploy.yml b/.circleci/test-deploy.yml index 7386284..2a2a197 100644 --- a/.circleci/test-deploy.yml +++ b/.circleci/test-deploy.yml @@ -41,7 +41,9 @@ jobs: - checkout - run: name: Mock atlas with echo.sh - command: sudo cp ./src/scripts/echo.sh /bin/atlas + command: | + sudo cp ./src/scripts/echo.sh /bin/atlas + sudo cp ./src/scripts/echo.sh /bin/atlasaction - atlas-orb/migrate_push: working_directory: testdata dir_name: my-cool-project @@ -55,24 +57,22 @@ jobs: - run: name: Check echo.out for expected output command: | - cat /tmp/echo.out - # Ensure "migrate push" was called twice with the expected arguments. - if [ $(grep -c "migrate push" /tmp/echo.out) -ne 2 ] - then - echo ERROR: expected two "migrate push" commands >&2 - exit 1 - fi - # Should include push with the git revision and latest tag. - grep -qe "my-cool-project:<< pipeline.git.revision >>" /tmp/echo.out - grep -qe "my-cool-project:latest" /tmp/echo.out - # Should include the dev URL. - grep -qe "--dev-url postgres://postgres:pass@localhost:5432/test?sslmode=disable" /tmp/echo.out - # Should include the environment. - grep -qe "--env circleci-test" /tmp/echo.out - # Should include the config. - grep -qe "--config file://atlas.hcl" /tmp/echo.out - # Should include the vars. - grep -qe "--var foo=bar --var baz=qux --var quux=corge" /tmp/echo.out + # check dir_name should be set in the environment + grep -qe "INPUT_DIR_NAME=my-cool-project" /tmp/env.out + # check tag should be set in the environment + grep -qe "INPUT_TAG=<< pipeline.git.revision >>" /tmp/env.out + # check tag latest should be set in the environment + grep -qe "INPUT_TAG=latest" /tmp/env.out + # check dir should be set in the environment + grep -qe "INPUT_DIR=file://migrations" /tmp/env.out + # check dev_url should be set in the environment + grep -qe "INPUT_DEV_URL=postgres://postgres:pass@localhost:5432/test?sslmode=disable" /tmp/env.out + # check env should be set in the environment + grep -qe "INPUT_ENV=circleci-test" /tmp/env.out + # check config should be set in the environment + grep -qe "INPUT_CONFIG=file://atlas.hcl" /tmp/env.out + # check vars should be set in the environment + grep -qe "INPUT_VARS=foo=bar baz=qux quux=corge" /tmp/env.out command-test-migrate-lint: executor: atlas-orb/default steps: @@ -108,6 +108,27 @@ jobs: # check vars should be set in the environment grep -qe "INPUT_VARS=foo=bar baz=qux quux=corge" /tmp/env.out integration-test: + docker: + - image: cimg/base:current + - image: cimg/postgres:16.2 + environment: + POSTGRES_USER: postgres + POSTGRES_DB: test + POSTGRES_PASSWORD: pass + steps: + - checkout + - atlas-orb/setup: + version: "latest" + cloud_token_env: "ATLAS_TOKEN" + - atlas-orb/migrate_lint: + working_directory: testdata + dir_name: my-cool-project + dev_url: postgres://postgres:pass@localhost:5432/test?sslmode=disable + - atlas-orb/migrate_push: + working_directory: testdata + dir_name: my-cool-project + dev_url: postgres://postgres:pass@localhost:5432/test?sslmode=disable + integration-test-gh: docker: - image: cimg/base:current - image: cimg/postgres:16.2 @@ -130,7 +151,7 @@ jobs: working_directory: testdata dir_name: my-cool-project dev_url: postgres://postgres:pass@localhost:5432/test?sslmode=disable - integration-test-lint: + integration-test-gh-lint: docker: - image: cimg/base:current environment: @@ -177,8 +198,15 @@ workflows: - command-test-setup - command-test-migrate-push - command-test-migrate-lint - - integration-test-lint: - context: ariga-atlas + - integration-test-gh: + context: ariga-atlas-gh + filters: *filters + requires: + - command-test-setup + - command-test-migrate-push + - command-test-migrate-lint + - integration-test-gh-lint: + context: ariga-atlas-gh filters: *filters requires: - command-test-setup diff --git a/src/commands/migrate_push.yml b/src/commands/migrate_push.yml index 4896af2..332101a 100755 --- a/src/commands/migrate_push.yml +++ b/src/commands/migrate_push.yml @@ -44,28 +44,58 @@ parameters: description: | The URL of the dev-database to use for analysis. For example: mysql://root:pass@localhost:3306/dev. Read more about [dev-databases](https://atlasgo.io/concepts/dev-database). + github_repo_env: + type: env_var_name + default: GITHUB_REPOSITORY + description: | + The repository name that linting results will be posted to. + github_token_env: + type: env_var_name + default: GITHUB_TOKEN + description: | + Environment variable containing the GitHub token. + If provided, the command will authenticate to GitHub. + (e.g. `GITHUB_TOKEN`) steps: - run: name: Push migrations to Atlas Cloud - command: <> + command: | + # replace GITHUB_REPOSITORY with the github_repo_env if provided + if [ -n "${<>}" ]; then + GITHUB_REPOSITORY=${<>} + fi + # replace GITHUB_TOKEN with the github_token_env if provided + if [ -n "${<>}" ]; then + GITHUB_TOKEN=${<>} + fi + atlasaction --action migrate/push working_directory: <> environment: - PARAM_DIR_NAME: <> - PARAM_TAG: <> - PARAM_DIR: <> - PARAM_CONFIG: <> - PARAM_ENV: <> - PARAM_VARS: <> - PARAM_DEV_URL: <> + INPUT_DIR_NAME: <> + INPUT_TAG: <> + INPUT_DIR: <> + INPUT_CONFIG: <> + INPUT_ENV: <> + INPUT_VARS: <> + INPUT_DEV_URL: <> - run: name: Push with latest tag to Atlas Cloud - command: <> + command: | + # replace GITHUB_REPOSITORY with the github_repo_env if provided + if [ -n "${<>}" ]; then + GITHUB_REPOSITORY=${<>} + fi + # replace GITHUB_TOKEN with the github_token_env if provided + if [ -n "${<>}" ]; then + GITHUB_TOKEN=${<>} + fi + atlasaction --action migrate/push working_directory: <> environment: - PARAM_DIR_NAME: <> - PARAM_TAG: latest - PARAM_DIR: <> - PARAM_CONFIG: <> - PARAM_ENV: <> - PARAM_VARS: <> - PARAM_DEV_URL: <> + INPUT_DIR_NAME: <> + INPUT_TAG: latest + INPUT_DIR: <> + INPUT_CONFIG: <> + INPUT_ENV: <> + INPUT_VARS: <> + INPUT_DEV_URL: <> diff --git a/src/scripts/migrate-push.sh b/src/scripts/migrate-push.sh deleted file mode 100755 index 9e0cb17..0000000 --- a/src/scripts/migrate-push.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash - -DIR_NAME=$(circleci env subst "${PARAM_DIR_NAME}") -TAG=$(circleci env subst "${PARAM_TAG}") -DIR=$(circleci env subst "${PARAM_DIR}") -CONFIG=$(circleci env subst "${PARAM_CONFIG}") -ENV=$(circleci env subst "${PARAM_ENV}") -VARS=$(circleci env subst "${PARAM_VARS}") -DEV_URL=$(circleci env subst "${PARAM_DEV_URL}") - -ARGS="" -if [ -n "$TAG" ]; then - ARGS="$DIR_NAME:$TAG" -else - # if TAG is not set, use commit SHA1 - ARGS="$DIR_NAME:$CIRCLE_SHA1" -fi -ARGS="$ARGS --dev-url $DEV_URL" -if [ -n "$DIR" ]; then - ARGS="$ARGS --dir $DIR" -fi -if [ -n "$CONFIG" ]; then - ARGS="$ARGS --config $CONFIG" -fi -if [ -n "$ENV" ]; then - ARGS="$ARGS --env $ENV" -fi -if [ -n "$VARS" ]; then - for _var in $VARS - do - ARGS="$ARGS --var $_var" - done -fi - -# shellcheck disable=SC2086 -atlas migrate push $ARGS