You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/articles/using-github-to-crosspost-to-dev-to.md
+55-38Lines changed: 55 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
---
2
-
title: How you can use github actions to publish to dev.to?
2
+
title: Want to crosspost to dev.to? There's a GitHub action for that.
3
3
published: false
4
-
description: When I create a new markdown article, I don't want to have copy and paste that markdown to multiple other blog sites. So, why not let an octocat handle this work for me with a github action?
5
-
tags: javascript, github, opensource, actions
4
+
description: When I create a new markdown article, I don't want to have copy and paste that markdown to multiple other blog sites. So, why not let an octocat handle this work for me with a GitHub action?
@@ -17,37 +17,33 @@ Over the last 6 years of Software Development, I've learned a lot, but I've neve
17
17
<br>
18
18
19
19
1. Let's go over the code.
20
-
2. Let's take a look at how to use the [github action](https://github.com/basicbrogrammer/crosspost-markdown).
20
+
2. Let's take a look at how to use the [GitHub Action](https://github.com/basicbrogrammer/crosspost-markdown).
21
21
22
22
# DaCode
23
23
24
24
The first order of business is to find out if your last commit contains articles.
25
25
26
26
<br>
27
27
28
-
In order to do so, we will use node's [execSync](https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options) function to run a git command which will give us the names of files changed in the given path argument.
In order to do so, we first require 2 inputs from the GitHub workflow: github-token & content-dir. These 2 arguments will be retrieved using the [@actions/core](https://github.com/actions/toolkit/tree/main/packages/core) package. The `github-token` argument will be used to initiate an [Octokit Rest client](https://octokit.github.io/rest.js/v18/) using the [@actions/github](https://github.com/actions/toolkit/tree/main/packages/github) package. We can use this instance to request the commit data. Once we have the response, all we have to do is iterate over the list of files and see if the filename match a given content directory (from the `content-dir` argument).
31
29
32
-
```
33
-
34
-
<br>
30
+
```typescript
31
+
import*ascorefrom'@actions/core';
32
+
import*asgithubfrom'@actions/github';
35
33
36
-
Let's take the return value turn it into a string, split the string up by line breaks, filter off any empty strings, and finally remove any leading and trailing whitespace with `trim()`
console.log(`Article ${data.title} NOT published. Skipping.`);
71
80
}
72
81
} catch (err) {
73
-
console.error(err);
82
+
core.setFailed(err.message);
74
83
}
75
84
};
85
+
86
+
exportdefaultpublish;
76
87
```
77
88
78
-
Currently, I am only crossposting to [dev.to](https://dev.to/basicbrogrammer), but soon I want to update the Github action to crosspost to [hashnode.com](https://hashnode.com/@basicBrogrammer) as well. 🤙
89
+
Currently, I am only crossposting to [dev.to](https://dev.to/basicbrogrammer), but soon I want to update the GitHub Action to crosspost to [hashnode.com](https://hashnode.com/@basicBrogrammer) as well. 🤙
79
90
80
91
<br>
81
92
@@ -86,13 +97,17 @@ The `devTo.publish` method uses the auth token and [node-fetch](https://www.npmj
86
97
87
98
And here is the dead simple code for our DevTo class:
88
99
89
-
```javascript
100
+
```typescript
101
+
importfetchfrom'node-fetch';
102
+
import*ascorefrom'@actions/core';
103
+
90
104
classDevTo {
105
+
token:string;
91
106
constructor() {
92
107
this.token=core.getInput('dev-to-token');
93
108
}
94
109
95
-
publish(body_markdown) {
110
+
asyncpublish(body_markdown:string):Promise<any> {
96
111
const body = {
97
112
article: {
98
113
body_markdown,
@@ -106,26 +121,26 @@ class DevTo {
106
121
'Content-Type': 'application/json',
107
122
'api-key': this.token,
108
123
},
109
-
}).then((response) =>response.json());
124
+
}).then((response:any) =>response.json());
110
125
}
111
126
}
112
127
113
-
module.exports=newDevTo();
128
+
exportdefaultnewDevTo();
114
129
```
115
130
116
131
That's really it. That's all the code needed to keep you from having to copy and paste your blog markdown to multiple distributors!
117
132
118
133
# DaAction
119
134
120
-
As I was writing this code, I was like why don't I make it a Github action and share it with anyone that would like to use it. So I did 😎.
135
+
As I was writing this code, I was like why don't I make it a GitHub Action and share it with anyone that would like to use it. So I did 😎.
121
136
122
137
<br>
123
138
124
-
Let's take a look at how you can use this action in your Github blog repo.
139
+
Let's take a look at how you can use this action in your GitHub blog repo.
125
140
126
141
<br>
127
142
128
-
Inside your Github repo, add a workflow file. Mine is `.github/workflows/crosspost.yml`.
143
+
Inside your GitHub repo, add a workflow file. Mine is `.github/workflows/crosspost.yml`.
129
144
130
145
<br>
131
146
@@ -141,6 +156,7 @@ on:
141
156
```
142
157
143
158
Next, we need to start writing the yaml for the job itself. First, we will check out the code:
159
+
144
160
```yaml
145
161
jobs:
146
162
crosspost:
@@ -154,7 +170,7 @@ Super simple.
154
170
155
171
<br>
156
172
157
-
Next step will be to run the crosspost-markdown action and pass in the necessary arguments (content-dir & dev-to-token).
173
+
Next step will be to run the crosspost-markdown action and pass in the necessary arguments (content-dir & dev-to-token). These can be set in the [secrets](https://docs.github.com/en/free-pro-team@latest/actions/reference/encrypted-secrets) section of your repo's settings.
158
174
159
175
```yaml
160
176
jobs:
@@ -164,13 +180,14 @@ jobs:
164
180
- name: Checkout Code
165
181
uses: actions/checkout@v2
166
182
167
-
- uses: basicBrogrammer/crosspost-markdown@v0.5
183
+
- uses: basicBrogrammer/crosspost-markdown@v0.1.0 # remember to see what the latest version is 8)
168
184
with:
169
185
content-dir: './content/articles/'
170
186
dev-to-token: ${{ secrets.DEV_TO }}
187
+
github-token: ${{ secrets.GITHUB_TOKEN }}
171
188
```
172
189
173
-
All done. Now when you push to your repo the Github action will run and if you have any new blog posts it will publish them to dev.to.
190
+
All done. Now when you push to your repo the GitHub Action will run and if you have any new blog posts it will publish them to dev.to.
0 commit comments